Treequel::

ANSIColorUtilities

module

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

anchor
ansi_code( *attributes )

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

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

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

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

# File lib/treequel/mixins.rb, line 361
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