Module: ANSIColor

Defined in:
lib/mega/ansicolor.rb

Overview

:title: ANSIColor

Module which makes it very easy to use ANSI codes. These are esspecially nice for beautifying shell output.

Synopsis

require 'mega/ansicolor'

include ANSIColor

p red, "Hello", blue, "World"   
=> "\e[31mHello\e[34mWorld"

p red { "Hello" } + blue { "World" }
=> "\e[31mHello\e[0m\e[34mWorld\e[0m"

Supported ANSI Commands

clear
reset           # synonym for :clear
bold
dark
italic          # not widely implemented
underline
underscore      # synonym for :underline
blink
rapid_blink     # not widely implemented
negative        # no reverse because of String#reverse
concealed
strikethrough   # not widely implemented
black
red
green
yellow
blue
magenta
cyan
white
on_black
on_red
on_green
on_yellow
on_blue
on_magenta
on_cyan
on_white

Author(s)

  • Florian Frank <flori#ping.de>

Constant Summary collapse

ColoredRegexp =
/\e\[([34][0-7]|[0-9])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 ], 
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attributesObject



156
157
158
# File 'lib/mega/ansicolor.rb', line 156

def attributes
  @@attributes.map { |c| c[0] }
end

Instance Method Details

#uncolored(string = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/mega/ansicolor.rb', line 142

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