module Treequel::ANSIColorUtilities

A collection of ANSI color utility functions

Constants

ANSI_ATTRIBUTES

Set some ANSI escape code constants (Shamelessly stolen from Perl's Term::ANSIColor by Russ Allbery <rra@stanford.edu> and Zenin <zenin@best.com>

Public Instance Methods

ansi_code( *attributes ) click to toggle source

Create a string that contains the ANSI codes specified and return it

# File lib/treequel/mixins.rb, line 399
def ansi_code( *attributes )
        attributes.flatten!
        attributes.collect! {|at| at.to_s }
        return '' unless /(?:vt10[03]|xterm(?:-color)?|linux|screen)/ =~ ENV['TERM']
        attributes = ANSI_ATTRIBUTES.values_at( *attributes ).compact.join(';')

        if attributes.empty?
                return ''
        else
                return "\e[%sm" % attributes
        end
end
colorize( *args ) { || ... } click to toggle source

Colorize the given string with the specified attributes and return it, handling line-endings, color reset, etc.

# File lib/treequel/mixins.rb, line 415
def colorize( *args )
        string = ''

        if block_given?
                string = yield
        else
                string = args.shift
        end

        ending = string[/(\s)$/] || ''
        string = string.rstrip

        return ansi_code( args.flatten ) + string + ansi_code( 'reset' ) + ending
end