module Shop::Color
Color
collects some methods for colorizing terminal output.
Constants
- CODES
Public Class Methods
included(other)
click to toggle source
Tries to enable Windows support if on that platform.
Returns nothing.
# File lib/shop/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
Public Instance Methods
colorize(string, color_code)
click to toggle source
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.
# File lib/shop/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