class Convoy::Formatter::StreamOutputFormatter

Constants

DEFAULT_INDENT
DEFAULT_INDENT_STRING
DEFAULT_OUTPUT_WIDTH

Attributes

current_indent[R]
cursor_position[R]
indent_string[R]
max_output_width[R]
stream[R]

Public Class Methods

new(stream = $stdout, options = {}, &block) click to toggle source
# File lib/convoy/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

Public Instance Methods

grid(options = {}, &block) click to toggle source
# File lib/convoy/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) click to toggle source
# File lib/convoy/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) click to toggle source
# File lib/convoy/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) click to toggle source
puts(string, options = { :newlines => 1 }) click to toggle source
# File lib/convoy/formatter/stream_output_formatter.rb, line 32
def puts(string, options = { :newlines => 1 })
    print(string)
    newline(options[:newlines])
end

Private Instance Methods

current_indent_string() click to toggle source
# File lib/convoy/formatter/stream_output_formatter.rb, line 78
def current_indent_string
    indent_string * current_indent
end
current_indent_width() click to toggle source
# File lib/convoy/formatter/stream_output_formatter.rb, line 74
def current_indent_width
    current_indent_string.length
end
pad_string_to_account_for_cursor_position(string) click to toggle source
# File lib/convoy/formatter/stream_output_formatter.rb, line 58
def pad_string_to_account_for_cursor_position(string)
    "#{padding_string}#{string}"
end
padding_string() click to toggle source
# File lib/convoy/formatter/stream_output_formatter.rb, line 70
def padding_string
    "." * cursor_position.position
end
remove_padding_that_account_for_cursor_position(segments) click to toggle source
# File lib/convoy/formatter/stream_output_formatter.rb, line 62
def remove_padding_that_account_for_cursor_position(segments)
    first_string = segments.first
    if first_string
        segments[0] = first_string.sub(/#{padding_string}/, '')
    end
    segments
end