-
Notifications
You must be signed in to change notification settings - Fork 0
/
gapbuffer.py
85 lines (73 loc) · 2.64 KB
/
gapbuffer.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
from enum import Enum
class CellType(Enum):
GAP = 1
ENDFILE = 2
class GapBuffor:
def __init__(self):
self.gap_size = 16
self.buffor = [CellType.GAP] * self.gap_size
self.current_gap_size = self.gap_size
self.cursor = 0
def write(self, text: str) -> None:
for ch in text:
self.buffor[self.cursor] = ch
self.cursor += 1
self.current_gap_size -= 1
if self.current_gap_size <= 0:
last_index = len(self.buffor) - 1
self.current_gap_size = self.gap_size
self.buffor += [CellType.GAP] * self.gap_size
for i in range((last_index + 1) - self.cursor):
start = last_index - i
end = last_index + self.gap_size - i
self.buffor[start], self.buffor[end] = self.buffor[end], self.buffor[start]
def move_cursor(self, x: int) -> None:
if x == 0:
return
elif x > 0:
missed_space = self.cursor + self.current_gap_size + x - len(self.buffor)
if missed_space > 0:
return
start = self.cursor
end = self.cursor + self.current_gap_size
for _ in range(x):
self.buffor[start] = self.buffor[end]
self.buffor[end] = CellType.GAP
start += 1
end += 1
self.cursor += x
else:
start = self.cursor - 1
end = self.cursor + self.current_gap_size - 1
for _ in range(-x):
if self.cursor <= 0:
break
self.cursor -= 1
self.buffor[end] = self.buffor[start]
self.buffor[start] = CellType.GAP
start -= 1
end -= 1
def delete(self, amount: int) -> None:
if amount > 0:
self.buffor = self.buffor[:max(self.cursor - amount, 0)] + self.buffor[self.cursor:]
self.cursor -= amount
if self.cursor < 0:
self.cursor = 0
def get_char(self, index: int) -> None:
if index == len(self.buffor):
return CellType.ENDFILE
return self.buffor[index]
def clear(self) -> None:
self.buffor.clear()
self.buffor = [CellType.GAP] * self.gap_size
self.current_gap_size = self.gap_size
self.cursor = 0
def load(self, data: str) -> None:
self.clear()
self.buffor += list(data)
def get_text(self) -> str:
res = ''
for cell in self.buffor:
if type(cell) == str:
res += cell
return res