Make debug printing in backend.py more convenient

This commit is contained in:
Kovid Goyal 2024-05-17 17:35:04 +05:30
parent 28f2919753
commit 036d1174c7
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -2,6 +2,7 @@
# License: GPLv3 Copyright: 2024, Kovid Goyal <kovid at kovidgoyal.net>
import json
import os
import string
import sys
import tempfile
@ -28,11 +29,22 @@ from kitty.options.utils import parse_font_spec
from kitty.utils import screen_size_function
def setup_debug_print() -> bool:
if 'KITTY_STDIO_FORWARDED' in os.environ:
try:
fd = int(os.environ['KITTY_STDIO_FORWARDED'])
except Exception:
return False
sys.stdout = open(fd, 'w', closefd=False)
return True
return False
def send_to_kitten(x: Any) -> None:
try:
sys.stdout.buffer.write(json.dumps(x).encode())
sys.stdout.buffer.write(b'\n')
sys.stdout.buffer.flush()
sys.__stdout__.buffer.write(json.dumps(x).encode())
sys.__stdout__.buffer.write(b'\n')
sys.__stdout__.buffer.flush()
except BrokenPipeError:
raise SystemExit('Pipe to kitten was broken while sending data to it')
@ -45,17 +57,6 @@ class TextStyle(TypedDict):
background: str
def print(*a: Any) -> None:
import builtins
import os
if 'KITTY_STDIO_FORWARDED' in os.environ:
fd = int(os.environ['KITTY_STDIO_FORWARDED'])
with open(fd, 'w', closefd=False) as f:
builtins.print(*a, file=f)
else:
builtins.print(*a, file=sys.stderr)
OptNames = Literal['font_family', 'bold_font', 'italic_font', 'bold_italic_font']
FamilyKey = Tuple[OptNames, ...]
@ -147,6 +148,7 @@ def resolved_faces(opts: Options) -> Dict[OptNames, ResolvedFace]:
def main() -> None:
setup_debug_print()
cache: Dict[FaceKey, RenderedSampleTransmit] = {}
for line in sys.stdin.buffer:
cmd = json.loads(line)