Skip to content

Conversation

@grimwm
Copy link

@grimwm grimwm commented Jan 13, 2026

Summary

This PR fixes multiple issues with Kitty graphics protocol support, particularly for Unicode placeholder mode which enables images to scroll with terminal content.

Key changes

  1. Two-step rendering for Unicode placeholders

    • Transmit image data as PNG without display (f=100,t=d)
    • Create virtual placement with U=1 and explicit cols/rows (a=p,U=1)
    • Generate placeholder characters with image ID encoded in foreground color
    • Skip pre-resizing when using Unicode placeholders (terminal handles scaling)
  2. Fix image ID encoding

    • Add globalKittyImageNum counter for 24-bit compatible image numbers
    • Use small sequential numbers (1-0xFFFFFF) that fit in RGB encoding
  3. Fix ClearAll functions

    • Use d=A (delete ALL) instead of just a=d
    • Add missing escape sequence terminator (\x1b\\)
  4. Skip terminal queries when protocol detected from environment

    • If Kitty/iTerm2 detected via KITTY_WINDOW_ID, TERM, etc., skip all terminal queries to avoid leaving garbage in stdin buffer
    • Fixes first-keypress issues with TUI frameworks like bubbletea
  5. Fix TOCTOU race condition in ResizeCache

    • Use atomic file operations to prevent race conditions

Test plan

  • Verified images display at correct size (not thumbnails)
  • Verified single 'q' press exits cleanly (no leftover images)
  • Verified first keypress registers immediately in TUI apps
  • Verified images scroll with terminal content
  • All existing tests pass

This commit fixes multiple issues with Kitty graphics protocol support,
particularly for Unicode placeholder mode which enables images to scroll
with terminal content.

Key changes:

1. Two-step rendering for Unicode placeholders
   - Transmit image data as PNG without display (f=100,t=d)
   - Create virtual placement with U=1 and explicit cols/rows (a=p,U=1)
   - Generate placeholder characters with image ID in foreground color
   - Skip pre-resizing when using Unicode placeholders (terminal scales)

2. Fix image ID encoding
   - Add globalKittyImageNum counter for 24-bit compatible image numbers
   - Use small sequential numbers (1-0xFFFFFF) that fit in RGB encoding

3. Fix ClearAll functions
   - Use d=A (delete ALL) instead of just a=d
   - Add missing escape sequence terminator (\x1b\\)

4. Skip terminal queries when protocol detected from environment
   - If Kitty/iTerm2 detected via KITTY_WINDOW_ID, TERM, etc., skip all
     terminal queries to avoid leaving garbage in stdin buffer
   - Fixes first-keypress issues with TUI frameworks like bubbletea

5. Fix TOCTOU race condition in ResizeCache
   - Use atomic file operations to prevent race conditions
@grimwm
Copy link
Author

grimwm commented Jan 14, 2026

@blacktop may I ask if there is somebody that can take a look at this? I have tested it on my end and can see that images scroll, but I would love to have a more complete review in case I need to make some changes.

@blacktop
Copy link
Owner

Apologies for being slow to respond; looking this it looks good, just trying to find the time to give it a thorough review.

@grimwm
Copy link
Author

grimwm commented Jan 18, 2026

Apologies for being slow to respond; looking this it looks good, just trying to find the time to give it a thorough review.

Oh, no worries. I understand the issues with finding time. I was just making sure the project wasn't stale. I know people lose focus (like me).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants