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
|
import threading
import traceback
import time
import workspace.sys
from workspace import ui
COLOR = (0x44, 0x44, 0x44)
class ShellWindow(ui.Window):
def __init__(self, parent, argv=None):
super().__init__(
parent, "Shell", menu=True, maximizable=False, color=COLOR)
if argv is None or len(argv) == 0:
argv = ["CLI"]
col = ui.Column()
self.add(col)
# col.add(ui.TitleSeparator(self))
self.canvas = ShellWidget(self, argv=argv)
self.set_background_color(ui.Color(*COLOR))
col.add(self.canvas, left=12, top=6, right=12, bottom=6)
self.closed.connect(self.__on_closed)
def __on_closed(self):
self.canvas.terminate_process()
class ShellWidget(ui.Canvas):
output = ui.Signal()
exited = ui.Signal()
def __init__(self, parent, *, argv, columns=80, rows=24):
super().__init__(parent)
self.font = ui.Font("Roboto Mono", 14)
self.set_background_color(ui.Color(*COLOR))
self.text_color = ui.Color(0xff, 0xff, 0xff)
self.input_thread = None
self.output_thread = None
self.output.connect(self.__on_output)
self._completed = False
self.exited.connect(self.__on_exited)
self.line_buffer = []
self.lines = []
# for _ in range(25):
self._new_line()
self.cx = 0
self.cy = 0
# self.execute(["version"])
# self.execute(["python", "-i"])
self.process = None
self.execute(argv)
# FIXME: Using Qt directly
from fsui.qt import Qt, QPainter, QPixmap
self._widget.setFocusPolicy(Qt.StrongFocus)
painter = QPainter()
pixmap = QPixmap(100, 100)
painter.begin(pixmap)
painter.setFont(self.font.qfont())
size = painter.boundingRect(
0, 0, 10000, 1000, Qt.AlignLeft | Qt.AlignTop, " " * columns)
painter.end()
print(size)
self.line_width = size.width()
self.line_height = size.height()
self.set_min_size((self.line_width, self.line_height * rows))
def execute(self, args):
self.process = workspace.sys.workspace_exec(args)
print(self.process)
self.input_thread = InputThread(self, self.process)
self.input_thread.start()
self.output_thread = OutputThread(self, self.process)
self.output_thread.start()
def terminate_process(self):
self.input_thread.stop()
self.output_thread.stop()
self.process.kill()
def _new_line(self):
self.lines = self.lines[-24:]
self.lines.append([" "] * 80)
self.cx = 0
self.cy = len(self.lines) - 1
def __on_output(self):
print("on_output")
# This runs in the main thread
data = self.output_thread.data()
print(data)
for byte in data:
self._print_byte(byte)
def __on_exited(self):
self._complete()
def _complete(self):
if not self._completed:
self._print_text("\n[Process completed]")
self.input_thread.stop()
self.output_thread.stop()
self._completed = True
def _print_text(self, text):
for char in text:
self._print_char(char)
def _print_byte(self, byte):
# FIXME: UTF-8...
char = byte.decode("ISO-8859-1")
self._print_char(char)
def _print_char(self, char):
if char == "\n":
self._new_line()
return
if char == '\r':
self.cx = 0
return
# print(self.cy, self.cx)
self.lines[self.cy][self.cx] = char
# line = self.lines[-1]
self.cx += 1
if self.cx == 80:
# self.cy += 1
# self.cx = 0
# if self.cy == 25:
# self.lines = self.lines[:24]
self._new_line()
# if len(line) < 80:
# self.lines[-1] = line + char
# else:
# line = char
# self.lines.append(line)
# if len(self.lines) > 25:
# self.lines = self.lines[-25:]
self.refresh()
def _erase_char(self):
# FIXME: TODO: multi-line erase..
if self.cx > 0:
self.cx -= 1
self.lines[self.cy][self.cx] = " "
self.refresh()
def on_paint(self):
painter = ui.Painter(self)
painter.set_font(self.font)
if hasattr(self, "text_color"):
painter.set_text_color(self.text_color)
# lines = self.buffer()
x, y = 0, 0
for line_data in self.lines:
line = "".join(line_data)
# tw, th = painter.measure_text(line)
painter.draw_text(line, x, y)
# y += th
y += self.line_height
def on_key_press(self, event):
print("on_key_press", event)
if self._completed:
print("process is completed, ignoring")
return
# FIXME: using a QKeyEvent directly
from fsui.qt import QKeyEvent
assert isinstance(event, QKeyEvent)
char = event.text()
print(repr(char))
if not char:
print("char was", repr(char))
return
if char == "\x08":
if len(self.line_buffer) > 0:
self.line_buffer = self.line_buffer[:-1]
self._erase_char()
return
if char == "\x04":
self.input_thread.add_byte(b"\x04")
# FIXME: Close stdin if/when you type Ctrl+D?
# self._p.stdin.close()
return
if char == "\r":
# Qt peculiarity...
char = "\n"
self.line_buffer.append(char)
self._new_line()
bytes = []
for char in self.line_buffer:
bytes.append(char.encode("ISO-8859-1"))
self.input_thread.add_bytes(bytes)
self.line_buffer.clear()
return
else:
self.line_buffer.append(char)
self._print_char(char)
# byte = char.encode("ISO-8859-1")
# self.input_thread.add_byte(byte)
class OutputThread(threading.Thread):
def __init__(self, widget, p):
super().__init__()
self._widget = widget
self._p = p
self._data = []
self._available = False
self._lock = threading.Lock()
self.stop_flag = False
def data(self):
with self._lock:
data = self._data
self._data = []
self._available = False
return data
def run(self):
print("OutputThread.run")
try:
self._run()
except Exception:
traceback.print_exc()
self._widget = None
def stop(self):
self.stop_flag = True
self._widget = None
def _run(self):
while True:
byte = self._p.stdout.read(1)
if not byte:
print("no more data, ending output thread")
if self._widget is not None:
self._widget.exited.emit()
return
with self._lock:
self._data.append(byte)
if not self._available:
self._available = True
print("emit time")
if self._widget is not None:
self._widget.output.emit()
class InputThread(threading.Thread):
"""Handles data sent to child process"""
def __init__(self, widget, p):
super().__init__()
self._widget = widget
self._p = p
self._data = []
# self._available = False
self._lock = threading.Lock()
self.stop_flag = False
def add_byte(self, byte):
with self._lock:
self._data.append(byte)
def add_bytes(self, bytes):
with self._lock:
self._data.extend(bytes)
def run(self):
print("InputThread.run")
try:
self._run()
except Exception:
traceback.print_exc()
self._widget = None
def stop(self):
self.stop_flag = True
self._widget = None
def _run(self):
while not self.stop_flag:
# FIXME: conditions instead
time.sleep(0.01)
with self._lock:
if len(self._data) == 0:
continue
data = b"".join(self._data)
self._data = []
print("writing", data)
self._p.stdin.write(data)
self._p.stdin.flush()
|