Module: Rex::Text::Color

Defined in:
lib/rex/text/color.rb

Overview

This module provides an interface to getting ANSI color codes. It’s taken mostly from perl’s Term::ANSIColor by Russ Allbery <[email protected]> and Zenin <[email protected]>.

Constant Summary collapse

SUPPORTED_FORMAT_CODES =
%w[%cya %red %grn %blu %yel %whi %mag %blk %dred %dgrn %dblu %dyel %dcya %dwhi %dmag %und %bld %clr %bgblu %bgyel %bggrn %bgmag %bgblk %bgred %bgcyn %bgwhi]
AnsiAttributes =
{
  'clear'      => 0,
  'reset'      => 0,
  'bold'       => 1,
  'dark'       => 2,
  'underline'  => 4,
  'underscore' => 4,
  'blink'      => 5,
  'reverse'    => 7,
  'concealed'  => 8,
  'black'      => 30,   'on_black'   => 40,
  'red'        => 31,   'on_red'     => 41,
  'green'      => 32,   'on_green'   => 42,
  'yellow'     => 33,   'on_yellow'  => 43,
  'blue'       => 34,   'on_blue'    => 44,
  'magenta'    => 35,   'on_magenta' => 45,
  'cyan'       => 36,   'on_cyan'    => 46,
  'white'      => 37,   'on_white'   => 47
}

Instance Method Summary collapse

Instance Method Details

#ansi(*attrs) ⇒ Object

Return a string with ANSI codes substituted. Derived from code written by The FaerieMUD Consortium.



40
41
42
43
44
# File 'lib/rex/text/color.rb', line 40

def ansi(*attrs)
  attr = attrs.collect {|a| AnsiAttributes[a] ? AnsiAttributes[a] : nil}.compact.join(';')
  attr = "\e[%sm" % attr if (attr.empty? == false)
  return attr
end

#colorize(*color) ⇒ Object

Colorize if this shell supports it



49
50
51
# File 'lib/rex/text/color.rb', line 49

def colorize(*color)
  supports_color?() ? ansi(*color) : ''
end

#do_colorize(*color) ⇒ Object

Colorize if this shell supports it



102
103
104
# File 'lib/rex/text/color.rb', line 102

def do_colorize(*color)
  supports_color?() ? ansi(*color) : ''
end

#reset_colorObject

Resets coloring so that it’s back to normal.



94
95
96
97
# File 'lib/rex/text/color.rb', line 94

def reset_color
  return if not supports_color?
  print(colorize('clear'))
end

#substitute_colors(msg, in_prompt = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rex/text/color.rb', line 53

def substitute_colors(msg, in_prompt = nil)
  str = msg.dup
  pre_color = post_color = ''
  if (in_prompt && supports_color?)
    pre_color = "\x01"  # RL_PROMPT_START_IGNORE
    post_color = "\x02" # RL_PROMPT_END_IGNORE
  end
  str.gsub!(/%cya/, pre_color+colorize('cyan')+post_color)
  str.gsub!(/%red/, pre_color+colorize('red')+post_color)
  str.gsub!(/%grn/, pre_color+colorize('green')+post_color)
  str.gsub!(/%blu/, pre_color+colorize('blue')+post_color)
  str.gsub!(/%yel/, pre_color+colorize('yellow')+post_color)
  str.gsub!(/%whi/, pre_color+colorize('white')+post_color)
  str.gsub!(/%mag/, pre_color+colorize('magenta')+post_color)
  str.gsub!(/%blk/, pre_color+colorize('black')+post_color)
  str.gsub!(/%dred/, pre_color+colorize('dark', 'red')+post_color)
  str.gsub!(/%dgrn/, pre_color+colorize('dark', 'green')+post_color)
  str.gsub!(/%dblu/, pre_color+colorize('dark', 'blue')+post_color)
  str.gsub!(/%dyel/, pre_color+colorize('dark', 'yellow')+post_color)
  str.gsub!(/%dcya/, pre_color+colorize('dark', 'cyan')+post_color)
  str.gsub!(/%dwhi/, pre_color+colorize('dark', 'white')+post_color)
  str.gsub!(/%dmag/, pre_color+colorize('dark', 'magenta')+post_color)
  str.gsub!(/%und/, pre_color+colorize('underline')+post_color)
  str.gsub!(/%bld/, pre_color+colorize('bold')+post_color)
  str.gsub!(/%clr/, pre_color+colorize('clear')+post_color)
  # Background Color
  str.gsub!(/%bgblu/, pre_color+colorize('on_blue')+post_color)
  str.gsub!(/%bgyel/, pre_color+colorize('on_yellow')+post_color)
  str.gsub!(/%bggrn/, pre_color+colorize('on_green')+post_color)
  str.gsub!(/%bgmag/, pre_color+colorize('on_magenta')+post_color)
  str.gsub!(/%bgblk/, pre_color+colorize('on_black')+post_color)
  str.gsub!(/%bgred/, pre_color+colorize('on_red')+post_color)
  str.gsub!(/%bgcyn/, pre_color+colorize('on_cyan')+post_color)
  str.gsub!(/%bgwhi/, pre_color+colorize('on_white')+post_color)

  str
end