[Eug-lug] req 4 help |walter

Bob Miller kbob at jogger-egg.com
Sat Jan 1 15:48:35 PST 2005


walter fry wrote:

> Recently I ventured  into some file which contained text in various colors. 
> I would like help finding the file which defines the meaning of the 
> different colors or is it colours ? I have tried various combinations of   
> man| grep | ls -lh |colors to no avail

Here is a Python script that will display all combinations of
foreground and background color, reverse and boldface.

You can modify it to also display blinking, underscored, or concealed
text.

#!/usr/bin/python

class Color:

    def __init__(self, name, number):
        self.name = name
        self.number = number

    def foreground(self):
        return self.number;

    def background(self):
        return self.number + 10

black   = Color('black',   30)
red     = Color('red',     31)
green   = Color('green',   32)
yellow  = Color('yellow',  33)
blue    = Color('blue',    34)
magenta = Color('magenta', 35)
cyan    = Color('cyan',    36)
white   = Color('white',   37)

colors = black, red, green, yellow, blue, magenta, cyan, white

class Attr:

    def __init__(self, name, number):
        self.name = name
        self.number = number

none       = Attr('none',       0)
bold       = Attr('bold',       1)
underscore = Attr('underscore', 4)
blink      = Attr('blink',      5)
reverse    = Attr('reverse',    7)
concealed  = Attr('concealed',  8)

attrs = (none, bold, underscore, blink, reverse, concealed)

def start_mode(fg, bg=None, *attrs):
    modes = []
    if fg:
        modes.append(fg.foreground())
    if bg:
        modes.append(bg.background())
    modes.extend([a.number for a in attrs])
    return '\33[%sm' % ';'.join([str(m) for m in modes])

def default_mode():
    return '\33[0m'

def colorize(text, fg, bg=None, *attrs):
    return start_mode(fg, bg, *attrs) + ('%-8s' % text) + default_mode()

for attrset in ((), (bold,), (reverse,), (reverse, bold)):
    print ' '.join([a.name for a in attrset])
    for fg in colors:
        print colorize(fg.name, fg),
    print
    for bg in colors:
        print colorize('none', None, bg),
    print
    for fg in colors:
        for bg in colors:
            print colorize(fg.name, fg, bg, *attrset),
        print
    print

-- 
Bob Miller                              K<bob>
kbobsoft software consulting
http://kbobsoft.com                     kbob at jogger-egg.com


More information about the EUGLUG mailing list