Module: Protest::Utils::ColorfulOutput

Included in:
Reports::Documentation, Reports::Progress
Defined in:
lib/protest/utils/colorful_output.rb

Overview

Mixin that provides colorful output to your console based reports. This uses bash’s escape sequences, so it won’t work on windows.

TODO: Make this work on windows with ansicolor or whatever the gem is named

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.colorizeObject

Whether to use colors in the output or not. The default is true.



27
28
29
# File 'lib/protest/utils/colorful_output.rb', line 27

def colorize
  @colorize
end

Class Method Details

.colorsObject

Returns a hash with the color values for different states. Override this method safely to change the output colors. The defaults are:

:passed

Light green

:pending

Light yellow

:errored

Light purple

:failed

Light red

See www.hypexr.org/bash_tutorial.php#colors for a description of Bash color codes.



18
19
20
21
22
23
# File 'lib/protest/utils/colorful_output.rb', line 18

def self.colors
  { :passed => "1;32",
    :pending => "1;33",
    :errored => "1;35",
    :failed => "1;31" }
end

Instance Method Details

Print the string to whatever IO stream is defined in the method #stream using the correct color depending on the state passed.



44
45
46
47
48
49
50
# File 'lib/protest/utils/colorful_output.rb', line 44

def print(string=nil, state=:normal)
  if string.nil? # calling IO#puts with nil is not the same as with no args
    stream.print
  else
    stream.print colorize(string, state)
  end
end

#puts(string = nil, state = :normal) ⇒ Object

Print the string followed by a newline to whatever IO stream is defined in the method #stream using the correct color depending on the state passed.



34
35
36
37
38
39
40
# File 'lib/protest/utils/colorful_output.rb', line 34

def puts(string=nil, state=:normal)
  if string.nil? # calling IO#puts with nil is not the same as with no args
    stream.puts
  else
    stream.puts colorize(string, state)
  end
end