diff --git a/kittens/tui/loop.py b/kittens/tui/loop.py index 717fe1bdc..f95214079 100644 --- a/kittens/tui/loop.py +++ b/kittens/tui/loop.py @@ -39,20 +39,25 @@ class BinaryWrite(Protocol): pass +def debug_write(*a: Any, **kw: Any) -> None: + from base64 import standard_b64encode + fobj = kw.pop('file', sys.stderr.buffer) + buf = io.StringIO() + kw['file'] = buf + print(*a, **kw) + stext = buf.getvalue() + text = b'\x1bP@kitty-print|' + standard_b64encode(stext.encode('utf-8')) + b'\x1b\\' + fobj.write(text) + fobj.flush() + + class Debug: fobj: Optional[BinaryWrite] = None def __call__(self, *a: Any, **kw: Any) -> None: - from base64 import standard_b64encode - buf = io.StringIO() - kw['file'] = buf - print(*a, **kw) - stext = buf.getvalue() - text = b'\x1bP@kitty-print|' + standard_b64encode(stext.encode('utf-8')) + b'\x1b\\' - fobj = self.fobj or sys.stdout.buffer - fobj.write(text) - fobj.flush() + kw['file'] = self.fobj or sys.stdout.buffer + debug_write(*a, **kw) debug = Debug() diff --git a/kitty/complete.py b/kitty/complete.py index f925b90c5..4fc49bb74 100644 --- a/kitty/complete.py +++ b/kitty/complete.py @@ -79,12 +79,8 @@ class MatchGroup: def debug(*a: Any, **kw: Any) -> None: - from kittens.tui.loop import Debug - if not hasattr(debug, 'output'): - d = Debug() - d.fobj = sys.stderr.buffer # type: ignore - setattr(debug, 'output', d) - getattr(debug, 'output')(*a, **kw) + from kittens.tui.loop import debug_write + debug_write(*a, **kw) class Delegate: