-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
53 lines (41 loc) · 1.58 KB
/
Copy pathutil.py
File metadata and controls
53 lines (41 loc) · 1.58 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
import os
import sys
PRINT_IN_COLOR = True
def get_arg(index, default=None):
'''Returns the command-line argument, or the default if not provided'''
return sys.argv[index] if len(sys.argv) > index else default
def clear_screen():
'''Clears the terminal screen'''
os.system('cls' if os.name == 'nt' else 'clear')
COLORS = ['black', 'red', 'green', 'yellow',
'blue', 'purple', 'cyan', 'white']
COLOR_CODES = {c: i for i, c in enumerate(COLORS)}
STYLE_CODES = {None: 0, 'b': 1, 'u': 2}
def color_string(s, fore='black', back='white', style=None, index=None):
'''Returns a new string colored as specified'''
if PRINT_IN_COLOR:
code = '\033[{};3{};4{}m'.format(
STYLE_CODES[None if index else style],
COLOR_CODES['white' if index else fore],
index if index else COLOR_CODES[back])
return code + s + '\033[0m'
else:
return s
def pprint(objs, sep=' ', per_row=12, indent=0, sort=False):
'''Pretty-prints an object or list of objects, using pprint_string() if available'''
if not isinstance(objs, list):
objs = [objs]
objs = [o.pprint_string() if getattr(o, 'pprint_string', None) else str(o)
for o in objs]
if sort:
objs.sort()
if len(objs) > per_row:
pprint(objs[0:per_row], indent=indent)
pprint(objs[per_row:], indent=4)
elif len(objs) > 0:
print()
space = indent * ' '
blocks = [s.split('\n') for s in objs]
for i in range(len(blocks[0])):
print(space + sep.join([b[i] for b in blocks]))
print()