Skip to content

Commit dac6291

Browse files
committed
the tests
1 parent 3fb44b4 commit dac6291

3 files changed

Lines changed: 17 additions & 11 deletions

File tree

docs/pages/advanced_topics/kitty_keyboard_protocol.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,12 @@ Wire format reference
166166
---------------------
167167

168168
**Push flags.**
169-
``CSI = <flags> ; <mode> u``. The spec defines mode 1 (set),
170-
mode 2 (OR into current), and mode 3 (AND-NOT into current).
171-
prompt_toolkit always hardcodes mode 1 with ``flags=1``; the other
172-
modes exist in the spec but aren't used here.
169+
``CSI > <flags> u`` pushes ``<flags>`` onto the terminal's stack so
170+
the pop on exit restores the pre-push state. prompt_toolkit always
171+
pushes ``flags=1`` (disambiguate). The spec also defines
172+
``CSI = <flags> ; <mode> u`` which *modifies* the top of the stack
173+
in place (mode 1 set, 2 OR, 3 AND-NOT); we don't use it because it
174+
offers no clean restore.
173175

174176
**Pop flags.**
175177
``CSI < u`` pops one entry. ``CSI < N u`` pops N.

src/prompt_toolkit/output/kitty_keyboard.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
The protocol exposes progressive enhancement flags that a program pushes
77
onto a terminal-maintained stack, then pops on exit:
88
9-
CSI = <flags> ; 1 u — push <flags>, mode 1 (set, not OR/AND)
9+
CSI > <flags> u — push <flags> onto the stack
1010
CSI < u — pop one entry
1111
CSI ? u — query current flags (response: CSI ? <flags> u)
1212
13+
(`CSI = <flags> ; <mode> u` also exists — it *modifies* the top of the
14+
stack in place rather than pushing. We want a clean restore on exit, so
15+
we push.)
16+
1317
Flag bits (from the spec):
1418
1519
0b1 (1) Disambiguate escape codes
@@ -86,8 +90,8 @@ def kitty_keyboard_protocol(
8690
# self-contained and the rest of the codebase has no mention of it.
8791
depth: int = getattr(output, "_kitty_keyboard_depth", 0)
8892
if depth == 0:
89-
# Push: `CSI = <flags> ; 1 u` (mode 1 = set).
90-
output.write_raw(f"\x1b[={flags};1u")
93+
# Push: `CSI > <flags> u`.
94+
output.write_raw(f"\x1b[>{flags}u")
9195
output.flush()
9296
output._kitty_keyboard_flags = flags # type: ignore[attr-defined]
9397
else:

tests/test_kitty_keyboard.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def test_output_push_and_pop():
233233
with kitty_keyboard_protocol(out):
234234
pass
235235
out.flush()
236-
assert buf.getvalue() == "\x1b[=1;1u\x1b[<u"
236+
assert buf.getvalue() == "\x1b[>1u\x1b[<u"
237237

238238

239239
def test_output_custom_flags():
@@ -242,7 +242,7 @@ def test_output_custom_flags():
242242
with kitty_keyboard_protocol(out, flags=flags):
243243
pass
244244
out.flush()
245-
assert buf.getvalue() == f"\x1b[={flags};1u\x1b[<u"
245+
assert buf.getvalue() == f"\x1b[>{flags}u\x1b[<u"
246246

247247

248248
def test_output_nested_does_not_double_push():
@@ -252,7 +252,7 @@ def test_output_nested_does_not_double_push():
252252
pass
253253
out.flush()
254254
# Push once, pop once — nested enter is invisible at the wire level.
255-
assert buf.getvalue() == "\x1b[=1;1u\x1b[<u"
255+
assert buf.getvalue() == "\x1b[>1u\x1b[<u"
256256

257257

258258
def test_output_pop_on_exception():
@@ -270,7 +270,7 @@ def test_output_push_is_flushed_before_yield():
270270
# stuck in the output buffer.
271271
buf, out = _buf_output()
272272
with kitty_keyboard_protocol(out):
273-
assert "\x1b[=1;1u" in buf.getvalue()
273+
assert "\x1b[>1u" in buf.getvalue()
274274

275275

276276
def test_output_nested_with_mismatched_flags_raises():

0 commit comments

Comments
 (0)