Images stack on each other
I'm basically using chafa as fzf preview for some script of mine. But there is the issue of image staking on one another.
Here is the code snippet
selected_files="$(fzf --multi --height=50% --layout=reverse --preview-window=right:60%:wrap \
--preview='
mimetype=$(file --mime-type -b {})
case "$mimetype" in
image/*)
chafa --clear --view-size 22x40 {}
;;
text/*)
batcat --style=numbers --color=always --line-range :500 {}
;;
*)
echo "No preview available"
;;
esac
'
)"
here the output looks like this in my suckless st
https://github.com/user-attachments/assets/d7503d6e-1132-4d73-83f0-b23524ed6c49
In st there is no over staking of pictures (picture previews are rather cancerous here though LOL. (Just kidding, Great Project)) & the hello text is also visible. Also when closed the preview also vanishes as expected.
But with kitty its a different story
https://github.com/user-attachments/assets/9e6bce25-79d4-444d-8667-2b14eacae0af
Here the pictures stack even after --clear flag is added. And the hello text in the end also gets covered up by the stacked previews. And the pictures stay in that same place even after script is finished or overruled.
Same story with Konsole.
Is there any solution to this? I can use it with st (it does not have any graphics patch applied) but if you give me a snippet to make it look a little better it'll be damn helpful.
Thank You in advance for this great project. (Love it and Use it a lot)
CHAFA VERSION: Chafa version 1.14.0
Loaders: AVIF GIF JPEG PNG QOI SVG TIFF WebP XWD Features: mmx sse4.1 popcnt avx2 Applying: mmx sse4.1 popcnt avx2
OS INFO: Linux Mint 22.1
I Just tested the same code snippet with foot terminal (waylad) (Uses sixel graphics) and it behaved just like I wanted. There were no restacking, the hello text also showed. no jitter, and when I terminated that script there were no hanging preview in the middle of the screen. So I guess the sixel using terminals are just fine (At least in wayland I guess).
But I don't know if Konsole use sixel or not, there were wanky behaviour there. like Kitty or other in wayland also.
TL;DR: Only foot in wayland is not having this bug. Others have
And Mint still does not support wayland screenrecording; that's why only verbal proof is provided (Trust me man ;) )
I trust you :-)
This is likely due to Kitty images persisting after exit, and there is no way for the application to clear the image area without knowing the image ID. With sixels you can overwrite with spaces, and previewers do this.
We have at least two possibilities:
- Add a way to specify the image ID to use, causing the previous image with the same ID to be retired.
- Allow filling the entire view area with the background color.
The first option sounds better to me, especially since we already track Kitty image IDs between invocations.
Edit:
Having refreshed my memory, there are actually more ways to retire a Kitty image, but you have to do it explicitly using its protocol: https://sw.kovidgoyal.net/kitty/graphics-protocol/#deleting-images
So we could e.g. issue a "delete placements at position" or "delete all placements" (with --clear, perhaps?).
See also #219.
Can't make it work. Help
This should solve your issue:
echo -en "\e_Ga=d,d=C;\e\\"; chafa ...
In the long run, we'll look into implementing something like what @hpjansson suggested earlier.
This should solve your issue:
echo -en "\e_Ga=d,d=C;\e\\"; chafa ...
No; It remains the same; thanks for help though.
Interesting π€...
Use d=A in place of d=C. This should definitely work but I was being careful initially because it deletes all images on the terminal screen.
One more thing... The image delete command will have to be executed before every preview command (not just the one for images), so it should be somewhere before the case statement in your script. It should like:
echo -en "\e_Ga=d,d=A\e\\"
mimetype=...
case ...
Doing this a bit different thing happen.
The Image stacking is still there in kitty i.e the images are staking on each other but when moved to the Text file the text showed fine and the images are not there.
TL;DR --> Images still stacks on each other and preview of non m=image files are good.
Do you mind sharing your current script and a recording showing the behavior?
Yeah basically the code is same though.
selected_files="$(fzf --bind='alt-j:down,alt-k:up' --multi --height=50% --layout=reverse --preview-window=right:60%:wrap \
--preview='
echo -en "\e_Ga=d,d=A\e\\"
mimetype=$(file --mime-type -b {})
case "$mimetype" in
image/*)
case "$TERM" in
xterm-kitty)
echo -en "\e_Ga=d,d=C;\e\\"; chafa -f kitty --clear --view-size 22x60 {}
;;
*)
chafa --clear --view-size 22x40 {}
;;
esac
;;
text/*)
batcat --style=numbers --color=always {}
;;
*)
echo "No preview available"
;;
esac
'
)"
It is a basic script which I impose on different other scripts for more functionality.
And for the screen recording, Linux Mint does not let screen to be recorded in any wayland session. The Foot preview is just fine and expected. (just like the first video (of st terminal) (with clear view of the pictures) above in the parent query) The kitty preview will be posted when I switch to a X11 session, may take some time but basically no change at all though.
I appreciate your patience. I'll post that recording surely in some time.
I see. I'll gladly wait.
You're welcome.
Apologies for the delay from my end. Appreciate your patience. Nevertheless here is the recording of kitty terminal Emulator with the last script.
https://github.com/user-attachments/assets/80d911ff-451a-4635-a98e-a2e56580728b
Apologies for the delay from my end. Appreciate your patience.
It's totally okay. π
Nevertheless here is the recording of kitty terminal Emulator with the last script.
Hmm... Interesting and confusing. I really wish I could test on my end but unfortunately, I can't for now.
For the last time (hopefully), would you mind trying this (just tweaked and corrected a few things):
selected_files="$(fzf --bind='alt-j:down,alt-k:up' --multi --height=50% --layout=reverse --preview-window=right:60%:wrap \
--preview='
echo -en "\e_Ga=d,d=A;\e\\"
mimetype=$(file --mime-type -b {})
case "$mimetype" in
image/*)
case "$TERM" in
xterm-kitty)
chafa -f kitty --clear --view-size 22x60 {}
;;
*)
chafa --clear --view-size 22x40 {}
;;
esac
;;
text/*)
batcat --style=numbers --color=always {}
;;
*)
echo "No preview available"
;;
esac
'
)"
It is not working either. Just like the last recording. behaviour is same. But thanks a lot for helping me this far. And you can try it on our own machine (in free time ofc.). still works great with sixel format.
Works also good with the sixel (experimental) of Black Box. Kitty is no good. As ghostty also uses the same protocol it behaves the same there also.
I'm not overly familiar with fzf, but just from briefly looking at the code, it doesn't appear to use the output of the preview script as is - it does some post processing where it separates text output from image output. That way it can reformat the text to fit the preview window, but the image output is just passed through to the terminal directly.
My guess is that something like chafa's --clear would be filtered out by this process, otherwise it would completely destroy the output of the app. And perhaps the extra kitty delete sequence gets lost in the same way.
Terminals that use Sixel or the iTerm image protocol wouldn't have this problem because they use a more conventional method of deletion (i.e. just overwriting an image with something else will delete it). Assumedly fzf would be doing that at the start of each new preview regardless of the type of file being previewed.
So you may find the only way to get Kitty to work is with Unicode placeholders, which behave more like Sixel and IIP. Although that significantly limits the number of KGP terminals you can support.
So we could e.g. issue a "delete placements at position" or "delete all placements" (with
--clear, perhaps?).
In my experience, the only delete operation that worked reliably on all KGP terminals was delete by id (i.e. d=i), but you may have better luck if you only need to support a few of them.
Thanks for the description. But I think that it will not totally solve the image stacking problem in kitty terminal (so to say kitty graphics protocol). I think chafa has a robust implementation of Sixel which is better than kitty at this moment; in terms of image stacking and image cleaning afterwards. At least that is my guess for now (May be anything more technical that I just don't know.)
I'm not overly familiar with fzf, but just from briefly looking at the code, it doesn't appear to use the output of the preview script as is - it does some post processing where it separates text output from image output. That way it can reformat the text to fit the preview window, but the image output is just passed through to the terminal directly.
Yes quite right. I planned to do this likewise. Images should show there preview in respective graphics protocol (like kitty, sixel, and more to add) and text preview will be by bat only. I intend to do this in a way that base on the mime-type of a file it will trigger the previewing snippet (i.e. for image type kitty, sixel, and for text type bat).
I have also made a video mime type that will generate thumbnail with ffmpegthumbnailer. Though it is not that optimized right now. Some tweaks are to be made in that space. But the image previewing is quite done only slight hick-ups are there in regard to some protocols.
I tested wezterm also worked really fine
I don't know what does wezterm use under the hood but chafa works fabulous there.
I don't know what does wezterm use under the hood but chafa works fabulous there.
Chafa defaults to sixels on WezTerm. I think its KGP implementation has matured since, so we may want to revisit that, though.