Class: Escort::Formatter::StreamOutputFormatter
- Inherits:
-
Object
- Object
- Escort::Formatter::StreamOutputFormatter
- 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
-
#current_indent ⇒ Object
readonly
Returns the value of attribute current_indent.
-
#cursor_position ⇒ Object
readonly
Returns the value of attribute cursor_position.
-
#indent_string ⇒ Object
readonly
Returns the value of attribute indent_string.
-
#max_output_width ⇒ Object
readonly
Returns the value of attribute max_output_width.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
- #grid(options = {}, &block) ⇒ Object
- #indent(count, &block) ⇒ Object
-
#initialize(stream = $stdout, options = {}, &block) ⇒ StreamOutputFormatter
constructor
A new instance of StreamOutputFormatter.
- #newline(newline_count = 1) ⇒ Object
- #print(string) ⇒ Object
- #puts(string, options = {:newlines => 1}) ⇒ Object
Constructor Details
#initialize(stream = $stdout, options = {}, &block) ⇒ StreamOutputFormatter
Returns a new instance of StreamOutputFormatter.
10 11 12 13 14 15 16 17 |
# File 'lib/escort/formatter/stream_output_formatter.rb', line 10 def initialize(stream = $stdout, = {}, &block) @stream = stream @max_output_width = [:max_output_width] || DEFAULT_OUTPUT_WIDTH @indent_string = [:indent_string] || DEFAULT_INDENT_STRING @current_indent = [:current_indent] || DEFAULT_INDENT @cursor_position = CursorPosition.new(@max_output_width) block.call(self) if block_given? end |
Instance Attribute Details
#current_indent ⇒ Object (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_position ⇒ Object (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_string ⇒ Object (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_width ⇒ Object (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 |
#stream ⇒ Object (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( = {}, &block) if block_given? [:width] ||= max_output_width grid = StringGrid.new(, &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 |
#print(string) ⇒ Object
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 = pad_string_to_account_for_cursor_position(string.to_s) segments = StringSplitter.new(max_output_width).split(splitter_input) segments = remove_padding_that_account_for_cursor_position(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, = {:newlines => 1}) print(string) newline([:newlines]) end |