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

Returns a new instance of 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


57
58
59
# File 'lib/chef/formatters/indentable_output_stream.rb', line 57

def <<(obj)
  print(obj)
end

#pastelObject

pastel.decorate is a lightweight replacement for highline.color


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

def pastel
  @pastel ||= begin
    require "pastel" unless defined?(Pastel)
    Pastel.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) (https://github.com/piotrmurach/pastel#3-supported-colors)


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

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.

Parameters:


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.

Parameters:


52
53
54
# File 'lib/chef/formatters/indentable_output_stream.rb', line 52

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

Parameters:


34
35
36
# File 'lib/chef/formatters/indentable_output_stream.rb', line 34

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