From 69827a7774b0977853aa58e014e506c85e334681 Mon Sep 17 00:00:00 2001 From: Erik Wilson Date: Mon, 18 Dec 2023 10:18:09 -0700 Subject: [PATCH 1/2] feat: add ansi scrolling support --- Makefile | 4 ++++ devcluster/console.py | 24 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index bb417b3..e96f72c 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,10 @@ build: $(MAKE) clean python3 setup.py -q sdist bdist_wheel +.PHONY: install +install: build + pip3 install --force-reinstall dist/*.whl + .PHONY: publish publish: build twine check dist/* diff --git a/devcluster/console.py b/devcluster/console.py index 5a8e36f..e85913a 100644 --- a/devcluster/console.py +++ b/devcluster/console.py @@ -307,26 +307,38 @@ def handle_key(self, key: str) -> None: self.state_machine_handle.dump_state() elif key == "q": self.state_machine_handle.quit() - elif key == "k": + # include up arrow and scroll up + elif key == "k" or key == "\033[A" or key == "\033OA": self.act_scroll(1) - elif key == "u": + # include page up + elif key == "u" or key == "\033[5": self.act_scroll(10) - elif key == "j": + # include down arrow and scroll down + elif key == "j" or key == "\033[B" or key == "\033OB": self.act_scroll(-1) - elif key == "d": + # include page down + elif key == "d" or key == "\033[6": self.act_scroll(-10) - elif key == "x": + # include end key and enter + elif key == "x" or key == "\033[F" or key == "\x0d": self.act_scroll_reset() elif key == " ": self.act_marker() else: + hex = key.encode("utf-8").hex() + if key[0] == "\033": + # escape sequence + key = "\\033" + key[1:] self.logger.log( - dc.fore_num(9) + dc.asbytes(f'"{key}" is not a known shortcut\n') + dc.res + dc.fore_num(9) + dc.asbytes(f'"{key}" is not a known shortcut ({hex})\n') + dc.res ) def handle_stdin(self, ev: int, _: int) -> None: if ev & dc.Poll.IN_FLAGS: key = sys.stdin.read(1) + # escape sequence + if key == "\033": + key += sys.stdin.read(2) self.handle_key(key) elif ev & dc.Poll.ERR_FLAGS: raise ValueError("stdin closed!") From 17a9a38afd299d3b1396f0d1b80ba216bc3ef20c Mon Sep 17 00:00:00 2001 From: Erik Wilson Date: Mon, 18 Dec 2023 11:18:36 -0700 Subject: [PATCH 2/2] non-blocking read on escape --- devcluster/console.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/devcluster/console.py b/devcluster/console.py index e85913a..a833e32 100644 --- a/devcluster/console.py +++ b/devcluster/console.py @@ -1,4 +1,5 @@ import os +import fcntl import time import subprocess import sys @@ -305,7 +306,8 @@ def handle_key(self, key: str) -> None: self.state_machine_handle.quit() elif key == "\x04": # ctrl-d self.state_machine_handle.dump_state() - elif key == "q": + # include escape key + elif key == "q" or key == "\033": self.state_machine_handle.quit() # include up arrow and scroll up elif key == "k" or key == "\033[A" or key == "\033OA": @@ -325,20 +327,25 @@ def handle_key(self, key: str) -> None: elif key == " ": self.act_marker() else: - hex = key.encode("utf-8").hex() + hexKey = key.encode("utf-8").hex() if key[0] == "\033": # escape sequence key = "\\033" + key[1:] self.logger.log( - dc.fore_num(9) + dc.asbytes(f'"{key}" is not a known shortcut ({hex})\n') + dc.res + dc.fore_num(9) + + dc.asbytes(f'"{key}" is not a known shortcut (0x{hexKey})\n') + + dc.res ) def handle_stdin(self, ev: int, _: int) -> None: if ev & dc.Poll.IN_FLAGS: key = sys.stdin.read(1) - # escape sequence if key == "\033": + # escape sequence, read next 2 characters non-blocking + orig_fl = fcntl.fcntl(sys.stdin, fcntl.F_GETFL) + fcntl.fcntl(sys.stdin, fcntl.F_SETFL, orig_fl | os.O_NONBLOCK) key += sys.stdin.read(2) + fcntl.fcntl(sys.stdin, fcntl.F_SETFL, orig_fl) self.handle_key(key) elif ev & dc.Poll.ERR_FLAGS: raise ValueError("stdin closed!")