update fzfclip

This commit is contained in:
2026-02-11 07:46:16 +01:00
parent d56cc0e4bf
commit 3173166ae0
3 changed files with 78 additions and 55 deletions
+72 -51
View File
@@ -38,9 +38,18 @@
set -euo pipefail
_cleanup() {
if [ -n "${CACHE_DIR:-}" ] && [ -d "$CACHE_DIR" ]; then
rm -rf "$CACHE_DIR"
fi
if [ -n "${WATCH_PID:-}" ]; then
kill "$WATCH_PID" 2>/dev/null || true
fi
}
trap _cleanup EXIT
CACHE_DIR=$(mktemp -d)
export CACHE_DIR
# trap rm later
export C_TERTIARY='\x1b[1;35m'
export C_PRIMARY='\x1b[1;34m'
@@ -91,10 +100,16 @@ export ENABLE_ITERM2
# Preview functions
_clear_preview() {
if [ "$ENABLE_ICAT" -eq 1 ]; then
printf "\x1b_Ga=d\x1b\\"
fi
}
export -f _clear_preview
_preview_image() {
local file="$1"
if [ "$ENABLE_ICAT" -eq 1 ]; then
printf "\x1b_Ga=d\x1b\\"
chafa -f kitty --size="${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}" "$file"
elif [ "$ENABLE_SIXEL" -eq 1 ]; then
chafa -f sixels --size="${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}" "$file"
@@ -108,40 +123,61 @@ export -f _preview_image
_preview_text() {
local content="$1"
if [ "$ENABLE_ICAT" -eq 1 ]; then
printf "\x1b_Ga=d\x1b\\"
fi
echo "$content" | head -n 100
printf "%s" "$content" | head -n 100
}
export -f _preview_text
_preview_video() {
local video_hash, thumb_file, path
path="$1"
video_hash=$(echo "$path" | md5sum | cut -d" " -f1)
thumb_file="$CACHE_DIR/$video_hash.png"
if [ ! -f "$thumb_file" ]; then
if type ffmpegthumbnailer &>/dev/null; then
ffmpegthumbnailer -i "$path" -o "$thumb_file" -s 480 -t 0 >/dev/null 2>&1
else
_preview_text "ffmpegthumbnailer not installed, cannot generate thumbnail for video."
fi
fi
if [ -s "$thumb_file" ]; then
_preview_image "$thumb_file"
else
_preview_text "Video: $path (No thumbnail)"
fi
}
export -f _preview_video
# Kinda buggy right now
# _preview_audio() {
# local path="$1"
# if type mpv &>/dev/null; then
# echo "Playing audio: $path" >&2
# exec mpv --no-video --keep-open=no --loop-file=no --loop-playlist=no "$path" &>/dev/null
# else
# _preview_text "Audio: $path"
# fi
# }
# export -f _preview_audio
_preview_file() {
path="$1"
path_mime=$(file -b --mime-type "$path")
if [[ $path_mime =~ image ]]; then
_preview_image "$path"
elif [[ "$path_mime" =~ video ]]; then
video_hash=$(echo "$path" | md5sum | cut -d" " -f1)
thumb_file="$CACHE_DIR/$video_hash.png"
if [ ! -f "$thumb_file" ]; then
if command -v ffmpegthumbnailer &>/dev/null; then
ffmpegthumbnailer -i "$path" -o "$thumb_file" -s 480 -t 0 >/dev/null 2>&1
else
_preview_text "ffmpegthumbnailer not installed, cannot generate thumbnail for video."
fi
fi
if [ -s "$thumb_file" ]; then
_preview_image "$thumb_file"
else
_preview_text "Video: $path (No thumbnail)"
fi
_preview_video "$path"
# elif [[ "$path_mime" =~ audio ]]; then
# _preview_audio "$path"
else
_preview_text "$path"
fi
}
export -f _preview_file
preview() {
_clear_preview
entry="$1"
content=$(echo "$entry" | cut -f2-)
@@ -154,31 +190,31 @@ preview() {
[ -f "$cache_file" ] || echo "$entry" | cliphist decode >"$cache_file"
_preview_image "$cache_file"
elif [ "$mimeType" = "text/html" ] && echo "$content" | grep -q QQ; then
qq_img_file=$(echo "$entry" | cliphist decode | grep -oP "^<img src=\"file://\K[^\"]+")
#qq_ext="${qq_img_file##*.}"
#qq_img_cache_file=$CACHE_DIR/$id.$qq_ext
#cp $qq_img_file $qq_img_cache_file
if [ -f "$qq_img_file" ]; then
_preview_image "$qq_img_file"
else
_preview_text "$qq_img_file does not exist."
fi
elif path=$(echo "$entry" | cliphist decode) && [[ "$path" == /* ]]; then
if [ -e "$path" ]; then
_preview_file "$path"
else
_preview_text "$path does not exist."
fi
elif decoded=$(echo "$entry" | cliphist decode) && [[ "$decoded" == file://* ]]; then
raw_path="${decoded#file://}"
raw_path=$(echo "$raw_path" | python -c "import sys, urllib.parse; print(urllib.parse.unquote(sys.stdin.read().strip()))")
if [ -e "$raw_path" ]; then
_preview_file "$raw_path"
paths=()
for path in $decoded; do
raw_path="${path#file://}"
raw_path=$(echo "$raw_path" | python -c "import sys, urllib.parse; print(urllib.parse.unquote(sys.stdin.read().strip()))")
paths+=("$raw_path")
done
if [ "${#paths[@]}" -eq 1 ] && [ -e "${paths[0]}" ]; then
_preview_file "${paths[0]}"
else
_preview_text "$raw_path does not exist."
text="Multiple files:"$'\n'
for p in "${paths[@]}"; do
text+="$p"$'\n'
done
_preview_text "$text"
fi
else
if [ "$ENABLE_ICAT" = 1 ]; then
printf "\x1b_Ga=d\x1b\\"
@@ -228,20 +264,6 @@ copy_selection() {
if [[ "$mime" =~ image ]]; then
echo "$decoded" | wl-copy
# HTML with QQ image
elif [ "$mime" = "text/html" ]; then
local qq_src
qq_src=$(echo "$decoded" | grep -oP "^<img src=\"file://\K[^\"]+")
if [ -f "$qq_src" ]; then
local encoded_path
encoded_path=$(url_encode "$qq_src")
echo "file://$encoded_path" | wl-copy --type text/uri-list
else
echo "$decoded" | wl-copy
fi
# URL starting with file://
elif [[ "$decoded" == file://* ]]; then
echo "$decoded" | wl-copy --type text/uri-list
@@ -265,7 +287,6 @@ FZF_PORT=$(shuf -i 10000-60000 -n 1)
RELOAD_CMD="cliphist list | format_clip_list | add_num"
wl-paste --watch bash -c "curl -s -X POST -d 'reload($RELOAD_CMD)' http://localhost:$FZF_PORT" >/dev/null 2>&1 &
WATCH_PID=$!
trap 'rm -rf "$CACHE_DIR"; kill $WATCH_PID 2>/dev/null' EXIT
# Ensure terminal is large enough
+3 -3
View File
@@ -45,13 +45,13 @@ mp4_offset=$((offset - 3))
tail -c "+$mp4_offset" "$file" > "$tmp_video"
play_cmd=()
if command -v mpv >/dev/null 2>&1; then
if type mpv >/dev/null 2>&1; then
play_cmd=(mpv --title="Live Photo View: $file" --keep-open=yes --loop-file=yes --loop-playlist=no --idle=yes)
elif command -v vlc >/dev/null 2>&1; then
elif type vlc >/dev/null 2>&1; then
play_cmd=(vlc --meta-title="Live Photo View: $file")
else
echo "Error: No suitable media player found." >&2
exit 1
fi
"${play_cmd[@]}" "$tmp_video"
"${play_cmd[@]}" "$tmp_video"