forked from pfalcon/picotui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seditor.py
256 lines (230 loc) · 6.85 KB
/
seditor.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#
# Very simple VT100 terminal text editor widget
# Copyright (c) 2015 Paul Sokolovsky
# Distributed under MIT License
#
import sys
import os
KEY_UP = 1
KEY_DOWN = 2
KEY_LEFT = 3
KEY_RIGHT = 4
KEY_HOME = 5
KEY_END = 6
KEY_PGUP = 7
KEY_PGDN = 8
KEY_QUIT = 9
KEY_ENTER = 10
KEY_BACKSPACE = 11
KEY_DELETE = 12
KEYMAP = {
b"\x1b[A": KEY_UP,
b"\x1b[B": KEY_DOWN,
b"\x1b[D": KEY_LEFT,
b"\x1b[C": KEY_RIGHT,
b"\x1bOH": KEY_HOME,
b"\x1bOF": KEY_END,
b"\x1b[1~": KEY_HOME,
b"\x1b[4~": KEY_END,
b"\x1b[5~": KEY_PGUP,
b"\x1b[6~": KEY_PGDN,
b"\x03": KEY_QUIT,
b"\r": KEY_ENTER,
b"\x7f": KEY_BACKSPACE,
b"\x1b[3~": KEY_DELETE,
}
class Editor:
def __init__(self):
self.top_line = 0
self.cur_line = 0
self.row = 0
self.col = 0
self.height = 25
@staticmethod
def wr(s):
# TODO: When Python is 3.5, update this to use only bytes
if isinstance(s, str):
s = bytes(s, "utf-8")
os.write(1, s)
@staticmethod
def cls():
Editor.wr(b"\x1b[2J")
@staticmethod
def goto(row, col):
# TODO: When Python is 3.5, update this to use bytes
Editor.wr("\x1b[%d;%dH" % (row + 1, col + 1))
@staticmethod
def clear_to_eol():
Editor.wr(b"\x1b[0K")
@staticmethod
def cursor(onoff):
if onoff:
Editor.wr(b"\x1b[?25h")
else:
Editor.wr(b"\x1b[?25l")
def set_cursor(self):
self.goto(self.row, self.col)
def adjust_cursor_eol(self):
l = len(self.content[self.cur_line])
if self.col > l:
self.col = l
def set_lines(self, lines):
self.content = lines
self.total_lines = len(lines)
def update_screen(self):
self.cursor(False)
self.goto(0, 0)
self.cls()
i = self.top_line
for c in range(self.height):
self.show_line(self.content[i])
self.wr(b"\r\n")
i += 1
if i == self.total_lines:
break
self.set_cursor()
self.cursor(True)
def update_line(self):
self.cursor(False)
self.wr(b"\r")
self.show_line(self.content[self.cur_line])
self.clear_to_eol()
self.set_cursor()
self.cursor(True)
def show_line(self, l):
self.wr(l)
def next_line(self):
if self.row + 1 == self.height:
self.top_line += 1
return True
else:
self.row += 1
return False
def handle_cursor_keys(self, key):
if key == KEY_DOWN:
if self.cur_line + 1 != self.total_lines:
self.cur_line += 1
self.adjust_cursor_eol()
if self.next_line():
self.update_screen()
else:
self.set_cursor()
elif key == KEY_UP:
if self.cur_line > 0:
self.cur_line -= 1
self.adjust_cursor_eol()
if self.row == 0:
if self.top_line > 0:
self.top_line -= 1
self.update_screen()
else:
self.row -= 1
self.set_cursor()
elif key == KEY_LEFT:
if self.col > 0:
self.col -= 1
self.set_cursor()
elif key == KEY_RIGHT:
self.col += 1
self.adjust_cursor_eol()
self.set_cursor()
elif key == KEY_HOME:
self.col = 0
self.set_cursor()
elif key == KEY_END:
self.col = len(self.content[self.cur_line])
self.set_cursor()
elif key == KEY_PGUP:
self.cur_line -= self.height
self.top_line -= self.height
if self.top_line < 0:
self.top_line = 0
self.cur_line = 0
self.row = 0
elif self.cur_line < 0:
self.cur_line = 0
self.row = 0
self.adjust_cursor_eol()
self.update_screen()
elif key == KEY_PGDN:
self.cur_line += self.height
self.top_line += self.height
if self.cur_line >= self.total_lines:
self.top_line = self.total_lines - self.height
self.cur_line = self.total_lines - 1
if self.top_line >= 0:
self.row = self.height - 1
else:
self.top_line = 0
self.row = self.cur_line
self.adjust_cursor_eol()
self.update_screen()
else:
return False
return True
def loop(self):
self.update_screen()
while True:
buf = os.read(0, 32)
sz = len(buf)
i = 0
while i < sz:
if buf[0] == 0x1b:
key = buf
i = len(buf)
else:
key = buf[i:i + 1]
i += 1
#self.show_status(repr(key))
if key in KEYMAP:
key = KEYMAP[key]
if key == KEY_QUIT:
return key
if self.handle_cursor_keys(key):
continue
res = self.handle_key(key)
if res is not None:
return res
def handle_key(self, key):
l = self.content[self.cur_line]
if key == KEY_ENTER:
self.content[self.cur_line] = l[:self.col]
self.cur_line += 1
self.content[self.cur_line:self.cur_line] = [l[self.col:]]
self.total_lines += 1
self.col = 0
self.next_line()
self.update_screen()
elif key == KEY_BACKSPACE:
if self.col:
self.col -= 1
l = l[:self.col] + l[self.col + 1:]
self.content[self.cur_line] = l
self.update_line()
elif key == KEY_DELETE:
l = l[:self.col] + l[self.col + 1:]
self.content[self.cur_line] = l
self.update_line()
else:
l = l[:self.col] + str(key, "utf-8") + l[self.col:]
self.content[self.cur_line] = l
self.col += 1
self.update_line()
def init_tty(self):
import tty, termios
self.org_termios = termios.tcgetattr(0)
tty.setraw(0)
def deinit_tty(self):
# Don't leave cursor in the middle of screen
self.goto(self.height, 0)
import termios
termios.tcsetattr(0, termios.TCSANOW, self.org_termios)
if __name__ == "__main__":
with open(sys.argv[1]) as f:
content = f.read().splitlines()
#content = f.readlines()
e = Editor()
e.init_tty()
e.set_lines(content)
e.loop()
e.deinit_tty()