summaryrefslogtreecommitdiff
path: root/tools/colorize
blob: ce6f410a4e584c105879c079c90349adccffa219 (plain) (blame)
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
#!/usr/bin/env python

from __future__ import print_function

import os
import sys

def color(i):
    if 0 <= i < 8:
        return '\x1b[%dm' % (30 + i)
    if 8 <= i < 16:
        return '\x1b[%dm' % (90 + (i - 8))

def main(argv):
    # can't change buffering on sys.stdout after creation using python APIs
    # so do our own buffering
    buffer = []

    colors = {}
    while argv:
        arg0 = argv[0]
        del argv[0]
        if arg0 == ':':
            break
        if ':' not in arg0:
            continue
        c, w = arg0.split(':', 1)
        colors[w] = color(int(c))
    if 1:
        arg = argv[0]
        c = colors.get('', '')
        e = c and '\x1b[m'
        buffer.extend([c, arg, e])
    for arg in argv[1:]:
        c = colors.get(arg, '')
        e = c and '\x1b[m'
        buffer.extend([' ', c, arg, e])
    buffer.append('\n')
    sys.stdout.write(''.join(buffer))
    sys.stdout.flush()
    os.execvp(argv[0], argv)

if __name__ == '__main__':
    main(sys.argv[:])