Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gif render issues on terminal while using with fzf #244

Open
UtsavBalar1231 opened this issue Jan 22, 2025 · 6 comments
Open

gif render issues on terminal while using with fzf #244

UtsavBalar1231 opened this issue Jan 22, 2025 · 6 comments

Comments

@UtsavBalar1231
Copy link

UtsavBalar1231 commented Jan 22, 2025

I have been trying to find a way to get chafa to work alongside as a image previewer for setting wallpaper. My wallpapers dir contains valid image formats 'png/jpg/gif'. While the static image preview is fine, during gif preview it just vomits on the terminal.

chafa-fzf-reduced.mp4

I have tested many different alternatives such as imgcat, kitty's icat and ueberzugpp. where ueberzugpp is the only one who can render gif preview without any terminal buffer abuse. Kitty and imgcat does not seem to support animated previews.

I have seen many people just putting the --animate off argument. but I would like to see if this can be fixed or hacked in some sense. Because there is nothing faster than chafa on the whole internet afaik.

Snippet from my wallpaper tool 
set_selected_wallpaper() {
	local wallpaper_dir
	wallpaper_dir="$(get_wallpaper_dir)"
	if [ -z "$wallpaper_dir" ]; then
		log_message "ERROR" "Wallpaper directory not found."
		return 1
	fi

	# Check if fzf is installed
	if ! command -v fzf >/dev/null; then
		log_message "ERROR" "fzf is not installed. Cannot select a wallpaper."
		return 1
	fi

	# Use fzf to select a wallpaper
	local preview_cmd
	if [[ -n $KITTY_WINDOW_ID && "$(get_battery_status)" == "Discharging" ]]; then
		preview_cmd="kitty icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --place=\${FZF_PREVIEW_COLUMNS}x\${FZF_PREVIEW_LINES}@0x0 {}"
	elif command -v chafa >/dev/null; then
		preview_cmd="chafa --clear -s \${FZF_PREVIEW_COLUMNS}x\${FZF_PREVIEW_LINES} {}"
	elif command -v viu >/dev/null; then
		preview_cmd="viu {}"
	else
		log_message "WARNING" "No image preview tool (kitty icat, viu, or chafa) is installed. Preview will not be available."
		preview_cmd="echo 'Preview not available {}'"
	fi

	# Use fzf to select a wallpaper with image preview
	local selected_wallpaper
	selected_wallpaper=$(
		find "$wallpaper_dir" -type f |
			fzf --reverse --border \
				--preview "$preview_cmd" \
				--preview-window=top:75%:wrap
	)
	if [ -z "$selected_wallpaper" ]; then
		log_message "WARNING" "No wallpaper selected."
		return 0
	fi

	# Stop the slideshow and set the selected wallpaper
	stop_slideshow
	if [[ "$selected_wallpaper" == *.gif ]]; then
		log_message "INFO" "Animating GIF wallpaper: $selected_wallpaper"
		animate_gif_wallpaper "$selected_wallpaper"
	else
		log_message "INFO" "Setting static wallpaper: $selected_wallpaper"
		set_wallpaper_fallback "$selected_wallpaper"
	fi

	set_slideshow_status "paused"
	log_message "INFO" "Selected wallpaper set: $selected_wallpaper"
}
Expected behaviour:
uerberzugpp-fzf-reduced.mp4
Environment:
OS   : ArchLinux (6.12.10)
PROC : Intel(R) Core(TM) i7-14700HX
TERM : kitty (0.39.0)
WM   : Hyprland (0.46.0)
@AnonymouX47
Copy link

AnonymouX47 commented Jan 22, 2025

Looks to me like a cursor positioning issue.

Why it happens, I'm not sure but fzf might have something to do with it cos last I checked, cursor positioning works well in your environment (i.e in the bare TE). So, I can't really suggest a solution.

A possible hack that comes to mind right now is to intercept chafa's output and modify/replace the CUU (Cursor Up) sequences emitted. Something like:

chafa --duration inf ... | sed -E -e s/\x1b\[[0-9]+A/\x1b[${PREVIEW_HEIGHT}A/

The PREVIEW_HEIGHT placeholder is where the main thing happens. You may play around with that till you get something that works.

Though I think it's more likely to be successful if the sequence were entirely replaced; seeing your fzf interface is full-screen and uses the alternate buffer (non-scrolling), you'd have a better chance of getting things to work by replacing the CUU sequence all together, as in:

  1. By saving and restoring the cursor position e.g:

    tput sc && chafa --duration inf ... | sed -E -e s/\x1b\[[0-9]*A/\x1b8/
  2. Absolute cursor positioning e.g

    chafa --duration inf ... | sed -E -e s/\x1b\[[0-9]*A/\x1b[2;2H/

    I'm not sure what the exact row and column positions should be, so you might have to tweak that. It should be the screen position of the top left cell of the fzf preview pane.

* Take note of the --duration inf option. Without it, animations will play through only once due to the piped output. By the way, it'll also cause chafa to stall after outputting still images but I'm not sure that should be much of an issue since I expect fzf to terminate the previewer process before it starts to preview another entry. See the option in the man page for more info.


I should also mention that ueberzugpp behaves as it does because it uses overlay windows instead of actual terminal graphics as chafa does but that comes with its own caveats, being a hack itself.

@hpjansson
Copy link
Owner

Thanks, @AnonymouX47.

Also, --relative on or --relative off could have an effect. Failing that, we should find out which seqs are failing in fzf exactly, and look for a better workaround.

@UtsavBalar1231
Copy link
Author

UtsavBalar1231 commented Jan 23, 2025

Thanks, @AnonymouX47.

Also, --relative on or --relative off could have an effect. Failing that, we should find out which seqs are failing in fzf exactly, and look for a better workaround.

--relative on seems to work with an tiny annoying issue, its horribly slow now at rendering on the fzf preview.

@hpjansson
Copy link
Owner

--relative on seems to work with an tiny annoying issue, its horribly slow now at rendering on the fzf preview.

Is fzf bottlenecking it (100% in top), or something else?

@UtsavBalar1231
Copy link
Author

UtsavBalar1231 commented Jan 24, 2025

--relative on seems to work with an tiny annoying issue, its horribly slow now at rendering on the fzf preview.

Is fzf bottlenecking it (100% in top), or something else?

Does not seems like it, fzf processing stays normal.

Image

  • Even chafa is not abusing cycles, which is weird

Image

@UtsavBalar1231
Copy link
Author

@hpjansson this is a test for you to see on your computer

https://drive.google.com/file/d/1mjbF5w2T_3z_rdjAKmfkQMNQtY1-APNh/view?usp=drivesdk

unzip and run ./wallpaper-slideshow set

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

No branches or pull requests

3 participants