-
Notifications
You must be signed in to change notification settings - Fork 60
Images render as a large mostly-empty box with the kitty graphics protocol #140
Description
Pasting or viewing an image in slk reserves a large block of space, but the picture itself is drawn tiny in the top-left corner of it. The rest is empty background.
This is with the kitty graphics protocol in Ghostty running inside tmux.
What's behind it
First, slk works out how much space to reserve from the terminal's character cell size, which it gets from a TIOCGWINSZ ioctl. Inside tmux that can report pixel dimensions that don't match the coordinate system kitty actually renders in, so the reserved size can be wrong before anything else happens. Asking the kitty graphics protocol for the window size directly is more reliable here.
Second, and this is the bigger one: whatever cell size slk measures, the code that prepares the image to send to the terminal ignores it and assumes every cell is 8x16 pixels
pxW := target.X * 8 pxH := target.Y * 16
So slk reserves a box based on one cell size and fills it with an image built for a different, smaller one. The picture ends up covering roughly half the width and half the height of the space set aside for it, anchored top-left — which is the mostly-empty box.
Worth stressing that the problem is the disagreement between those two numbers, not either number alone. If both used 8x16 it would look fine.
Other things I ran into around the same area
- Small images get blown up. Nothing caps how far an image is scaled up, so a small one stretches to fill the maximum height. In my logs a 59x57 pixel image was given a 588x580 pixel area — roughly a 10x blowup of something with no detail to give.
- Blurry images on a high-resolution screen. Slack keeps several sizes of every picture, and slk picks the smallest one that's still big enough for the space it needs. If none of them is big enough — which happens with a Retina screenshot, where every thumbnail is smaller than the display needs — it settles for the largest and stretches it. It should fall back to the original file instead, so it's at least using the highest-resolution copy that exists.
- Slight stretching. Images are fitted to whole rows and columns, which shifts the shape a few percent from the original instead of leaving a thin margin.
- A picture from a previous session sometimes appears. Terminals remember images by number, and that memory outlives slk. Since slk starts numbering from the beginning every run, a fresh image can collide with one the terminal still has from last time. This one is intermittent and often clears on restart, which makes it confusing to chase.
- Your own pastes get downloaded back from Slack, even though slk already has the bytes it just uploaded.
Happy to send a fix
I have a branch that addresses these. It's a fair bit at once, so I'm glad to split it into smaller PRs if you'd prefer — happy to go whichever way is easier to review.
Before:
ImageAfter:
Image