Class: Escort::Formatter::StreamOutputFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/escort/formatter/stream_output_formatter.rb

Constant Summary collapse

DEFAULT_OUTPUT_WIDTH =
80
DEFAULT_INDENT_STRING =
' '
DEFAULT_INDENT =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream = $stdout, options = {}, &block) ⇒ StreamOutputFormatter


10
11
12
13
14
15
16
17
# File 'lib/escort/formatter/stream_output_formatter.rb', line 10

def initialize(stream = $stdout, options = {}, &block)
  @stream = stream
  @max_output_width = options[:max_output_width] || DEFAULT_OUTPUT_WIDTH
  @indent_string = options[:indent_string] || DEFAULT_INDENT_STRING
  @current_indent = options[:current_indent] || DEFAULT_INDENT
  @cursor_position = CursorPosition.new(@max_output_width)
  block.call(self) if block_given?
end

Instance Attribute Details

#current_indentObject (readonly)

Returns the value of attribute current_indent


8
9
10
# File 'lib/escort/formatter/stream_output_formatter.rb', line 8

def current_indent
  @current_indent
end

#cursor_positionObject (readonly)

Returns the value of attribute cursor_position


8
9
10
# File 'lib/escort/formatter/stream_output_formatter.rb', line 8

def cursor_position
  @cursor_position
end

#indent_stringObject (readonly)

Returns the value of attribute indent_string


8
9
10
# File 'lib/escort/formatter/stream_output_formatter.rb', line 8

def indent_string
  @indent_string
end

#max_output_widthObject (readonly)

Returns the value of attribute max_output_width


8
9
10
# File 'lib/escort/formatter/stream_output_formatter.rb', line 8

def max_output_width
  @max_output_width
end

#streamObject (readonly)

Returns the value of attribute stream


8
9
10
# File 'lib/escort/formatter/stream_output_formatter.rb', line 8

def stream
  @stream
end

Instance Method Details

#grid(options = {}, &block) ⇒ Object


48
49
50
51
52
53
54
# File 'lib/escort/formatter/stream_output_formatter.rb', line 48

def grid(options = {}, &block)
  if block_given?
    options[:width] ||= max_output_width
    grid = StringGrid.new(options, &block)
    puts grid.to_s
  end
end

#indent(count, &block) ⇒ Object


42
43
44
45
46
# File 'lib/escort/formatter/stream_output_formatter.rb', line 42

def indent(count, &block)
  newline unless cursor_position.newline?
  new_indent = current_indent + count
  self.class.new(stream, :max_output_width => max_output_width - count, :indent_string => indent_string, :current_indent => new_indent, &block)
end

#newline(newline_count = 1) ⇒ Object


37
38
39
40
# File 'lib/escort/formatter/stream_output_formatter.rb', line 37

def newline(newline_count = 1)
  stream.print("\n" * newline_count)
  cursor_position.reset if newline_count > 0
end

19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/escort/formatter/stream_output_formatter.rb', line 19

def print(string)
  splitter_input = (string.to_s)
  segments = StringSplitter.new(max_output_width).split(splitter_input)
  segments = (segments)
  segments.each do |segment|
    output_string = "#{current_indent_string}#{segment}"
    output_string = segment unless cursor_position.newline?
    stream.print output_string
    cursor_position.update_for(segment)
    newline if segments.last != segment
  end
end

#puts(string, options = {:newlines => 1}) ⇒ Object


32
33
34
35
# File 'lib/escort/formatter/stream_output_formatter.rb', line 32

def puts(string, options = {:newlines => 1})
  print(string)
  newline(options[:newlines])
end