From 60b74bee184c7b7d4c1e58ecf473495ea243111e Mon Sep 17 00:00:00 2001 From: benexl Date: Wed, 29 Jan 2025 18:47:12 +0300 Subject: [PATCH] feat: update the preview script --- fastanime/cli/config.py | 5 ++ fastanime/cli/utils/scripts.py | 85 ++++++++++++++++------------------ 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/fastanime/cli/config.py b/fastanime/cli/config.py index bde807b..9e5aeb1 100644 --- a/fastanime/cli/config.py +++ b/fastanime/cli/config.py @@ -56,6 +56,7 @@ class Config(object): "format": "best[height<=1080]/bestvideo[height<=1080]+bestaudio/best", "icons": "false", "image_previews": "True" if S_PLATFORM != "win32" else "False", + "image_renderer": "icat" if os.environ.get("KITTY_WINDOW_ID") else "chafa", "normalize_titles": "True", "notification_duration": "120", "max_cache_lifetime": "03:00:00", @@ -132,6 +133,7 @@ class Config(object): self.header_ascii_art = self.configparser.get("general", "header_ascii_art") self.icons = self.configparser.getboolean("general", "icons") self.image_previews = self.configparser.getboolean("general", "image_previews") + self.image_renderer = self.configparser.get("general", "image_renderer") self.normalize_titles = self.configparser.getboolean( "general", "normalize_titles" ) @@ -290,6 +292,9 @@ header_ascii_art = {new_line.join([tab + line for line in self.header_ascii_art. header_color = {self.header_color} +# the image renderer to use [icat/chafa] +image_renderer = {self.image_renderer} + # To be passed to fzf # Be sure to indent fzf_opts = {new_line.join([tab + line for line in self.fzf_opts.split(new_line)])} diff --git a/fastanime/cli/utils/scripts.py b/fastanime/cli/utils/scripts.py index da6b1be..57f81a0 100644 --- a/fastanime/cli/utils/scripts.py +++ b/fastanime/cli/utils/scripts.py @@ -1,53 +1,46 @@ fzf_preview = r""" -# -# Adapted from the preview script in the fzf repo -# -# Dependencies: -# - https://github.com/hpjansson/chafa -# - https://iterm2.com/utilities/imgcat -# -fzf-preview() { - file=${1/#\~\//$HOME/} - dim=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES} - if [[ $dim = x ]]; then - dim=$(stty size /dev/null; then - case "$(uname -a)" in - # termux does not support sixel graphics - # and produces weird output - *ndroid*) chafa -s "$dim" "$file";; - *) chafa -f sixel -s "$dim" "$file";; - esac - # Add a new line character so that fzf can display multiple images in the preview window - echo + if [ "$FASTANIME_IMAGE_RENDERER" = "icat" ] && [ -z "$GHOSTTY_BIN_DIR" ]; then + if command -v kitten >/dev/null 2>&1; then + kitten icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --place="$dim@0x0" "$file" | sed "\$d" | sed "$(printf "\$s/\$/\033[m/")" + elif command -v icat >/dev/null 2>&1; then + icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --place="$dim@0x0" "$file" | sed "\$d" | sed "$(printf "\$s/\$/\033[m/")" + else + kitty icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --place="$dim@0x0" "$file" | sed "\$d" | sed "$(printf "\$s/\$/\033[m/")" + fi - # 3. If chafa is not found but imgcat is available, use it on iTerm2 - elif command -v imgcat >/dev/null; then - # NOTE: We should use https://iterm2.com/utilities/it2check to check if the - # user is running iTerm2. But for the sake of simplicity, we just assume - # that's the case here. - imgcat -W "${dim%%x*}" -H "${dim##*x}" "$file" + elif [ -n "$GHOSTTY_BIN_DIR" ]; then + if command -v kitten >/dev/null 2>&1; then + kitten icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --place="$dim@0x0" "$file" | sed "\$d" | sed "$(printf "\$s/\$/\033[m/")" + elif command -v icat >/dev/null 2>&1; then + icat --clear --transfer-mode=memory --unicode-placeholder --stdin=no --place="$dim@0x0" "$file" | sed "\$d" | sed "$(printf "\$s/\$/\033[m/")" + else + chafa -s "$dim" "$file" + fi + elif command -v chafa >/dev/null 2>&1; then + case "$PLATFORM" in + android) chafa -s "$dim" "$file" ;; + windows) chafa -f sixel -s "$dim" "$file" ;; + *) chafa -s "$dim" "$file" ;; + esac + echo - # 4. Cannot find any suitable method to preview the image - else - echo install chafa or imgcat or install kitty terminal so you can enjoy image previews - fi + elif command -v imgcat >/dev/null; then + imgcat -W "${dim%%x*}" -H "${dim##*x}" "$file" + + else + echo please install a terminal image viewer + echo either icat for kitty terminal and wezterm or imgcat or chafa + fi } """