Debian Codegraph @

testenv/misc/colour_terminal.py

raw
1 from functools import partial
2 import platform
3 from os import getenv
4 import sys
5
6 """ This module allows printing coloured output to the terminal when running a
7 Wget Test under certain conditions.
8 The output is coloured only on Linux systems. This is because coloured output
9 in the terminal on Windows requires too much effort for what is simply a
10 convenience. This might work on OSX terminals, but without a confirmation, it
11 remains unsupported.
12
13 Another important aspect is that the coloured output is printed only if the
14 environment variable MAKE_CHECK is not set. This variable is set when running
15 the test suite through, `make check`. In that case, the output is not only
16 printed to the terminal but also copied to a log file where the ANSI escape
17 codes on;y add clutter. """
18
19
20 T_COLORS = {
21 'PURPLE': '\033[95m',
22 'BLUE': '\033[94m',
23 'GREEN': '\033[92m',
24 'YELLOW': '\033[93m',
25 'RED': '\033[91m',
26 'ENDC': '\033[0m'
27 }
28
29 system = True if platform.system() in ('Linux', 'Darwin') else False
30 check = False if getenv("MAKE_CHECK") == 'True' else True
31
32
33 def printer(color, string):
34 if sys.stdout.isatty() and system and check:
35 print(T_COLORS.get(color) + string + T_COLORS.get('ENDC'))
36 else:
37 print(string)
38
39
40 print_blue = partial(printer, 'BLUE')
41 print_red = partial(printer, 'RED')
42 print_green = partial(printer, 'GREEN')
43 print_purple = partial(printer, 'PURPLE')
44 print_yellow = partial(printer, 'YELLOW')
45
46 # vim: set ts=8 sw=3 tw=80 et :