class Chef::Formatters::IndentableOutputStream

Handles basic indentation and colorization tasks

Attributes

current_stream[RW]
err[R]
indent[RW]
line_started[R]
out[R]
semaphore[R]

Public Class Methods

new(out, err) click to toggle source
# File lib/chef/formatters/indentable_output_stream.rb, line 13
def initialize(out, err)
  @out, @err = out, err
  @indent = 0
  @line_started = false
  @semaphore = Mutex.new
end

Public Instance Methods

<<(obj) click to toggle source

Print a raw chunk

# File lib/chef/formatters/indentable_output_stream.rb, line 66
def <<(obj)
  print(obj)
end
color(string, *args) click to toggle source

Print text. This will start a new line and indent if necessary but will not terminate the line (future print and puts statements will start off where this print left off).

@param string [String] @param args [Array<Hash,Symbol>]

# File lib/chef/formatters/indentable_output_stream.rb, line 33
def color(string, *args)
  print(string, from_args(args))
end
highline() click to toggle source
# File lib/chef/formatters/indentable_output_stream.rb, line 20
def highline
  @highline ||= begin
    require "highline"
    HighLine.new
  end
end
print(string, *args) click to toggle source

Print a string.

Arguments

string: string to print. options: a hash with these possible options:

  • :stream => OBJ: unique identifier for a stream. If two prints have

    different streams, they will print on separate lines.
    Otherwise, they will stay together.
  • :start_line => BOOLEAN: if true, print will begin on a blank (indented) line.

  • :end_line => BOOLEAN: if true, current line will be ended.

  • :name => STRING: a name to prefix in front of a stream. It will be printed

    once (with the first line of the stream) and subsequent lines
    will be indented to match.

Alternative

You may also call print('string', :red) (a list of colors a la Highline.color)

puts(string, *args) click to toggle source

Print a line. This will continue from the last start_line or print, or start a new line and indent if necessary.

@param string [String] @param args [Array<Hash,Symbol>]

# File lib/chef/formatters/indentable_output_stream.rb, line 52
def puts(string, *args)
  print(string, from_args(args, end_line: true))
end
puts_line(string, *args) click to toggle source

Print an entire line from start to end. This will terminate any existing lines and cause indentation.

@param string [String] @param args [Array<Hash,Symbol>]

# File lib/chef/formatters/indentable_output_stream.rb, line 61
def puts_line(string, *args)
  print(string, from_args(args, start_line: true, end_line: true))
end
start_line(string, *args) click to toggle source

Print the start of a new line. This will terminate any existing lines and cause indentation but will not move to the next line yet (future 'print' and 'puts' statements will stay on this line).

@param string [String] @param args [Array<Hash,Symbol>]

# File lib/chef/formatters/indentable_output_stream.rb, line 43
def start_line(string, *args)
  print(string, from_args(args, start_line: true))
end

Private Instance Methods

from_args(colors, merge_options = {}) click to toggle source
# File lib/chef/formatters/indentable_output_stream.rb, line 114
def from_args(colors, merge_options = {})
  if colors.size == 1 && colors[0].kind_of?(Hash)
    merge_options.merge(colors[0])
  else
    merge_options.merge({ colors: colors })
  end
end
indent_line(options) click to toggle source
# File lib/chef/formatters/indentable_output_stream.rb, line 156
def indent_line(options)
  if !@line_started

    # Print indents.  If there is a stream name, either print it (if we're
    # switching streams) or print enough blanks to match
    # the indents.
    if options[:name]
      if @current_stream != options[:stream]
        @out.print "#{(' ' * indent)}[#{options[:name]}] "
      else
        @out.print " " * (indent + 3 + options[:name].size)
      end
    else
      # Otherwise, just print indents.
      @out.print " " * indent
    end

    if @current_stream != options[:stream]
      @current_stream = options[:stream]
    end

    @line_started = true
  end
end
move_to_next_line() click to toggle source
# File lib/chef/formatters/indentable_output_stream.rb, line 149
def move_to_next_line
  if @line_started
    @out.puts ""
    @line_started = false
  end
end
print_line(line, options) click to toggle source
print_string(string, options) click to toggle source
should_end_line?(options) click to toggle source
# File lib/chef/formatters/indentable_output_stream.rb, line 110
def should_end_line?(options)
  options[:end_line] && @line_started
end
should_start_line?(options) click to toggle source
# File lib/chef/formatters/indentable_output_stream.rb, line 106
def should_start_line?(options)
  options[:start_line] || @current_stream != options[:stream]
end