module Imgurr::Color

Color collects some methods for colorizing terminal output. Thanks to github.com/holman

Constants

CODES

Public Class Methods

included(other) click to toggle source

Tries to enable Windows support if on that platform.

Returns nothing.

# File lib/imgurr/color.rb, line 19
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/imgurr/color.rb, line 39
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