Skip to content

Commit 7fa2d57

Browse files
committed
policy(docs[osc52]): Add docstrings, doctests, and OSC 52/VT500 refs
why: Document observer reply gates, ground-state checks, and OSC 52 clipboard events with executable doctests and pinned upstream refs. what: - Add VT500 / Ghostty v1.4.0 refs and doctests to at_ground_state - Add xterm / tmux 3.8 OSC 52 refs and doctests to enqueue_reply, _emit_reply, and ClipboardReadRequested
1 parent 234e824 commit 7fa2d57

3 files changed

Lines changed: 90 additions & 4 deletions

File tree

‎src/libvt/events.py‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class ClipboardWrite(Event):
231231

232232
@dataclass(frozen=True, slots=True, kw_only=True, match_args=False)
233233
class ClipboardReadRequested(Event):
234-
"""Emitted when ``OSC 52 ; <selection> ; ?`` queries clipboard contents.
234+
r"""Emitted when ``OSC 52 ; <selection> ; ?`` queries clipboard contents.
235235
236236
Emitted only when :attr:`~libvt.config.Policy.clipboard_read` is
237237
:attr:`~libvt.config.ClipboardReadPolicy.EMIT_EVENT`; ``libvt`` never
@@ -241,6 +241,27 @@ class ClipboardReadRequested(Event):
241241
----------
242242
selection : str
243243
Requested clipboard selection target(s) (e.g. ``"c"`` or ``"p"``).
244+
245+
References
246+
----------
247+
- `xterm Control Sequences — Manipulate Selection Data (OSC 52)
248+
<https://invisible-island.net/xterm/ctlseqs/ctlseqs.html>`__
249+
- `ghostty-org/ghostty clipboard_operation.zig (v1.4.0)
250+
<https://github.com/ghostty-org/ghostty/blob/v1.4.0/src/terminal/osc/parsers/clipboard_operation.zig#L24-L45>`__
251+
- `tmux/tmux input.c (3.8)
252+
<https://github.com/tmux/tmux/blob/3.8/input.c#L3395-L3398>`__
253+
254+
Examples
255+
--------
256+
>>> from libvt.config import ClipboardReadPolicy, Policy
257+
>>> from libvt.terminal import Terminal
258+
>>> term = Terminal(
259+
... policy=Policy(clipboard_read=ClipboardReadPolicy.EMIT_EVENT),
260+
... record_events=True,
261+
... )
262+
>>> term.feed(b"\x1b]52;c;?\x07")
263+
>>> term.drain_events()
264+
(ClipboardReadRequested(selection='c'),)
244265
"""
245266

246267
selection: str

‎src/libvt/parser.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,26 @@ def utf8_pending(self) -> bool:
703703

704704
@property
705705
def at_ground_state(self) -> bool:
706-
"""Return ``True`` when in ``GROUND`` with no partial UTF-8 bytes pending."""
706+
r"""Return ``True`` when in ``GROUND`` with no partial UTF-8 bytes pending.
707+
708+
References
709+
----------
710+
- `Paul Flo Williams — A parser for DEC's ANSI-compatible video terminals
711+
<https://vt100.net/emu/dec_ansi_parser>`__
712+
- `ghostty-org/ghostty src/terminal/Parser.zig (v1.4.0)
713+
<https://github.com/ghostty-org/ghostty/blob/v1.4.0/src/terminal/Parser.zig#L15-L23>`__
714+
715+
Examples
716+
--------
717+
>>> from libvt.parser import Parser
718+
>>> p = Parser()
719+
>>> _ = p.feed(b"\x1b[")
720+
>>> p.at_ground_state
721+
False
722+
>>> _ = p.feed(b"0m")
723+
>>> p.at_ground_state
724+
True
725+
"""
707726
return self._state == ParserState.GROUND and not self._utf8_pending
708727

709728
def reset(self) -> None:

‎src/libvt/terminal.py‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,26 @@ def parser_state(self) -> ParserState:
17251725

17261726
@property
17271727
def at_ground_state(self) -> bool:
1728-
"""Return ``True`` when the parser is in ``GROUND`` with no pending UTF-8."""
1728+
r"""Return ``True`` when the parser is in ``GROUND`` with no pending UTF-8.
1729+
1730+
References
1731+
----------
1732+
- `Paul Flo Williams — A parser for DEC's ANSI-compatible video terminals
1733+
<https://vt100.net/emu/dec_ansi_parser>`__
1734+
- `ghostty-org/ghostty src/terminal/Parser.zig (v1.4.0)
1735+
<https://github.com/ghostty-org/ghostty/blob/v1.4.0/src/terminal/Parser.zig#L15-L23>`__
1736+
1737+
Examples
1738+
--------
1739+
>>> from libvt.terminal import Terminal
1740+
>>> term = Terminal(rows=2, cols=10)
1741+
>>> term.feed(b"\xe6\x97")
1742+
>>> term.at_ground_state
1743+
False
1744+
>>> term.feed(b"\xa5")
1745+
>>> term.at_ground_state
1746+
True
1747+
"""
17291748
return self._parser.at_ground_state
17301749

17311750
@property
@@ -2184,7 +2203,7 @@ def drain_replies(self) -> bytes:
21842203
return out
21852204

21862205
def enqueue_reply(self, data: collections.abc.Buffer, /) -> None:
2187-
"""Queue explicit host reply *data* onto :meth:`drain_replies`.
2206+
r"""Queue explicit host reply *data* onto :meth:`drain_replies`.
21882207
21892208
Parameters
21902209
----------
@@ -2193,6 +2212,22 @@ def enqueue_reply(self, data: collections.abc.Buffer, /) -> None:
21932212
an ``OSC 52`` clipboard payload answering
21942213
:class:`~libvt.events.ClipboardReadRequested`), bounded by
21952214
:attr:`~libvt.config.Limits.max_reply_bytes` when buffered.
2215+
2216+
References
2217+
----------
2218+
- `xterm Control Sequences — Manipulate Selection Data (OSC 52)
2219+
<https://invisible-island.net/xterm/ctlseqs/ctlseqs.html>`__
2220+
- `tmux/tmux input.c — input_osc_52_reply (3.8)
2221+
<https://github.com/tmux/tmux/blob/3.8/input.c#L3341-L3412>`__
2222+
2223+
Examples
2224+
--------
2225+
>>> from libvt.config import Policy
2226+
>>> from libvt.terminal import Terminal
2227+
>>> term = Terminal(policy=Policy(emit_replies=False))
2228+
>>> term.enqueue_reply(b"\x1b]52;c;aGVsbG8=\x1b\\")
2229+
>>> term.drain_replies()
2230+
b'\x1b]52;c;aGVsbG8=\x1b\\'
21962231
"""
21972232
payload = bytes(data)
21982233
if not payload:
@@ -2255,6 +2290,17 @@ def notify_process_exit(self) -> None:
22552290
)
22562291

22572292
def _emit_reply(self, payload: bytes) -> None:
2293+
r"""Append automatic terminal reply *payload* when ``Policy.emit_replies``.
2294+
2295+
Examples
2296+
--------
2297+
>>> from libvt.config import Policy
2298+
>>> from libvt.terminal import Terminal
2299+
>>> term = Terminal(policy=Policy(emit_replies=False))
2300+
>>> term.feed(b"\x1b[5n")
2301+
>>> term.drain_replies()
2302+
b''
2303+
"""
22582304
if not self._policy.emit_replies or not payload:
22592305
return
22602306
if self._on_pty_write is not None:

0 commit comments

Comments
 (0)