Skip to content

Instantly share code, notes, and snippets.

@stevecj
Created July 29, 2014 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevecj/85ff3d92b6919d76d934 to your computer and use it in GitHub Desktop.
Save stevecj/85ff3d92b6919d76d934 to your computer and use it in GitHub Desktop.
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