module Cucumber::Term::ANSIColor
This module allows to colorize text using ANSI escape sequences.
Include the module in your class and use its methods to colorize text.
Example:
require 'cucumber/term/ansicolor' class MyFormatter include Cucumber::Term::ANSIColor def initialize(config) $stdout.puts yellow("Initializing formatter") $stdout.puts green("Coloring is active \o/") if Cucumber::Term::ANSIColor.coloring? $stdout.puts grey("Feature path:") + blue(bold(config.feature_dirs)) end end
To see what colours and effects are available, just run this in your shell:
ruby -e "require 'rubygems'; require 'cucumber/term/ansicolor'; puts Cucumber::Term::ANSIColor.attributes"
Constants
- COLORED_REGEXP
-
Regular expression that is used to scan for ANSI-sequences while uncoloring strings.
Attributes
Turns the coloring on or off globally, so you can easily do this for example:
Cucumber::Term::ANSIColor::coloring = $stdout.isatty
Turns the coloring on or off globally, so you can easily do this for example:
Cucumber::Term::ANSIColor::coloring = $stdout.isatty
Public Class Methods
Source
# File lib/cucumber/term/ansicolor.rb, line 80 def included(klass) return unless klass == String ATTRIBUTES.delete(:clear) ATTRIBUTE_NAMES.delete(:clear) end
Public Instance Methods
Source
# File lib/cucumber/term/ansicolor.rb, line 117 def attributes ATTRIBUTE_NAMES end
Returns an array of all Cucumber::Term::ANSIColor attributes as symbols.
Source
# File lib/cucumber/term/ansicolor.rb, line 104 def uncolored(text = nil) if block_given? uncolorize(yield) elsif text uncolorize(text) elsif respond_to?(:to_str) uncolorize(to_str) else '' end end
Returns an uncolored version of the string ANSI-sequences are stripped from the string.
Private Instance Methods
Source
# File lib/cucumber/term/ansicolor.rb, line 123 def colorize(text, color_code) return String.new(text || '') unless Cucumber::Term::ANSIColor.coloring? return "\e[#{color_code}m" unless text "\e[#{color_code}m#{text}\e[0m" end
Source
# File lib/cucumber/term/ansicolor.rb, line 130 def uncolorize(string) string.gsub(COLORED_REGEXP, '') end