From 454af7a7c02a9b4f4f2041825889a155f2c4f7e2 Mon Sep 17 00:00:00 2001 From: chrys87 Date: Thu, 24 Aug 2017 10:11:07 +0200 Subject: [PATCH] Update terminalManagement --- play zone/terminalManagement | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/play zone/terminalManagement b/play zone/terminalManagement index edc3dfa3..e830a934 100644 --- a/play zone/terminalManagement +++ b/play zone/terminalManagement @@ -32,7 +32,7 @@ class Terminal: return {"c": (cursor.x, cursor.y), "lines": lines} -def open_terminal(command="bash", columns=25, lines=80): +def open_terminal(command="bash", columns=80, lines=25): p_pid, master_fd = pty.fork() print('PID',p_pid) if p_pid == 0: # Child. @@ -51,6 +51,7 @@ def HandleTerminal(): try: tty.setraw(0) terminal, p_pid, p_out = open_terminal() + std_out = os.fdopen(sys.stdout.fileno(), "w+b", 0) #termios.tcdrain(p_pid) #termios.tcdrain(0) while running: @@ -62,12 +63,13 @@ def HandleTerminal(): if debug: print('pre p_out') try: - msgBytes = os.read(p_out.fileno(), 4) + msgBytes = read_all(p_out.fileno()) + #msgBytes = p_out.read(65536) + #msgBytes = os.read(p_out.fileno(), 4) #p_out.read(4096) except (EOFError, OSError): running = False - #sys.exit(0) - terminal.feed(msgBytes) + #sys.exit(0) os.write(sys.stdout.fileno(), msgBytes) sys.stdout.flush() #print(terminal.screen.display) @@ -75,11 +77,11 @@ def HandleTerminal(): print('after p_out') if sys.stdin in r: if debug: - print('pre stdin') - msgBytes = os.read(sys.stdin.fileno(), 1) - terminal.feed(msgBytes) - p_out.write(msgBytes) - p_out.flush() + print('pre stdin') + msgBytes = read_all(sys.stdin.fileno()) + #msgBytes = os.read(sys.stdin.fileno(), 1) + terminal.feed(msgBytes) + os.write(p_out.fileno(), msgBytes) if debug: print('after stdin') except Exception as e: # Process died? @@ -102,5 +104,20 @@ def resize_terminal(fd): rows, cols, _, _ = struct.unpack('hhhh', s) return rows, cols +def read_all(fd): + bytes = os.read(fd, 4096) + if bytes == b'': + raise EOFError + while has_more(fd): + data = os.read(fd, 4096) + if data == b'': + raise EOFError + bytes += data + return bytes + +def has_more(fd): + r, w, e = select.select([fd], [], [], 0) + return (fd in r) + if __name__ == "__main__": HandleTerminal()