Created
July 29, 2014 16:50
-
-
Save stevecj/85ff3d92b6919d76d934 to your computer and use it in GitHub Desktop.
ANSI color test written in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
ColorNameSequence = %w[ | |
Black | |
Red | |
Green | |
Yellow | |
Blue | |
Magenta | |
Cyan | |
White | |
] | |
ColorDef = Struct.new( :name, :value ) | |
ForegroundColorSequence = | |
[ | |
ColorDef.new( 'Default', nil ), | |
ColorDef.new( '~Default', 7 ), | |
] + | |
ColorNameSequence.each_with_index.map{ |name, color_num| | |
ColorDef.new( name, 30 + color_num ) | |
} | |
BackgroundColorSequence = | |
[ | |
ColorDef.new( 'Default', nil ), | |
ColorDef.new( '~Default', 7 ), | |
] + | |
ColorNameSequence.each_with_index.map{ |name, color_num| | |
ColorDef.new( name, 40 + color_num ) | |
} | |
AllAttributesOff = 0 | |
BoldValue = 1 | |
ItalicValue = 3 | |
ReverseOnValue = 7 | |
# Build an ANSI graphics mode escape sequence from array of attribute value numbers. | |
def gm(*values) | |
escape_seq = | |
"\x1b" << | |
'[' << | |
( values.compact.uniq * ';' ) << | |
'm' | |
end | |
puts | |
puts ' Foreground: ' << ForegroundColorSequence.map{ |c| '%-10s' % c.name } * ' ' | |
puts | |
puts ' ' << ( '+----------' * ForegroundColorSequence.length ) | |
BackgroundColorSequence.each do |bgc| | |
content = ForegroundColorSequence.map{ |fgc| gm( bgc.value, fgc.value ) << 'gYw'.center(10, ' ') << gm( AllAttributesOff ) } * ' ' | |
puts ( '%12s |' % "" ) << content | |
puts ( '%12s |' % "On #{bgc.name}:" ) << content | |
content = ForegroundColorSequence.map{ |fgc| gm( bgc.value, fgc.value, BoldValue ) << 'gYw'.center(10, ' ') << gm( AllAttributesOff ) } * ' ' | |
puts ( '%12s |' % "" ) << content | |
puts ( '%12s |' % "" ) << content | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment