-
Notifications
You must be signed in to change notification settings - Fork 0
/
printing.py
89 lines (79 loc) · 2.32 KB
/
printing.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
# -*- coding:utf-8 -*-
"""
Class to simplify printing with colors on terminal.
"""
from termcolor import colored
class Printer(object):
"""Class to simplify printing with colors on terminal."""
def __init__(self):
self.text_colors = [
"grey",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white"
]
self.back_colors = [
"grey",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white"
]
self.attributes = [
"bold",
"dark",
"underline",
"blink",
"reverse",
"concealed" # hidden
]
self.max_string = 10
self.max_spaces = 10
def _format_string(self, string, max_length, spacing):
"""
Defines spaces to show as a table and limits
characters to show.
"""
if len(string) > max_length - 2:
string = string[:max_length - 2] + "..."
string = string.ljust(spacing)
return string
def color_print(self, text, foreground="white", background="grey", styles=None):
text = colored(text, foreground, "on_" + background, attrs=styles)
print(text)
def special_print(self, struct):
"""
Prints an object in a special way, for simplified view.
for lists, prints each element in a line.
"""
if type(struct) == list:
for item in struct:
print(item)
elif type(struct) == dict:
keys = list(struct.keys())
len_chars_numbers = len(str(len(keys)))
for key, value in struct.items():
index = keys.index(key)
print("{:<5} {} {}".format(
index,
self._format_string(key, self.max_string, self.max_spaces),
self._format_string(value, self.max_string, self.max_spaces)
))
else:
print(struct)
def main():
Printer().color_print("hello", "red", "green", ["concealed"])
Printer().special_print({
"hello": "bye",
"red": "blue",
"black": "white"
})
if __name__ == "__main__":
main()