-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_rg_bar.py
103 lines (86 loc) · 2.37 KB
/
common_rg_bar.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
'''
Given:
1. status code: (0 - OK, other value - BAD)
2. terminal window width
3. (optional) Text to display (without color)
shows red/green bar to visualize return code of previous command
'''
import sys
esc = chr(27)
info_txt = '''
Usage: %(prog_name)s status_code number_of_columns
1. status code: 0 - OK (green color), other values - BAD (red color)
2. number of columns: the width of text console
3. (optional) Text to display
'''
def unicolor_emit(start_text, col_char, cols_limit, code):
print(''.join((
start_text,
esc,
'[4',
col_char,
'm',
' ' * (cols_limit - 2 - len(code) - len(start_text)),
code,
esc,
'[0m',
)))
def dual_tape_emit(cols_limit):
red = [esc, '[4', '1', 'm', ' ']
white = [esc, '[4', '7', 'm', ' ']
end = [esc, '[0m']
all_text = (red + white) * (cols_limit // 2)
if cols_limit % 2:
all_text.extend(red)
all_text.extend(end)
print(''.join(all_text))
def two_color(error_occured):
'''
error_occured:
0 - OK (green)
other values - some error (red)
'''
if error_occured:
col_char = '1'
else:
col_char = '2'
return col_char
def dual_emit(start_text, cols_limit, error_occured):
'''
Simplified version, 2 colors, for including
from other code
'''
col_char = two_color(error_occured)
code = str(error_occured)
unicolor_emit(start_text, col_char, cols_limit, code)
def main():
if len(sys.argv) >= 2:
code = sys.argv[1]
if code == 'x':
col_char = '3'
cols_limit = 78
code = '' # No code provided - only yellow bar
elif code == 't':
if len(sys.argv) >= 3:
cols_limit = int(sys.argv[2]) - 2
else:
cols_limit = 78
dual_tape_emit(cols_limit)
sys.exit(0)
else:
if code == 'y':
col_char = '3'
else:
value = int(code)
col_char = two_color(value)
cols_limit = int(sys.argv[2])
if len(sys.argv) >= 4:
start_text = sys.argv[3] + ' '
else:
start_text = ''
unicolor_emit(start_text, col_char, cols_limit, code)
else:
print(info_txt % dict(
prog_name=sys.argv[0],
))