def __init__(self, stdin, stdout, term_type): # A patch until https://github.com/ipython/ipython/issues/11745 is solved TerminalInteractiveShell.simple_prompt = False term_input = Vt100Input(stdin) term_output = Vt100_Output.from_pty(stdout, term_type) super().__init__(pt_session_options=dict(input=term_input, output=term_output), stdin=stdin, stdout=stdout) self.use_rawinput = True
def attach(self, detach_other_clients=False, color_depth=ColorDepth.DEPTH_8_BIT): """ Attach client user interface. """ assert isinstance(detach_other_clients, bool) self._send_size() self._send_packet({ 'cmd': 'start-gui', 'detach-others': detach_other_clients, 'color-depth': color_depth, 'term': os.environ.get('TERM', ''), 'data': '' }) with raw_mode(sys.stdin.fileno()): data_buffer = b'' stdin_fd = sys.stdin.fileno() socket_fd = self.socket.fileno() current_timeout = INPUT_TIMEOUT # Timeout, used to flush escape sequences. try: def winch_handler(signum, frame): self._send_size() signal.signal(signal.SIGWINCH, winch_handler) while True: r = select_fds([stdin_fd, socket_fd], current_timeout) if socket_fd in r: # Received packet from server. data = self.socket.recv(1024) if data == b'': # End of file. Connection closed. # Reset terminal o = Vt100_Output.from_pty(sys.stdout) o.quit_alternate_screen() o.disable_mouse_support() o.disable_bracketed_paste() o.reset_attributes() o.flush() return else: data_buffer += data while b'\0' in data_buffer: pos = data_buffer.index(b'\0') self._process(data_buffer[:pos]) data_buffer = data_buffer[pos + 1:] elif stdin_fd in r: # Got user input. self._process_stdin() current_timeout = INPUT_TIMEOUT else: # Timeout. (Tell the server to flush the vt100 Escape.) self._send_packet({'cmd': 'flush-input'}) current_timeout = None finally: signal.signal(signal.SIGWINCH, signal.SIG_IGN)