Class: Chef::Formatters::IndentableOutputStream

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/formatters/indentable_output_stream.rb

Overview

Handles basic indentation and colorization tasks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out, err) ⇒ IndentableOutputStream


13
14
15
16
17
18
# 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

Instance Attribute Details

#current_streamObject

Returns the value of attribute current_stream


10
11
12
# File 'lib/chef/formatters/indentable_output_stream.rb', line 10

def current_stream
  @current_stream
end

#errObject (readonly)

Returns the value of attribute err


7
8
9
# File 'lib/chef/formatters/indentable_output_stream.rb', line 7

def err
  @err
end

#indentObject

Returns the value of attribute indent


8
9
10
# File 'lib/chef/formatters/indentable_output_stream.rb', line 8

def indent
  @indent
end

#line_startedObject (readonly)

Returns the value of attribute line_started


9
10
11
# File 'lib/chef/formatters/indentable_output_stream.rb', line 9

def line_started
  @line_started
end

#outObject (readonly)

Returns the value of attribute out


6
7
8
# File 'lib/chef/formatters/indentable_output_stream.rb', line 6

def out
  @out
end

#semaphoreObject (readonly)

Returns the value of attribute semaphore


11
12
13
# File 'lib/chef/formatters/indentable_output_stream.rb', line 11

def semaphore
  @semaphore
end

Instance Method Details

#<<(obj) ⇒ Object

Print a raw chunk


54
55
56
# File 'lib/chef/formatters/indentable_output_stream.rb', line 54

def <<(obj)
  print(obj)
end

#color(string, *args) ⇒ Object

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).


30
31
32
# File 'lib/chef/formatters/indentable_output_stream.rb', line 30

def color(string, *args)
  print(string, from_args(args))
end

#highlineObject


20
21
22
23
24
25
# File 'lib/chef/formatters/indentable_output_stream.rb', line 20

def highline
  @highline ||= begin
    require "highline"
    HighLine.new
  end
end

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)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/formatters/indentable_output_stream.rb', line 75

def print(string, *args)
  options = from_args(args)

  # Make sure each line stays a unit even with threads sending output
  semaphore.synchronize do
    if should_start_line?(options)
      move_to_next_line
    end

    print_string(string, options)

    if should_end_line?(options)
      move_to_next_line
    end
  end
end

#puts(string, *args) ⇒ Object

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


43
44
45
# File 'lib/chef/formatters/indentable_output_stream.rb', line 43

def puts(string, *args)
  print(string, from_args(args, :end_line => true))
end

#puts_line(string, *args) ⇒ Object

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


49
50
51
# File 'lib/chef/formatters/indentable_output_stream.rb', line 49

def puts_line(string, *args)
  print(string, from_args(args, :start_line => true, :end_line => true))
end

#start_line(string, *args) ⇒ Object

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).


37
38
39
# File 'lib/chef/formatters/indentable_output_stream.rb', line 37

def start_line(string, *args)
  print(string, from_args(args, :start_line => true))
end