ActiveState Powered by ActiveState

Recipe 574451: Handling ANSI Terminal.


This function can be used, with or instead of print, to get ANSI output.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'''
    Set ANSI Terminal Color and Attributes.
'''
from sys import stdout

esc = '\033[1;'
reset = '%sm' % esc
format = '%dm'
fgoffset, bgoffset = 30, 40
for k, v in dict(
    attrs = 'none bold faint italic underline blink fast reverse concealed',
    colors = 'grey red green yellow blue magenta cyan white'
).items(): globals()[k]=dict((s,i) for i,s in enumerate(v.split()))

def term(arg=None, sep=' ', end='\n'):
    '''
        "arg" is a string or None
        if "arg" is None : the terminal is reset to his default values.
        if "arg" is a string it must contain "sep" separated values.
        if args are found in globals "attrs" or "colors" \
    they are interpreted as ANSI commands else they are output as text.
        colors, if any, must be first (foreground first then background)
        you can not specify a background color alone ; \
    if you specify only one color, it will be the foreground one.
        examples:
    term('red')                  : set red as the foreground color
    term('red blue')             : red on blue
    term('red blink')            : blinking red
    term()                       : restore terminal default values
    term('reverse')              : swap default colors
    term('cyan blue reverse')    : blue on cyan <=> term('blue cyan)
    term('red reverse')          : a way to set up the background only
    term('red reverse blink')    : you can specify \
                                any combinaison of attributes \
                                in any order with or without colors
    term('blink Python')         : output a blinking 'Python'
    '''
    cmd, txt = [reset], []
    if arg:
        arglist=arg.split(sep)
        for offset in (fgoffset, bgoffset):
            if arglist and arglist[0] in colors:
                cmd.append(format % (colors[arglist.pop(0)]+offset))
        for a in arglist:
            c=format % attrs[a] if a in attrs else None
            if c and c not in cmd:
                cmd.append(c)
            else:
                txt.append(a)
    if txt and end: txt[-1]+=end
    stdout.write(esc.join(cmd)+sep.join(txt))

if __name__ == '__main__':

    print
    term('reverse blink')
    print 'reverse blink  default colors'
    term('red')
    print 'red'
    term('red blue')
    print 'red blue'
    term('yellow blink')
    print 'yellow blink'
    term('default')
    term('cyan blue cyan blue')
    term('cyan blue reverse cyan blue reverse')
    term('blue cyan blue cyan')
    term('red reverse red reverse')
    term('yellow red yellow on red 1')
    term('yellow,red,yellow on red 2', sep=',')
    print 'yellow on red 3'
    print
    for bg in colors:
        term(bg.title().center(8), sep='.', end='')
        for fg in colors:
            att=[fg, bg]
            if fg==bg: att.append('blink')
            att.append(fg.center(8))
            term(','.join(att), sep=',', end='')
        print
    print
    for att in attrs:
        term('%s,%s' % (att, att.title().center(10)), sep=',', end='')
    print
    term()
    print

Discussion

Note that most terminals don't handle most of the attributes, the colors seem to be well supported though.
You can run this module to find out what is supported by your terminal.

Sign in to comment