Class: RightScale::RightPopen::SafeOutputBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/right_popen/safe_output_buffer.rb

Overview

Provides an output handler implementation that buffers output (from a child process) while ensuring that the output does not exhaust memory in the current process. it does this by preserving only the most interesting bits of data (start of lines, last in output).

Constant Summary collapse

ELLIPSIS =

note utf-8 encodings for the Unicode elipsis character are inconsistent between ruby platforms (Windows vs Linux) and versions (1.8 vs 1.9).

'...'
DEFAULT_MAX_LINE_COUNT =
64
DEFAULT_MAX_LINE_LENGTH =
256

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer = [], max_line_count = DEFAULT_MAX_LINE_COUNT, max_line_length = DEFAULT_MAX_LINE_LENGTH) ⇒ SafeOutputBuffer

Parameters

Parameters:

  • buffer (Array) (defaults to: [])

    for lines

  • max_line_count (Integer) (defaults to: DEFAULT_MAX_LINE_COUNT)

    to limit number of lines in buffer

  • max_line_length (Integer) (defaults to: DEFAULT_MAX_LINE_LENGTH)

    to truncate charcters from start of line

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
# File 'lib/right_popen/safe_output_buffer.rb', line 50

def initialize(buffer = [],
               max_line_count = DEFAULT_MAX_LINE_COUNT,
               max_line_length = DEFAULT_MAX_LINE_LENGTH)
  raise ArgumentError.new('buffer is required') unless @buffer = buffer
  raise ArgumentError.new('max_line_count is invalid') unless (@max_line_count = max_line_count) > 1
  raise ArgumentError.new('max_line_length is invalid') unless (@max_line_length = max_line_length) > ELLIPSIS.length
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



44
45
46
# File 'lib/right_popen/safe_output_buffer.rb', line 44

def buffer
  @buffer
end

#max_line_countObject (readonly)

Returns the value of attribute max_line_count.



44
45
46
# File 'lib/right_popen/safe_output_buffer.rb', line 44

def max_line_count
  @max_line_count
end

#max_line_lengthObject (readonly)

Returns the value of attribute max_line_length.



44
45
46
# File 'lib/right_popen/safe_output_buffer.rb', line 44

def max_line_length
  @max_line_length
end

Instance Method Details

#display_textObject



58
# File 'lib/right_popen/safe_output_buffer.rb', line 58

def display_text; @buffer.join("\n"); end

#safe_buffer_data(data) ⇒ Object

Buffers data with specified truncation.

Parameters

Parameters:

  • data (Object)

    of any kind



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/right_popen/safe_output_buffer.rb', line 64

def safe_buffer_data(data)
  # note that the chomping ensures that the exact output cannot be
  # preserved but the truncation would tend to eliminate trailing newlines
  # in any case. if you want exact output then don't use safe buffering.
  data = data.to_s.chomp
  if @buffer.size >= @max_line_count
    @buffer.shift
    @buffer[0] = ELLIPSIS
  end
  if data.length > @max_line_length
    truncation = [data.length - (@max_line_length - ELLIPSIS.length), 0].max
    data = "#{data[0..(@max_line_length - ELLIPSIS.length - 1)]}#{ELLIPSIS}"
  end
  @buffer << data
  true
end