Module: Term::ANSIColor

Extended by:
ANSIColor
Included in:
Fhlow::Pen, ANSIColor
Defined in:
lib/module_term/ansicolor.rb

Constant Summary collapse

COLORED_REGEXP =

Regular expression that is used to scan for ANSI-sequences. It is used to uncolor strins.

/\e\[([34][0-7]|[0-9]|01;3[1-6])m/
@@attributes =
[
  [ :clear        ,   0 ], 
  [ :reset        ,   0 ],     # synonym for :clear
  [ :bold         ,   1 ], 
  [ :dark         ,   2 ], 
  [ :italic       ,   3 ],     # not widely implemented
  [ :underline    ,   4 ], 
  [ :underscore   ,   4 ],     # synonym for :underline
  [ :blink        ,   5 ], 
  [ :rapid_blink  ,   6 ],     # not widely implemented
  [ :negative     ,   7 ],     # no reverse because of String#reverse
  [ :concealed    ,   8 ], 
  [ :strikethrough,   9 ],     # not widely implemented
  [ :black        ,  30 ], 
  [ :red          ,  31 ], 
  [ :green        ,  32 ], 
  [ :yellow       ,  33 ], 
  [ :blue         ,  34 ], 
  [ :magenta      ,  35 ], 
  [ :cyan         ,  36 ], 
  [ :white        ,  37 ], 
  [ :on_black     ,  40 ], 
  [ :on_red       ,  41 ], 
  [ :on_green     ,  42 ], 
  [ :on_yellow    ,  43 ], 
  [ :on_blue      ,  44 ], 
  [ :on_magenta   ,  45 ], 
  [ :on_cyan      ,  46 ], 
  [ :on_white     ,  47 ], 
  [ :lightred     , "01;31"],
  [ :lightgreen   , "01;32"],
  [ :lightyellow  , "01;33"],
  [ :lightblue    , "01;34"],
  [ :lightmagenta , "01;35"],
  [ :lightcyan    , "01;36"],
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attributesObject

Returns an array of all Term::ANSIColor attributes as symbols.



96
97
98
# File 'lib/module_term/ansicolor.rb', line 96

def attributes
  @@attributes.map { |c| c.first }
end

.coloring=(val) ⇒ Object

Turns the coloring on or off globally, so you can easily do this for example:

Term::ANSIColor::coloring = STDOUT.isatty


50
51
52
# File 'lib/module_term/ansicolor.rb', line 50

def self.coloring=(val)
  @@coloring = val
end

.coloring?Boolean

Returns true, if the coloring function of this module is switched on, false otherwise.

Returns:

  • (Boolean)


43
44
45
# File 'lib/module_term/ansicolor.rb', line 43

def self.coloring?
  @@coloring
end

Instance Method Details

#uncolored(string = nil) ⇒ Object

Returns an uncolored version of the string, that is all ANSI-sequences are stripped from the string.



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/module_term/ansicolor.rb', line 81

def uncolored(string = nil) # :yields:
  if block_given?
    yield.gsub(COLORED_REGEXP, '')
  elsif string
    string.gsub(COLORED_REGEXP, '')
  elsif respond_to?(:to_str)
    gsub(COLORED_REGEXP, '')
  else
    ''
  end
end