Module: IParty::CLI::Colorize

Included in:
Application, Formatter
Defined in:
lib/iparty/cli/colorize.rb

Defined Under Namespace

Classes: UnknownColorError

Constant Summary collapse

COLORMAP =
{
  black: 30,
  gray: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37,
}.freeze

Instance Method Summary collapse

Instance Method Details

#colorize(str, color = :yellow) ⇒ Object Also known as: c



20
21
22
23
# File 'lib/iparty/cli/colorize.rb', line 20

def colorize str, color = :yellow
  ccode = COLORMAP[color.to_sym] || raise(UnknownColorError, "unknown color `#{color}'")
  @opts[:colorize] ? "\e[#{ccode}m#{str}\e[0m" : str.to_s
end

#decolorize(str) ⇒ Object



26
27
28
# File 'lib/iparty/cli/colorize.rb', line 26

def decolorize str
  str.to_s.gsub(/\e\[.*?(\d)+m/, "")
end

#with_color(*args) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/iparty/cli/colorize.rb', line 30

def with_color *args
  color_was = @opts[:colorize]
  @opts[:colorize] = args.fetch(0, true)
  yield
ensure
  @opts[:colorize] = color_was
end