Module: XCPretty::ANSI

Included in:
Printer
Defined in:
lib/xcpretty/ansi.rb

Constant Summary collapse

FORMATTED_MATCHER =
%r{\e\[(\d+)[;]?(\d+)?m(.*)\e\[0m}
EFFECT =
{
  :reset     => '0',
  :bold      => '1',
  :underline => '4'
}
COLORS =
{
  :black  => '30',
  :red    => '31',
  :green  => '32',
  :yellow => '33',
  :blue   => '34',
  :cyan   => '36',
  :white  => '37',
  :plain  => '39'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#colorizeObject

Returns the value of attribute colorize.



3
4
5
# File 'lib/xcpretty/ansi.rb', line 3

def colorize
  @colorize
end

Instance Method Details

#ansi_parse(text, color, effect = nil) ⇒ Object



63
64
65
66
67
68
# File 'lib/xcpretty/ansi.rb', line 63

def ansi_parse(text, color, effect=nil)
  return text unless colorize?
  colors_code = COLORS[color]  || ''
  effect_code = EFFECT[effect] ? ';' + EFFECT[effect] : ''
  "\e[#{colors_code}#{effect_code}m#{text}\e[#{EFFECT[:reset]}m"
end

#applied_effects(text) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/xcpretty/ansi.rb', line 48

def applied_effects(text)
  effects = []
  if text =~ FORMATTED_MATCHER
    colors = COLORS.invert[$1]
    effect = EFFECT.invert[$2]
    effects << colors if colors
    effects << effect if effect
  end
  effects
end

#colorize?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/xcpretty/ansi.rb', line 24

def colorize?
  !!@colorize
end

#cyan(text) ⇒ Object



40
41
42
# File 'lib/xcpretty/ansi.rb', line 40

def cyan(text)
  ansi_parse(text, :cyan)
end

#green(text) ⇒ Object



36
37
38
# File 'lib/xcpretty/ansi.rb', line 36

def green(text)
  ansi_parse(text, :green, :bold)
end

#red(text) ⇒ Object



32
33
34
# File 'lib/xcpretty/ansi.rb', line 32

def red(text)
  ansi_parse(text, :red)
end

#strip(text) ⇒ Object



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

def strip(text)
  text =~ FORMATTED_MATCHER ? $3 : text
end

#white(text) ⇒ Object



28
29
30
# File 'lib/xcpretty/ansi.rb', line 28

def white(text)
  ansi_parse(text, :plain, :bold)
end

#yellow(text) ⇒ Object



44
45
46
# File 'lib/xcpretty/ansi.rb', line 44

def yellow(text)
  ansi_parse(text, :yellow)
end