-
Notifications
You must be signed in to change notification settings - Fork 0
/
textEditor.py
168 lines (108 loc) · 3.98 KB
/
textEditor.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from textRenderer import TextRenderer
import time
import sys
class TextEditor(TextRenderer):
'''
option-o to write out
option-q to quit
'''
def __init__(self):
super(TextEditor, self).__init__()
self.height -= 1
self.unsavedContent = False
def updateDim(self):
self.height, self.width = self.window.getmaxyx()
self.height -= 1
self.width -= self.getMargin()
def dump(self):
if not self.pathAndFile:
self.log.write('pathAndFile not specified yet\n')
sys.exit(1)
# prepare
text = '\n'.join(self.lines)
with open(self.pathAndFile, 'w') as f:
f.write(text)
self.unsavedContent = False
return False
def processKey(self, k):
options = {
'return': self.keyReturn,
'delete': self.keyDelete,
'down': self.keyDown,
'up': self.keyUp,
'left': self.keyLeft,
'right': self.keyRight,
'opt-o': self.dump,
'opt-q': self.close
}
update = False
if k in options:
function = options[k]
update = function()
elif type(k) == int and k < 256:
update = self.key(k)
self.lastKeyPress['type'] = k
self.lastKeyPress['time'] = time.time()
if update:
self.updateScreen()
def key(self, k):
self.unsavedContent = True
try:
char = bytearray([k]).decode()
except UnicodeDecodeError:
self.log.write('UnicodeDecodeError: ' + str(k) + '\n')
return
preSection = self.lines[self.y + self.scrollY][:(self.x + self.scrollX)]
postSection = self.lines[self.y + self.scrollY][(self.x + self.scrollX):]
if k == 9: # tab
char = ' ' # four spaces
self.lines[self.y + self.scrollY] = preSection + char + postSection
if k == 9:
for _ in range(3):
self.keyRight()
self.keyRight()
return True
def keyReturn(self):
self.unsavedContent = True
pre = self.lines[self.y + self.scrollY][:self.x + self.scrollX] # current line
post = self.lines[self.y + self.scrollY][self.x + self.scrollX:]
self.lines[self.y + self.scrollY] = pre
self.lines.insert(self.y + self.scrollY + 1, post)
self.keyRight()
return True
def keyDelete(self):
self.unsavedContent = True
if self.x + self.scrollX > 0:
preSection = self.lines[self.y + self.scrollY][:(self.x + self.scrollX) - 1]
postSection = self.lines[self.y + self.scrollY][(self.x + self.scrollX):]
self.lines[self.y + self.scrollY] = preSection + postSection
self.x -= 1
if self.scrollX > 0:
maxWidth = self.scrollX + self.width
visibleLines = self.lines[self.scrollY:self.scrollY + self.height]
self.log.write(str(visibleLines) + '\n')
if not any([len(line) >= maxWidth for line in visibleLines]):
self.scrollX -= 1
self.x += 1
elif self.y + self.scrollY > 0:
# blank line
remains = self.lines.pop(self.y + self.scrollY)
#length = len(self.lines[self.y + self.scrollY - 1])
self.keyLeft()
self.lines[self.y + self.scrollY] += remains
if self.y + self.scrollY == len(self.lines) - 1:
# last line
self.window.erase()
# erase() to cleanup leftovers
self.lastX = self.x + self.scrollX
return True
if __name__ == '__main__':
if len(sys.argv) != 2:
print('usage: TextEditor file')
sys.exit(1)
# if not os.path.isfile(sys.argv[1]):
# print('error: file not found')
# sys.exit(1)
with TextEditor() as m:
m.load(sys.argv[1])
m.run()