Module: Dye

Extended by:
Dye
Included in:
Dye
Defined in:
lib/dye.rb

Defined Under Namespace

Classes: UnknownSgrCode

Constant Summary collapse

VERSION =
File.read(File.expand_path('../../VERSION', __FILE__)).strip
BASIC_SGR =

Select Graphic Rendition

{
  :clear     => 0,
  :bold      => 1,
  :underline => 4,
  :blinking  => 5,
  :reversed  => 7,

  :black     => 30,
  :red       => 31,
  :green     => 32,
  :yellow    => 33,
  :blue      => 34,
  :magenta   => 35,
  :cyan      => 36,
  :white     => 37,

  :onblack   => 40,
  :onred     => 41,
  :ongreen   => 42,
  :onyellow  => 43,
  :onblue    => 44,
  :onmagenta => 45,
  :oncyan    => 46,
  :onwhite   => 47
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#colorObject Also known as: color?

Returns the value of attribute color.



52
53
54
# File 'lib/dye.rb', line 52

def color
  @color
end

#strict_ansiObject Also known as: strict_ansi?

Returns the value of attribute strict_ansi.



56
57
58
# File 'lib/dye.rb', line 56

def strict_ansi
  @strict_ansi
end

Instance Method Details

#dye(*args) ⇒ Object



59
60
61
# File 'lib/dye.rb', line 59

def dye(*args)
  apply_styles( {}, *args )
end


74
75
76
# File 'lib/dye.rb', line 74

def print_reset_colors
  print sgr(:clear) if color?
end

#sgr(*names) ⇒ Object



68
69
70
71
72
# File 'lib/dye.rb', line 68

def sgr(*names)
  return if names.empty?
  codes = names.map{|n| sgr_to_code(n) }
  strict_ansi? ? "\e[#{codes.join(';')}m" : codes.map{|c| "\e[#{c}m" }.join
end

#strip_ansi(string) ⇒ Object



63
64
65
66
# File 'lib/dye.rb', line 63

def strip_ansi(string)
  return '' if string.nil?
  string.gsub(/\e\[[\d;]+m/, '')
end