From ce8d8770311dcc15c2506b5bd4c291b54ff498de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Alonso?= <242080768+alvaroalonsoparrondo-a11y@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:56:55 +0100 Subject: [PATCH] fix(graphics): decide a kitty placement's cursor move by P= and U= values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #844 Signed-off-by: Álvaro Alonso <242080768+alvaroalonsoparrondo-a11y@users.noreply.github.com> --- CHANGELOG.md | 6 ++++++ crates/termlens/src/emu/vt100.rs | 2 +- crates/termlens/src/graphics.rs | 24 +++++++++++++++++++++--- crates/termlens/tests/graphics.rs | 27 +++++++++++++++++++++++++++ docs/DESIGN.md | 2 +- 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 710021d2..849c83a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,12 @@ reads that marker. ### Fixed +- A kitty placement decides whether it moves the cursor by the values of + `U=` and `P=`, as kitty does. `P=0` names no parent, so it moves the + cursor, and any nonzero `U=` makes the placement virtual, so it does + not; termlens took `P=` by its presence and `U=` only at 1, so the text + a client wrote after `P=0` landed on the image. (#844) + - Both published crates, `termlens` and `termlens-cli`, ship `LICENSE-MIT` and `LICENSE-APACHE`. They are `MIT OR Apache-2.0`, and both licences ask for their text in every copy, but the texts sat only at the repository diff --git a/crates/termlens/src/emu/vt100.rs b/crates/termlens/src/emu/vt100.rs index 9269d52c..0587068c 100644 --- a/crates/termlens/src/emu/vt100.rs +++ b/crates/termlens/src/emu/vt100.rs @@ -165,7 +165,7 @@ impl Vt100Emulator { /// /// kitty moves it right by the placement's columns and down by its rows /// less one — onto the image's last row, one column past it — for a - /// placement that asks (no `C=1`, `U=1` or `P=`). xterm, with sixel + /// placement that asks (no `C=1`, nonzero `U=` or nonzero `P=`). xterm, with sixel /// scrolling on as it is by default, moves it down to the image's last /// text row and leaves the column. Each ignores the other's escape. fn image_step(&self, payload: &GraphicsPayload, placement: &Placement) -> Option { diff --git a/crates/termlens/src/graphics.rs b/crates/termlens/src/graphics.rs index d0f90f16..458eb7f7 100644 --- a/crates/termlens/src/graphics.rs +++ b/crates/termlens/src/graphics.rs @@ -1205,8 +1205,9 @@ pub(crate) struct Placement { pub(crate) cell_x: u32, pub(crate) cell_y: u32, /// kitty: whether a placement moves the cursor at all. `C=1` asks it not - /// to, and neither a Unicode-placeholder placement (`U=1`) nor one - /// relative to another image (`P=`) moves it. + /// to, and neither a Unicode-placeholder placement (a nonzero `U=`) nor + /// one relative to another image (a nonzero `P=`) moves it. kitty reads + /// both by value, so `P=0` and `U=0` name no parent and no placeholder. pub(crate) moves: bool, /// sixel: the image's height in pixels as xterm measures it, the larger /// of the raster attributes' and the painted rows'. @@ -1317,7 +1318,7 @@ impl GraphicsBuilder { src_y: value(b"y"), cell_x: value(b"X"), cell_y: value(b"Y"), - moves: value(b"C") != 1 && value(b"U") != 1 && key(control, b"P").is_none(), + moves: value(b"C") != 1 && value(b"U") == 0 && value(b"P") == 0, height: 0, forgets: None, number: value(b"I"), @@ -1770,6 +1771,23 @@ mod tests { } } + /// kitty decides by value: `P=0` names no parent and `U=0` no + /// placeholder, so both move the cursor, while any nonzero `U=` makes + /// the placement virtual (`handle_put_command`, #844). + #[test] + fn p_and_u_decide_the_cursor_move_by_value() { + let moves = |control: &[u8]| { + let mut builder = GraphicsBuilder::default(); + builder.kitty(control); + builder.chunk(0, b"", true, usize::MAX); + builder.finish_placed().expect("a payload").1.moves + }; + assert!(moves(b"a=T,c=3,r=2,P=0")); + assert!(moves(b"a=T,c=3,r=2,U=0")); + assert!(!moves(b"a=T,c=3,r=2,U=2")); + assert!(!moves(b"a=T,c=3,r=2,P=1")); + } + /// What a delete frees, as kitty's `handle_delete_command` frees it: /// only the upper-case deletes by id say so on their own, and only /// without `p=` (#672). Every other escape forgets nothing. diff --git a/crates/termlens/tests/graphics.rs b/crates/termlens/tests/graphics.rs index d00e1574..8826e859 100644 --- a/crates/termlens/tests/graphics.rs +++ b/crates/termlens/tests/graphics.rs @@ -335,3 +335,30 @@ fn a_declared_kitty_terminal_moves_the_cursor_past_a_placement() -> termlens::Re } Ok(()) } + +/// kitty reads `P=` and `U=` by value: `P=0` names no parent, so the +/// placement moves the cursor, and `U=2` is a virtual placement, which does +/// not (#844). +#[test] +#[cfg_attr( + windows, + ignore = "ConPTY does not forward kitty or sixel payloads (#149)" +)] +fn p_and_u_decide_the_cursor_move_by_value() -> termlens::Result<()> { + for (key, expected) in [("P=0", (1, 3)), ("U=2", (0, 0))] { + let escape = format!(r"\e_Ga=T,f=24,s=1,v=1,c=3,r=2,{key};ESIz\e\\X"); + let mut t = util::spawn_emit( + Terminal::builder() + .size(20, 5) + .timeout(Duration::from_secs(30)) + .graphics(termlens::Graphics::Kitty) + .cell_size(10, 20), + &["--raw", escape.as_str(), "--wait"], + )?; + t.wait_until(|s| s.contains("X"))?; + assert_eq!(t.screen().find("X"), Some(expected), "{key}"); + t.send(termlens::Key::Enter)?; + t.wait_exit()?; + } + Ok(()) +} diff --git a/docs/DESIGN.md b/docs/DESIGN.md index 349e20a1..945f5ef6 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -925,7 +925,7 @@ Rules: declared (`graphics()`, with `cell_size()`; #588): kitty puts it on the placement's last row, one column past its right edge, wrapping to the next row past the screen's and scrolling past the bottom margin, unless - `C=1`, `U=1` or `P=` says otherwise; xterm, with sixel scrolling on as it + `C=1`, a nonzero `U=` or a nonzero `P=` says otherwise; xterm, with sixel scrolling on as it is by default, puts it on the image's last text row and leaves the column. A terminal declared without the protocol ignores the escape and moves nothing, which is the default. The cursor is moved with line feeds