Skip to content

Instantly share code, notes, and snippets.

@stevecj stevecj/colortest
Created Jul 29, 2014

Embed
What would you like to do?
ANSI color test written in Ruby
#!/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
You can’t perform that action at this time.