module XCPerfect::ANSI

ANSI applies color & style to the output to make it look more appealing to the eye

Constants

COLORS
EFFECT
FORMATTED_MATCHER

Attributes

colorize[RW]

Public Instance Methods

ansi_parse(text, color, effect=nil) click to toggle source
# File lib/xcperfect/ansi.rb, line 78
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) click to toggle source
# File lib/xcperfect/ansi.rb, line 63
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
color_for(percentage) click to toggle source
# File lib/xcperfect/ansi.rb, line 30
def color_for(percentage)
  case (percentage * 100).round
  when 0..33
    ->(x) { red(x) }
  when 33...66
    ->(x) { yellow(x) }
  when 66..100
    ->(x) { green(x) }
  else
    raise "Percentage of #{percentage * 100} is not possible"
  end
end
colorize?() click to toggle source
# File lib/xcperfect/ansi.rb, line 26
def colorize?
  !!@colorize
end
cyan(text) click to toggle source
# File lib/xcperfect/ansi.rb, line 55
def cyan(text)
  ansi_parse(text, :cyan)
end
green(text) click to toggle source
# File lib/xcperfect/ansi.rb, line 51
def green(text)
  ansi_parse(text, :green, :bold)
end
red(text) click to toggle source
# File lib/xcperfect/ansi.rb, line 47
def red(text)
  ansi_parse(text, :red)
end
strip(text) click to toggle source
# File lib/xcperfect/ansi.rb, line 74
def strip(text)
  text =~ FORMATTED_MATCHER ? $3 : text
end
white(text) click to toggle source
# File lib/xcperfect/ansi.rb, line 43
def white(text)
  ansi_parse(text, :plain, :bold)
end
yellow(text) click to toggle source
# File lib/xcperfect/ansi.rb, line 59
def yellow(text)
  ansi_parse(text, :yellow)
end