Module: IStats::Color
Overview
Color collects some methods for colorizing terminal output. Thanks to github.com/holman
Constant Summary collapse
- CODES =
{ :reset => "\e[0m", :green => "\e[32m", :light_yellow => "\e[93m", :yellow => "\e[33m", :red => "\e[31m", :flash_red => "\e[31;5m" }
Class Method Summary collapse
-
.included(other) ⇒ Object
Tries to enable Windows support if on that platform.
Instance Method Summary collapse
-
#colorize(string, color_code) ⇒ Object
Wraps the given string in ANSI color codes.
Class Method Details
.included(other) ⇒ Object
Tries to enable Windows support if on that platform.
Returns nothing.
20 21 22 23 24 25 26 |
# File 'lib/iStats/color.rb', line 20 def self.included(other) if RUBY_PLATFORM =~ /win32/ || RUBY_PLATFORM =~ /mingw32/ require 'Win32/Console/ANSI' end rescue LoadError # Oh well, we tried. end |
Instance Method Details
#colorize(string, color_code) ⇒ Object
Wraps the given string in ANSI color codes
string - The String to wrap. color_code - The String representing he ANSI color code
Examples
colorize("Boom!", :magenta)
# => "\e[35mBoom!\e[0m"
Returns the wrapped String unless the the platform is windows and does not have Win32::Console, in which case, returns the String.
40 41 42 43 44 45 46 47 |
# File 'lib/iStats/color.rb', line 40 def colorize(string, color_code) if !defined?(Win32::Console) && !!(RUBY_PLATFORM =~ /win32/ || RUBY_PLATFORM =~ /mingw32/) # looks like this person doesn't have Win32::Console and is on windows # just return the uncolorized string return string end "#{CODES[color_code] || color_code}#{string}#{CODES[:reset]}" end |