-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_keys.py
More file actions
309 lines (266 loc) · 10.5 KB
/
process_keys.py
File metadata and controls
309 lines (266 loc) · 10.5 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
from pprint import pformat
import re
from completion import (complete_double_tab, complete_tab, get_suggest,
handle_completion)
from vitural_terminal import *
def process_history(arg):
if arg == '!!':
return Shell.HISTORY_STACK[-1]
else:
return Shell.HISTORY_STACK[int(arg[1:])]
def process_KEY_UP(input, curs_pos):
try:
if len(Shell.HISTORY_STACK) == 0:
return input
if input not in [Shell.HISTORY_STACK[Shell.STACK_CURRENT_INDEX], '\n', '']:
Shell.HISTORY_STACK.append(input)
Shell.STACK_CURRENT_INDEX -= 1
if abs(Shell.STACK_CURRENT_INDEX) != len(Shell.HISTORY_STACK): # Not meet the start
Shell.del_nlines(Shell.count_lines(
Shell.HISTORY_STACK[Shell.STACK_CURRENT_INDEX]), curs_pos[0], False)
window.addstr(curs_pos[0], 0, Shell.PROMPT +
Shell.HISTORY_STACK[Shell.STACK_CURRENT_INDEX-1])
input = Shell.HISTORY_STACK[Shell.STACK_CURRENT_INDEX-1]
Shell.STACK_CURRENT_INDEX -= 1
else:
if input is not Shell.HISTORY_STACK[0]: # EndOfStack
Shell.del_nlines(Shell.count_lines(Shell.HISTORY_STACK[0]))
window.addstr(
curs_pos[0], 0, Shell.PROMPT + Shell.HISTORY_STACK[0])
input = Shell.HISTORY_STACK[0]
return input
except IndexError:
pass
def process_KEY_DOWN(input, curs_pos):
try:
if len(Shell.HISTORY_STACK) == 0:
return input
if input not in [Shell.HISTORY_STACK[Shell.STACK_CURRENT_INDEX], '\n', '']:
Shell.HISTORY_STACK.append(input)
Shell.STACK_CURRENT_INDEX += 1
if Shell.STACK_CURRENT_INDEX != -1: # Not meet the end of stack
try:
Shell.HISTORY_STACK[Shell.STACK_CURRENT_INDEX+1]
except IndexError:
return input
Shell.del_nlines(Shell.count_lines(
Shell.HISTORY_STACK[Shell.STACK_CURRENT_INDEX]))
# print the previous
window.addstr(
curs_pos[0], 0, Shell.PROMPT + Shell.HISTORY_STACK[Shell.STACK_CURRENT_INDEX+1])
input = Shell.HISTORY_STACK[Shell.STACK_CURRENT_INDEX+1]
Shell.STACK_CURRENT_INDEX += 1
else:
if input is not Shell.HISTORY_STACK[-1]: # EndOfStack
Shell.del_nlines(
Shell.count_lines(Shell.HISTORY_STACK[-1]))
window.addstr(
curs_pos[0], 0, Shell.PROMPT + Shell.HISTORY_STACK[-1])
input = Shell.HISTORY_STACK[-1]
return input
except IndexError:
pass
def process_KEY_LEFT(input, input_pos):
pos = Shell.cursor_pos()
if pos[1] > 10 or pos[0] != input_pos[0]:
if pos[1] - 1 < 0:
pos = (pos[0] - 1, Shell.WIDTH)
Shell.move(pos[0], pos[1]-1)
elif pos[1] == 10:
Shell.move(pos[0], pos[1])
def process_KEY_RIGHT(input, input_pos):
pos = Shell.cursor_pos()
step = pos[0]*Shell.WIDTH + pos[1] + 1
if Shell.step(pos[0], pos[1]) < Shell.step(input_pos[0], input_pos[1]) + len(input):
Shell.move(step // Shell.WIDTH, step % Shell.WIDTH)
def process_KEY_BACKSPACE(input, input_pos):
pos = Shell.cursor_pos()
del_loc = Shell.step(pos[0], pos[1]) - \
Shell.step(input_pos[0], input_pos[1])
if del_loc > 0:
input = input[:del_loc-1] + input[del_loc:]
Shell.del_nlines(Shell.count_lines(
input), input_pos[0], revese=False)
window.addstr(input_pos[0], 0, Shell.PROMPT + input)
if pos[1] > 10 or pos[0] != input_pos[0]:
Shell.move(pos[0], pos[1]-1)
elif pos[1] == 10:
Shell.move(pos[0], pos[1])
return input
def process_KEY_TAB(input, input_pos):
string = input
if Shell.last_key in ['TAB', 'TAB2']: # second TAB
data = complete_double_tab(input)
if len(data) and data != input:
Shell.printf('\n'+pformat(data, Shell.WIDTH))
Shell.last_key = 'TAB2'
Shell.can_break = True
Shell.restore = True
return input
else:
pos = Shell.cursor_pos()
insert_pos = Shell.step(pos[0], pos[1]) - \
Shell.step(input_pos[0], input_pos[1])
string = input[:insert_pos]
if string != complete_tab(input[:insert_pos]):
string = complete_tab(input[:insert_pos])
input = string + input[insert_pos:]
Shell.write_log(new=input[insert_pos:], end='', mode='a')
Shell.last_key = 'TAB'
Shell.add_str(input_pos[0], 10, input)
step = Shell.step(input_pos[0], input_pos[1]) + len(string)
Shell.move(step//Shell.WIDTH, step % Shell.WIDTH)
return input
def process_KEY_DELETE(input, input_pos):
pos = Shell.cursor_pos()
del_loc = Shell.step(pos[0], pos[1]) - \
Shell.step(input_pos[0], input_pos[1]) + 1
if del_loc > 0:
input = input[:del_loc-1] + input[del_loc:]
Shell.del_nlines(Shell.count_lines(
input), input_pos[0], revese=False)
window.addstr(input_pos[0], 0, Shell.PROMPT + input)
Shell.move(pos[0], pos[1])
return input
def process_KEY_RESIZE(input, input_pos):
window.clear()
window.refresh()
data = Shell.read_log()
Shell.add_str(0, 0, data)
Shell.HEIGHT, Shell.WIDTH = window.getmaxyx()
pos = Shell.cursor_pos()
# Recalculate input position
loc_step = Shell.step(pos[0], pos[1]) - len(input)
input_pos = loc_step//Shell.WIDTH, loc_step % Shell.WIDTH
Shell.move(pos[0], pos[1])
return input, input_pos
def process_insert_mode(input, input_pos, char, last_data):
pos = Shell.cursor_pos()
insert_loc = Shell.step(pos[0], pos[1]) - \
Shell.step(input_pos[0], input_pos[1])
if Shell.step(input_pos[0], input_pos[1]) + len(input) == Shell.WIDTH * Shell.HEIGHT:
insert_loc += Shell.WIDTH
input = input[:insert_loc] + char + input[insert_loc:]
if Shell.step(input_pos[0], input_pos[1]) + len(input) > Shell.WIDTH * Shell.HEIGHT:
input_pos = input_pos[0] - 1, input_pos[1]
window.addstr(input_pos[0], input_pos[1], input)
Shell.move(pos[0], pos[1]+1)
return input, input_pos
def process_input():
input = ""
Shell.restore_window()
last_key = Shell.last_key
if Shell.restore:
try:
input = Shell.HISTORY_STACK[-1]
char = Shell.getch(Shell.PROMPT, restore=input)
Shell.restore = False
input_pos = Shell.cursor_pos()[0], len(Shell.PROMPT)
except IndexError:
char = Shell.getch(Shell.PROMPT)
input_pos = Shell.cursor_pos()
else:
char = Shell.getch(Shell.PROMPT)
input_pos = Shell.cursor_pos()
last_data = Shell.read_log()
if last_key == 'TAB2':
last_data = last_data[:last_data.rfind(Shell.PROMPT)+len(Shell.PROMPT)]
Shell.can_break = False
while char not in ['\n']:
######################### KEY process ####################################
"""
This block's purposes are handling special KEYS
"""
if char == chr(curses.KEY_RESIZE):
input, input_pos = process_KEY_RESIZE(input, input_pos)
char = ''
elif char == chr(curses.KEY_UP):
Shell.last_key = char
input = process_KEY_UP(input, input_pos)
char = ''
elif char == chr(curses.KEY_DOWN):
Shell.last_key = char
input = process_KEY_DOWN(input, input_pos)
char = ''
elif char == chr(curses.KEY_LEFT):
Shell.last_key = char
process_KEY_LEFT(input, input_pos)
char = ''
elif char == chr(curses.KEY_RIGHT):
Shell.last_key = char
process_KEY_RIGHT(input, input_pos)
char = ''
elif char == chr(curses.KEY_END):
Shell.last_key = char
Shell.move_relative(input_pos, len(input))
char = ''
elif char == chr(curses.KEY_HOME):
Shell.last_key = char
Shell.move_relative(input_pos, 0)
char = ''
elif char == chr(curses.KEY_BACKSPACE) or ord(char) == 127: # curses.BACKSPACE
Shell.last_key = char
input = process_KEY_BACKSPACE(input, input_pos)
char = ''
elif ord(char) == 9: # curses.TAB
input = process_KEY_TAB(input, input_pos)
if Shell.can_break:
break
char = ''
elif char == chr(curses.KEY_DC):
Shell.last_key = char
input = process_KEY_DELETE(input, input_pos)
char = ''
##############################################################################################
# Insert mode
if char != '':
Shell.last_key = char
input, input_pos = process_insert_mode(
input, input_pos, char, last_data)
Shell.write_log(last_data, input)
Shell.restore = False
char = chr(window.getch())
_input = input
if _input not in ['\n', ''] or Shell.can_break is True:
try:
if Shell.HISTORY_STACK[-1] != _input:
Shell.HISTORY_STACK.append(_input)
Shell.STACK_CURRENT_INDEX = 0
except IndexError:
Shell.HISTORY_STACK.append(_input)
Shell.STACK_CURRENT_INDEX = 0
################### retrieve history command ############
if input.startswith('!'):
Shell.HISTORY_STACK.pop()
ls_re = re.split('(!!|!-?\d*)', input)
if input.startswith('!'):
Shell.printf('\n'+process_history(input), end='')
for c in ls_re:
if '!' in c:
input = input.replace(c, process_history(c))
#########################################################
if Shell.last_key not in['TAB2']:
step = input_pos[0]*Shell.WIDTH + input_pos[1] + len(input)
Shell.move(step // Shell.WIDTH, step % Shell.WIDTH)
Shell.write_log(new='\n', mode='a')
if not Shell.can_break:
Shell.last_key = '\n'
if Shell.last_key in ['TAB2']:
Shell.printf(Shell.PROMPT, end='', write_log=False)
input = ""
else:
window.addstr("\n")
window.refresh()
return input
if __name__ == '__main__':
shell = Shell()
while True:
input = process_input()
if input == 'exit':
break
elif input == 'history':
Shell.display_history()
elif input == 'clear':
Shell.clear()
pass
curses.endwin()