-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfile.py
77 lines (69 loc) · 2.61 KB
/
file.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
from colorama import Fore, Style
class File:
def __init__(self, file=None, tab_length=4, name=None):
# if the file is none then that means it was duplicated
self.lines = []
self.name = name or ""
self.line_num_width = 0
if file is not None:
self.lines = [
line.rstrip().replace("\t", " " * tab_length) for line in file
]
self.name = file.name if name is None else name
self.line_num_width = len(str(len(self.lines)))
def duplicate(self):
dup = File()
dup.lines = self.lines[:]
dup.name = self.name
dup.line_num_width = self.line_num_width
return dup
def parse(self, parser):
return parser.parse("\n".join(self.lines))
def get_line(self, line):
try:
return self.lines[line - 1]
except BaseException:
return "err"
def get_lines(self, start, end):
return self.lines[start - 1 : end]
def get_text(self):
return "\n".join(self.lines)
def display(
self, start_line, start_col, end_line, end_col, color=Fore.RED, underline=True
):
output = []
if start_line == end_line:
line = self.get_line(start_line)
output.append(
f"{Fore.CYAN}{start_line:>{self.line_num_width}} | {Style.RESET_ALL}{line}"
)
if underline:
output.append(
" " * (self.line_num_width + 2 + start_col)
+ f"{color + '^' * (end_col - start_col) if underline else ''}"
+ Style.RESET_ALL
)
else:
for line_num, line in enumerate(
self.get_lines(start_line, end_line), start=start_line
):
if line_num == start_line:
line = (
line[: start_col - 1]
+ f"{color if underline else ''}"
+ line[start_col - 1 :]
+ Style.RESET_ALL
)
elif line_num == end_line:
line = (
f"{color if underline else ''}"
+ line[: end_col - 1]
+ Style.RESET_ALL
+ line[end_col - 1 :]
)
else:
line = f"{color if underline else ''}" + line + Style.RESET_ALL
output.append(
f"{Fore.CYAN}{line_num:>{self.line_num_width}} | {Style.RESET_ALL}{line}"
)
return "\n".join(output)