forked from vxgmichel/gambatte-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_terminal.py
More file actions
86 lines (71 loc) · 3 KB
/
Copy pathremote_terminal.py
File metadata and controls
86 lines (71 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Provide a blessed Terminal subclass for remote (SSH/telnet) streams.
"""
from __future__ import annotations
import codecs
import contextlib
from typing import IO, Generator
from blessed import Terminal as BlessedTerminal
from blessed.terminal import WINSZ
class RemoteTerminal(BlessedTerminal):
"""A blessed Terminal subclass for remote streams (SSH, telnet).
Stubs raw/cbreak mode (the remote connection is already raw) and
overrides size detection to use server protocol-negotiated values.
Callers should invoke ``get_xtgettcap()`` after initialization
to probe the terminal's true capabilities once the connection
is fully established.
"""
def __init__(
self,
stream: IO[str],
keyboard_fd: int,
rows: int,
columns: int,
kind: str | None = None,
) -> None:
self._rows = rows
self._columns = columns
self._remote_keyboard_fd = keyboard_fd
super().__init__(
kind=kind,
stream=stream,
force_styling=True,
kind_fallback="xterm-256color",
)
# wire `_keyboard_fd` and enable `_is_a_tty` *after* class initialization.
self._keyboard_fd = self._remote_keyboard_fd # type: ignore[assignment]
self._is_a_tty = True
self._keyboard_decoder = codecs.getincrementaldecoder("UTF-8")()
def probe_xtgettcap(self, timeout: float = 1.0) -> None:
"""
Probe terminal capabilities via XTGETTCAP and apply results.
This allows to improved 'number_of_colors' detection, and, to "overlay" capabilities not
found in jinxed terminfo database but detected by XTGETTCAP: 'blink', 'sitm', 'ritm',
'cvvis', 'Smulx', 'Setulc', 'Ms', the same way that blessed.Terminal() would have but we
is_a_tty was detected False when we initialized it.
This method is not called or used by gambaterm-ssh or gambaterm-telnet, because the above
capabilities are not used and kitty keyboard support pretty reliably suggests 24-bit color
support. It is just here as a suggestion, and can be safely deleted.
"""
self._xtgettcap_cache = self._Terminal__init__xtgettcap() # type: ignore[assignment]
self.number_of_colors = self._Terminal__init__color_capabilities() # type: ignore[assignment]
if self._xtgettcap_cache.supported and self.does_styling:
self._jinxed_term.overlay_capabilities(
**self._xtgettcap_cache.make_jinxed_capabilities()
)
@contextlib.contextmanager
def raw(self) -> Generator[None, None, None]:
yield
@contextlib.contextmanager
def cbreak(self) -> Generator[None, None, None]:
yield
def _height_and_width(self) -> WINSZ:
return WINSZ(
ws_row=self._rows,
ws_col=self._columns,
ws_xpixel=0,
ws_ypixel=0,
)
def update_size(self, rows: int, columns: int) -> None:
self._rows = rows
self._columns = columns