-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathterm.py
More file actions
140 lines (108 loc) · 3.6 KB
/
Copy pathterm.py
File metadata and controls
140 lines (108 loc) · 3.6 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
from typing import List, Any
import enum
import shutil
import os
import threading
import time
import sys
class Color(enum.Enum):
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
MAGENTA = "\033[95m"
CYAN = "\033[96m"
WHITE = "\033[97m"
terminal_width = shutil.get_terminal_size(fallback=(80, 24)).columns
terminal_height = shutil.get_terminal_size(fallback=(80, 24)).lines - 2
def clear():
if "GIT_ANCHOR_INTERACTIVE" not in os.environ:
return
global terminal_height
terminal_height = shutil.get_terminal_size(fallback=(80, 24)).lines - 2
print("\033[2J\033[H", end="")
clog(Color.RED, "#" * terminal_width)
def wait():
if "GIT_ANCHOR_INTERACTIVE" not in os.environ:
return
q = []
t = threading.Thread(target=lambda: q.append(input()), daemon=True)
t.start()
i = 0
while True:
print("\r\033[K", end="")
if len(q) > 0:
break
print(
f"\033[2K\033[1G{Color.YELLOW.value}Enter any key to continue{'.' * (i)}\033[0m",
end="",
)
sys.stdout.flush()
time.sleep(0.5)
i = (i + 1) % 4
t.join()
def log(color: Color, obj: Any):
if "GIT_ANCHOR_INTERACTIVE" not in os.environ:
return
global terminal_height
lines = []
if terminal_height <= 0:
clear()
if isinstance(obj, str):
lines = repr_str(
obj, max_width=terminal_width, max_lines=min(20, terminal_height)
)
elif isinstance(obj, list):
lines = repr_arr(obj, max_lines=terminal_height)
else:
lines = repr(obj, max_lines=terminal_height)
terminal_height -= len(lines)
clog_arr(color, lines)
def clog(color: Color, text: str):
"""Print text in color."""
print(f"{color.value}{text}\033[0m")
def clog_arr(color: Color, arr: List[str]):
for text in arr:
clog(color, text)
def repr(obj: Any, max_width: int = 80, max_lines: int = 20) -> List[str]:
"""pretty print object based on its type"""
if isinstance(obj, list):
return repr_arr(obj, max_width=max_width, max_lines=min(20, max_lines))
elif isinstance(obj, tuple):
if len(obj) == 0:
return []
return repr(obj[0], max_width=max_width, max_lines=min(20, max_lines)) + repr(
obj[1:], max_width=max_width, max_lines=min(20, max_lines)
)
else:
return repr_str(obj, max_width=max_width, max_lines=min(20, max_lines))
def repr_str(
obj: str, prefix: str = "", max_width: int = 80, max_lines: int = 4
) -> List[str]:
lines = str(obj).strip().splitlines()
compact_lines = [
[
" " * len(prefix) + line[i : i + max_width - len(prefix)]
for i in range(0, len(line), max_width - len(prefix))
]
for line in lines
]
line_array = sum(compact_lines, [])
line_array[0] = prefix + line_array[0][len(prefix) :]
result = line_array[0:max_lines]
if len(line_array) > max_lines:
result[-1] = " " * len(prefix) + "..."
return result
def repr_arr(arr: List[Any], max_width: int = 80, max_lines: int = 40) -> List[str]:
items_show: List[str] = []
for index, item in enumerate(arr):
item_repr = repr_str(
item, prefix=f"{index + 1:02d}. ", max_width=max_width, max_lines=5
)
items_show.extend(item_repr)
if len(item_repr) > 1 and item_repr[-1] != "\n":
items_show.append("")
result = items_show[0:max_lines]
if len(items_show) > max_lines:
result[-1] = f"... and {len(items_show) - max_lines} more"
return result