Class: RMTools::Painter

Inherits:
Object show all
Defined in:
lib/rmtools/console/coloring.rb

Constant Summary collapse

BOLD =
1
UNDERLINE =
4
GRAYBG =
5
BOLDBG =
7
KEY =
BLACK = 30
RED =
31
GREEN =
32
YELLOW =
33
BLUE =
34
PURPLE =
35
CYAN =
36
GRAY =
37
Colors =
{:black => 30, :red => 31, :green => 32, :yellow => 33, :blue => 34, :purple => 35, :cyan => 36, :gray => 37, :pink => [35, 1], :violet => [35, 1],
  :k => 30, :r => 31, :g => 32, :y => 33, :b => 34, :p => 35, :c => 36, :w => [37, 1], :v => [35, 1]
}.unify_keys
Effects =
{:bold => 1, :underline => 4, :graybg => 5, :boldbg => 7,
    :b => 1, :u => 4, :gbg => 5, :bbg => 7
}.unify_keys

Class Method Summary collapse

Class Method Details

.clean(str) ⇒ Object



83
84
85
# File 'lib/rmtools/console/coloring.rb', line 83

def clean str
  str.gsub(/\e\[[\d;]*m/, '')
end

.demo(str, pattern = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/rmtools/console/coloring.rb', line 34

def demo(str, pattern=nil)
  %w[black red green yellow blue purple cyan gray].product(%w[bold underline graybg boldbg]).each {|color, effect|
    if pattern
      puts ghl(str, pattern, "#{color}_#{effect}")
    else
      puts paint(str, transparent, color, effect)
    end
  }
end

.method_missing(m, str, transparent = false) ⇒ Object

Without transparent Painter stops coloring once it find colored substring

puts "words have one #{Painter.red_bold 'highlighted'} among them"

<default>words have one <red>highlighted</red> among them</default>

puts Painter.gray "words have one #{Painter.red_bold 'highlighted'} among them"

<gray>words have one </gray><red>highlighted</red><default> among them</default>

puts Painter.gray "words have one #{Painter.red_bold 'highlighted'} among them", true

<gray>words have one <red>highlighted</red> among them</gray>

Actually, transparent coloring is slower



79
80
81
# File 'lib/rmtools/console/coloring.rb', line 79

def method_missing(m, str, transparent=false)
  paint str, transparent, *(m.to_s/"_").bs
end

.paint(str, transparent, num = nil, effect = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rmtools/console/coloring.rb', line 44

def paint(str, transparent, num=nil, effect=nil)
  # default cmd.exe cannot into ANSI
  str = str.to_s
  return str if ENV['ComSpec'] =~ /cmd(.exe)?$/
  if num.is String
    num = Colors[num]
    if !num
      effect = Effects[num]
    elsif num.is Array
      num, effect = num
    end
  end
  effect = Effects[effect] if effect.is String
  if num and effect
    str = str.gsub("\e[m", "\e[m\e[#{effect};#{num}m") if transparent
    "\e[#{effect};#{num}m#{str}\e[m"
  elsif effect
    str = str.gsub("\e[m", "\e[m\e[#{effect}m") if transparent
    "\e[#{effect}m#{str}\e[m"
  elsif num
    str = str.gsub("\e[m", "\e[m\e[#{num}m") if transparent
    "\e[#{num}m#{str}\e[m"
  else str    
  end 
end