A collection of ANSI color utility functions
Set some ANSI escape code constants (Shamelessly stolen from Perl's Term::ANSIColor by Russ Allbery rra@stanford.edu and Zenin zenin@best.com
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
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