Class: ChefEmailReporter::Formatters::MultipartOutputStream

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-email-reporter/formatters/multipart_output_stream.rb

Overview

Handles basic indentation and colorization tasks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMultipartOutputStream

Returns a new instance of MultipartOutputStream.



33
34
35
36
37
38
39
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 33

def initialize
  @text_stream = StringIO.new
  @html_stream = StringIO.new
  @indent = 0
  @line_started = false
  @semaphore = Mutex.new
end

Instance Attribute Details

#current_streamObject

Returns the value of attribute current_stream.



30
31
32
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 30

def current_stream
  @current_stream
end

#indentObject

Returns the value of attribute indent.



28
29
30
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 28

def indent
  @indent
end

#line_startedObject (readonly)

Returns the value of attribute line_started.



29
30
31
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 29

def line_started
  @line_started
end

#semaphoreObject (readonly)

Returns the value of attribute semaphore.



31
32
33
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 31

def semaphore
  @semaphore
end

Instance Method Details

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



54
55
56
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 54

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

#html_partObject



46
47
48
49
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 46

def html_part
  @html_stream.rewind
  @html_stream.read
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)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 95

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.



67
68
69
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 67

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.



73
74
75
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 73

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



61
62
63
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 61

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

#text_partObject



41
42
43
44
# File 'lib/chef-email-reporter/formatters/multipart_output_stream.rb', line 41

def text_part
  @text_stream.rewind
  @text_stream.read
end