Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/incolor.rb

Overview

incolor.rb - Library for colorizing console output

This file is part of the incolor project Copyright © 2010 Pavel Nazarov

Constant Summary collapse

@@colors =
Hash[
  'reset'  => '0',
  'bold'   => '1',
  'ul'   => '4',
  'frame'  => '7',
  'black'  => '30',
  'red'    => '31',
  'green'  => '32',
  'yellow' => '33',
  'blue'   => '34',
  'pink'   => '35',
  'cyan' => '36',
  'white'  => '37',
]

Instance Method Summary collapse

Instance Method Details

#colorize(string) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/incolor.rb', line 30

def colorize(string)
  string.scan(/<.+?>.+?\/>/) do |s|
    color = s.scan(/<[a-z]+>/)[0][1..-2]
    text = s.scan(/>.+\//)[0][1..-2]
    code = "\e[#{@@colors[color]}m"
    std = "\e[#{@@colors['reset']}m"

    string = string.sub(s, "#{code}#{text}#{std}")
  end

  return string
end

#incolorObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/incolor.rb', line 43

def incolor
  string = self
  until string.tags? == false
    string = colorize(string)
  end

  begin
    printf string
  rescue
    puts string
  end
end

#tags?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/incolor.rb', line 23

def tags?
  if self =~ /<.+?>.+?\/>/
    return true
  else return false
  end
end