Module: XCPretty::ANSI

Included in:
Formatter
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.



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

def colorize
  @colorize
end

Instance Method Details

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



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

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



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

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)


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

def colorize?
  !!@colorize
end

#cyan(text) ⇒ Object



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

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

#green(text) ⇒ Object



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

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

#red(text) ⇒ Object



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

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

#strip(text) ⇒ Object



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

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

#white(text) ⇒ Object



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

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

#yellow(text) ⇒ Object



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

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