about summary refs log tree commit diff stats
path: root/test/utils/cosmetics.py
blob: e80e1f09e6274d175d3d572888d37a3b220337a0 (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
45
46
47
48
49
50
51
52
53
54
55
import os
import platform

is_win = platform.system() == "Windows"

def getTerminalSize():
    "Return the size of the terminal : COLUMNS, LINES"

    env = os.environ

    def ioctl_GWINSZ(fd):
        try:
            import fcntl
            import termios
            import struct
            import os
            cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
                                                 '1234'))
        except:
            return
        return cr
    cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
    if not cr:
        try:
            fd = os.open(os.ctermid(), os.O_RDONLY)
            cr = ioctl_GWINSZ(fd)
            os.close(fd)
        except:
            pass
    if not cr:
        cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
    return int(cr[1]), int(cr[0])


WIDTH = getTerminalSize()[0]
colors = {"red": "\033[91;1m",
          "end": "\033[0m",
          "green": "\033[92;1m",
          "lightcyan": "\033[96m",
          "blue": "\033[94;1m"}

if is_win:
    colors = {"red": "",
              "end": "",
              "green": "",
              "lightcyan": "",
              "blue": ""}

def write_colored(text, color, already_printed=0):
    text_colored = colors[color] + text + colors["end"]
    print " " * (WIDTH - already_printed - len(text)) + text_colored


def write_underline(text):
    print "\033[4m" + text + colors["end"]