-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.py
198 lines (160 loc) · 6.46 KB
/
terminal.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
# -*- coding: utf-8 -*-
import curses
import logging
from threading import Thread
MAX_HISTORY_SIZE = 100
class TerminalHandler(logging.StreamHandler):
""" Enables logging in terminal. """
def __init__(self, terminal):
logging.StreamHandler.__init__(self)
self.terminal = terminal
self.terminal.start()
def emit(self, record):
msg = self.format(record)
self.terminal.display(msg)
class Terminal(Thread):
def __init__(self, callback=lambda *args: None, press=lambda *args: None):
Thread.__init__(self)
self.callback = callback
self.press = press
self.running = False
self.prompt_text = ">"
self.prompt_x = len(self.prompt_text)
self.lines = []
self.history = []
self.input_text = ""
self.bottom_text = ""
self.height = 0
self.width = 0
self.cursor_x = 0
self.cursor_y = 0
self.history_pos = 0
self.insert = True
def run(self):
""" Starts the terminal. """
self.running = True
curses.wrapper(self._mainLoop)
def stop(self):
""" Stops the terminal. """
self.running = False
# self.stdscr.nodelay(True)
self.display("Press any key to exit...")
def display(self, *msg, sep=" "):
""" Displays the concatenated msg in the terminal with separator. """
for line in sep.join([str(m) for m in msg]).split("\n"):
line = line.replace("\r", "")
if self.width > 0:
for i in range(0, len(line), self.width):
self.lines.append(line[i:i + self.width])
else:
self.lines.append(line)
while len(self.lines) > self.height - 1 and self.height > 0:
self.lines.pop(0)
self._draw()
def appendHistory(self, text):
self.history.append(str(text))
while len(self.history) > MAX_HISTORY_SIZE:
self.history.pop(0)
self.history_pos = len(self.history)
def _mainLoop(self, stdscr):
self.stdscr = stdscr
k = 0
input_x = self.prompt_x + len(self.input_text)
cursor_x = self.prompt_x
stdscr.keypad(True)
stdscr.clear()
stdscr.refresh()
while self.running:
self.height, self.width = stdscr.getmaxyx()
if k == curses.KEY_RIGHT:
cursor_x += 1
elif k == curses.KEY_LEFT:
cursor_x -= 1
elif k == curses.KEY_DOWN:
if self.history_pos < len(self.history) - 1:
self.history_pos += 1
self.input_text = self.history[self.history_pos]
else:
self.history_pos = len(self.history)
self.input_text = self.bottom_text
input_x = self.prompt_x + len(self.input_text)
cursor_x = input_x
elif k == curses.KEY_UP:
if self.history_pos > 0:
self.history_pos -= 1
self.input_text = self.history[self.history_pos]
input_x = self.prompt_x + len(self.input_text)
cursor_x = input_x
elif k == curses.KEY_ENTER or k == 10 or k == 13:
self.callback(self, self.input_text)
self.input_text = ""
self.history_pos = len(self.history)
self.bottom_text = self.input_text
elif k == curses.KEY_BACKSPACE or k == 8:
if cursor_x > self.prompt_x:
pos = cursor_x - self.prompt_x
self.input_text = self.input_text[:pos - 1] + \
self.input_text[pos:]
cursor_x -= 1
self.history_pos = len(self.history)
self.bottom_text = self.input_text
elif k == curses.KEY_DC or k == 127:
if cursor_x >= self.prompt_x:
pos = cursor_x - self.prompt_x
self.input_text = self.input_text[:pos] + \
self.input_text[pos + 1:]
self.history_pos = len(self.history)
self.bottom_text = self.input_text
elif k == curses.KEY_HOME:
cursor_x = self.prompt_x
elif k == curses.KEY_END:
cursor_x = input_x
elif k == curses.KEY_IC:
self.insert = not self.insert
elif k == curses.KEY_EIC:
self.insert = False
else:
char = chr(k)
char_text = curses.keyname(k).decode("ascii")
if len(char_text) > 1 and char_text[0] == "^":
self.press(self, char_text)
elif len(char_text) > 1 and char_text[0:4] == "ALT_":
self.press(self, char_text)
elif char.isprintable() and input_x < self.width - 1:
# TODO: Enable longer length
pos = cursor_x - self.prompt_x
if self.insert:
self.input_text = self.input_text[:pos] + \
char + self.input_text[pos:]
else:
self.input_text = self.input_text[:pos] + \
char + self.input_text[pos + len(char):]
self.history_pos = len(self.history)
self.bottom_text = self.input_text
cursor_x += len(char)
input_x = self.prompt_x + len(self.input_text)
cursor_x = max(self.prompt_x, cursor_x)
cursor_x = min(self.width - 1, input_x, cursor_x)
self.cursor_x = cursor_x
self.cursor_y = self.height - 1
self._draw()
if self.running:
k = stdscr.getch()
def _draw(self):
self.stdscr.clear()
self.height, self.width = self.stdscr.getmaxyx()
for i, l in enumerate(self.lines):
self.stdscr.addstr(i, 0, l) # TODO: Fix resizing.
self.stdscr.addstr(self.height - 1, 0,
self.prompt_text + self.input_text)
self.stdscr.move(self.cursor_y, self.cursor_x)
self.stdscr.refresh()
if __name__ == "__main__":
def callback(term, text):
if text.strip().lower() == "stop":
term.stop()
term.appendHistory(text)
term.display(text)
term = Terminal(callback)
term.start()
term.join()