This repository was archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagsmsg.py
More file actions
164 lines (126 loc) · 3.95 KB
/
agsmsg.py
File metadata and controls
164 lines (126 loc) · 3.95 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
class style:
"""
All styles are in ANSI escape code.
"""
class color():
black = 30
red = 31
green = 32
yellow = 33
blue = 34
purple = 35
cyan = 36
white = 37
class font():
bold = 1
italic = 2
underline = 4
strike = 9
@staticmethod
def stylize(color, msg):
""" Return msg with specified color.
Must use pre-defined color/format constants.
"""
return f'\x1b[{color}m{msg}\x1b[0m'
def info(msg, end='\n', color=style.color.green):
""" Print a information message (Default color green). """
print(f'{style.stylize(color, f"INFO")} {msg}', end=end, flush=True)
def fail(msg, end='\n', color=style.color.red):
""" Print a failing message (Default color red). """
print(f'{style.stylize(color, f"FAIL")} {msg}', end=end, flush=True)
def fatal(msg):
""" Print a failing message (Default color red) then exit with code 1. """
fail(msg)
exit(1)
def warn(msg, end='\n', color=style.color.yellow):
""" Print a warning message (Default color yellow). """
print(f'{style.stylize(color, f"WARN")} {msg}', end=end, flush=True)
def name(name_, swap=False, end='\n'):
""" Return the name in brackets. For example:\n
```text
[abc def]
[abc-def ghi]
```
"""
name_ = name_.split('/')[1] if 'submission/' in name_ else name_
name_ = ' '.join(reversed(name_.split(' '))) if swap else name_
return f'[{name_}]'
def press_continue(button='return'):
""" Press a button to continue. """
info(f'Press <{button}> to continue')
input()
def warn_index(index, msg, color=style.color.yellow):
""" Print "[`index`] message" (Yellow). """
print(style.stylize(color, f'[{index}] {msg}'), flush=True)
# Methods below only return string #
def bold(msg):
""" Return the bold message. """
return style.stylize(style.font.bold, msg)
def underline(msg):
""" Return the message with underline. """
return style.stylize(style.font.underline, msg)
def ask_yn(msg, type_='info'):
""" Prompts the user for yes (Y/y) or no (N/n).
Returns `True` if entering Y or y, `False` otherwise.
"""
text = f'{msg} [Y/n]: '
if type_ == 'info':
info(text, '')
elif type_ == 'warn':
warn(text, '')
elif type_ == 'fail':
fail(text, '')
option = input().lower()
while option != 'y' and option != 'n':
fail(f'Invalid option: {option}. Please try again: ', '')
option = input().lower()
return option == 'y'
def ask_retry():
""" Ask for retry. """
return ask_yn('Retry?')
def ask_index(start, end):
""" Prompt the user for numbers.
Return it if is a valid index, `None` otherwise.
"""
option = None
while True:
try:
option = int(input())
except ValueError:
fail(f'Invalid option (input must be an integer): {option}')
continue
if start <= option <= end:
break
fail(f'Invalid option (index out of range): {option}')
return option
def index_list(list, skip=True):
""" Print an indexed list. For example:\n
```text
[0] abc
[1] def
[2] ghi
[3] Skip
```
If `skip` set to `False`, `[x] Skip` option will not be displayed.
"""
for i, l in enumerate(list):
print(f'[{i}] {underline(l)}')
if skip:
print(f'[{i + 1}] Skip')
def textbar(msg, length=3):
""" Print a message with spaces and vertical bar before it. For example:\n
```text
5 | This is a message
123 | This is another message
```
"""
print(f'{" " * length} | {msg}', end='\r')
def align_left(msg, length):
""" Align the message to left. """
return f'{msg:<{length}}'
def align_center(msg, length):
""" Align the message to center. """
return f'{msg:^{length}}'
def align_right(msg, length):
""" Align the message to right. """
return f'{msg:>{length}}'