Skip to content

Instantly share code, notes, and snippets.

@kuntau
Forked from wolandark/execute_all.sh
Created August 1, 2024 09:55
Show Gist options
  • Save kuntau/1b736b3cb92fddca338893886d4ff45a to your computer and use it in GitHub Desktop.
Save kuntau/1b736b3cb92fddca338893886d4ff45a to your computer and use it in GitHub Desktop.
fzf omni file preview and execute for Ctrl_t
#!/bin/bash
# MIME type of the file
mime=$(file --mime-type -b "$1" 2>/dev/null)
# Handle different MIME types
case "$mime" in
text/*)
# Use vim for text files
vim "$1"
;;
*)
# Let xdg-open handle the rest
xdg-open "$1" >/dev/null 2>&1
;;
esac
exit 0
# to be placed in ~/.bashrc
export FZF_CTRL_T_OPTS="
--preview '~/preview_all.sh {}'
--height '100%'
--bind 'ctrl-/:change-preview-window(down|hidden|)'
--bind 'enter:execute(~/execute_all.sh {+})'"
#!/bin/bash
# MIME type of the file
mime=$(file --mime-type -b "$1" 2>/dev/null)
# Cleanup function
cleanup() {
[[ -n "$thumb_cache" ]] && rm -f "$thumb_cache"
}
trap cleanup EXIT
# Handle different MIME types
case "$mime" in
text/*)
# Use bat for text files
bat --color=always --style=numbers "$1" 2>/dev/null
;;
inode/directory)
tree "$1" 2>/dev/null
;;
image/jpeg | image/png)
# Use magick to display images in the terminal using sixel
magick "$1" -geometry 800x480 sixel:- 2>/dev/null
;;
application/pdf)
# Use pdftotext for PDF files
pdftotext "$1" - | head -n 1000 2>/dev/null
;;
application/epub+zip)
# Use epub2txt for EPUB files
epub2txt "$1" | head -n 1000 2>/dev/null
;;
video/*)
# Generate a thumbnail for video files
thumb_cache=$(mktemp "${TMPDIR:-/tmp}/thumb_cache.XXXXX.png")
ffmpegthumbnailer -i "$1" -o "$thumb_cache" -s 0 2>/dev/null
# Display the thumbnail
img2sixel < "$thumb_cache" 2>/dev/null
;;
application/x-tar | application/gzip | application/x-compressed-tar | application/x-bzip2 | application/x-xz | application/zip | application/x-7z-compressed | application/x-rar-compressed)
~/.config/lf/pistol-static-linux-x86_64 "$1" 2>/dev/null
;;
*)
# Default: show file details
echo "File type: $mime"
echo "No preview available for this file type."
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment