Module: Regtest::Colors

Included in:
Regtest
Defined in:
lib/regtest/colors.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.mappingObject

Color mapping Examples:

Regtest::Colors.mapping[:filename] = :lightblue
Regtest::Colors.mapping[:fail] = :@red # '@' prefix means background color
Regtest::Colors.mapping[:statistics] = %i(cyan italic) # more than one color code is possible


23
24
25
# File 'lib/regtest/colors.rb', line 23

def mapping
  @mapping
end

Class Method Details

.apply(str, *codes) ⇒ Object

Apply color codes to a string



26
27
28
29
30
31
32
33
# File 'lib/regtest/colors.rb', line 26

def apply str, *codes
  codes.flatten.each do |c|
    num = @codes[c.to_sym]
    fail format('Code %s not supported.', c) unless num
    str = format("\e[%dm%s", num, str)
  end
  format("%s\e[0m", str)
end

.codesObject

Get available color codes



36
37
38
# File 'lib/regtest/colors.rb', line 36

def codes
  @codes.keys
end

Instance Method Details

#report(*args, type: nil) ⇒ Object

Redefine Regtest.report



9
10
11
12
13
14
15
# File 'lib/regtest/colors.rb', line 9

def report *args, type: nil
  color = Colors.mapping[type]
  if color && $stdout.tty?
    args = args.map {|a| Colors.apply(a.to_s, color)}
  end
  puts *args
end