Skip to content

Instantly share code, notes, and snippets.

@mkhl
Created February 23, 2011 21:35
Show Gist options
  • Save mkhl/841231 to your computer and use it in GitHub Desktop.
Save mkhl/841231 to your computer and use it in GitHub Desktop.
Generate ANSI escape sequences.
#!/usr/bin/env ruby -w
# Usage: ansicolor attr... [-- string...]
# Generate ANSI escape sequences.
# With string arguments, print them on stdout,
# followed by a `clear' escape sequence.
begin
require 'term/ansicolor'
rescue LoadError
require 'rubygems'
require 'term/ansicolor'
end
def run(attrs, echos = nil)
begin
out = attrs.collect { |a| Term::ANSIColor.send a.to_sym }
out << echos.join(" ") << Term::ANSIColor.clear << "\n" if echos
print out
rescue NoMethodError => err
puts "#{File.basename($PROGRAM_NAME)}: undefined attribute '#{err.name}'"
end
end
idx = ARGV.index '--'
if idx
ARGV.delete_at idx
run(ARGV[0...idx], ARGV[idx..-1])
else
run(ARGV.to_a)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment