Module: Boom::Color

Extended by:
Color
Included in:
Color, Command, Remote, Storage::Json, Storage::Redis
Defined in:
lib/kaboom/color.rb

Overview

Color collects some methods for colorizing terminal output.

Constant Summary collapse

CODES =
{
  :reset   => "\e[0m",

  :cyan    => "\e[36m",
  :magenta => "\e[35m",
  :red     => "\e[31m",
  :yellow  => "\e[33m"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other) ⇒ Object

Tries to enable Windows support if on that platform.

Returns nothing.



18
19
20
21
22
23
24
# File 'lib/kaboom/color.rb', line 18

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.



38
39
40
41
42
43
44
45
# File 'lib/kaboom/color.rb', line 38

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