Skip to content

Instantly share code, notes, and snippets.

@Konfekt
Last active September 27, 2024 19:37
Show Gist options
  • Save Konfekt/c3ccd2ce83804103d8ac77b2e863f532 to your computer and use it in GitHub Desktop.
Save Konfekt/c3ccd2ce83804103d8ac77b2e863f532 to your computer and use it in GitHub Desktop.
scope.sh for text preview of office documents in ranger file manager (using .mailcap file from https://gist.github.com/Konfekt/9797372146e65a70a44c1e24a35ae0a2)
#!/usr/bin/env bash
# Uses ~/.mailcap from https://gist.github.com/Konfekt/9797372146e65a70a44c1e24a35ae0a2
# to read binary files
set -o noclobber -o noglob -o nounset -o pipefail
IFS=$'\n'
## If the option `use_preview_script` is set to `true`,
## then this script will be called and its output will be displayed in ranger.
## ANSI color codes are supported.
## STDIN is disabled, so interactive scripts won't work properly
## This script is considered a configuration file and must be updated manually.
## It will be left untouched if you upgrade ranger.
## Because of some automated testing we do on the script #'s for comments need
## to be doubled up. Code that is commented out, because it's an alternative for
## example, gets only one #.
## Meanings of exit codes:
## code | meaning | action of ranger
## -----+------------+-------------------------------------------
## 0 | success | Display stdout as preview
## 1 | no preview | Display no preview at all
## 2 | plain text | Display the plain content of the file
## 3 | fix width | Don't reload when width changes
## 4 | fix height | Don't reload when height changes
## 5 | fix both | Don't ever reload
## 6 | image | Display the image `$IMAGE_CACHE_PATH` points to as an image preview
## 7 | image | Display the file directly as an image
## Script arguments
FILE_PATH="${1}" # Full path of the highlighted file
# PV_WIDTH="${2}" # Width of the preview pane (number of fitting characters)
# PV_HEIGHT="${3}" # Height of the preview pane (number of fitting characters)
IMAGE_CACHE_PATH="${4}" # Full path that should be used to cache image preview
PV_IMAGE_ENABLED="${5}" # 'True' if image previews are enabled, 'False' otherwise.
# FILE_EXTENSION="${FILE_PATH##*.}"
# FILE_EXTENSION_LOWER="$(printf "%s" "${FILE_EXTENSION}" | tr '[:upper:]' '[:lower:]')"
MIMETYPE="$(file --dereference --brief --mime-type -- "${FILE_PATH}")"
# Image previews, if enabled in ranger.
if [ "$PV_IMAGE_ENABLED" = "True" ]; then
case "$MIMETYPE" in
# Image previews for SVG files, disabled by default.
###image/svg+xml)
### convert "$FILE_PATH" "$IMAGE_CACHE_PATH" && exit 6 || exit 1;;
# Image previews for image files. w3mimgdisplay will be called for all
# image files (unless overriden as above), but might fail for
# unsupported types.
image/*)
orientation="$( identify -format '%[EXIF:Orientation]\n' -- "${FILE_PATH}" )"
## If orientation data is present and the image actually
## needs rotating ("1" means no rotation)...
if [[ -n "$orientation" && "$orientation" != 1 ]]; then
## ...auto-rotate the image according to the EXIF data.
convert -- "${FILE_PATH}" -auto-orient "${IMAGE_CACHE_PATH}" && exit 6
fi
exit 7 ;;
# Image preview for video, disabled by default.:
video/*)
# Get embedded thumbnail
ffmpeg -i "${FILE_PATH}" -map 0:v -map -0:V -c copy "${IMAGE_CACHE_PATH}" && exit 6
# # Get frame 10% into video
# ffmpegthumbnailer -i "${FILE_PATH}" -o "${IMAGE_CACHE_PATH}" -s 0 && exit 6
;;
application/pdf)
# pdftocairo adds the extension itself, so we have to remove it
pdftocairo -jpeg -singlefile "$FILE_PATH" "$IMAGE_CACHE_PATH" \
&& mv "${IMAGE_CACHE_PATH}.jpg" "$IMAGE_CACHE_PATH" \
&& exit 6
;;
esac
fi
if command -v run-mailcap >/dev/null && command -v setsid >/dev/null; then
setsid --fork run-mailcap --action=cat -- "$FILE_PATH" | head -n 200
elif command -v lesspipe.sh >/dev/null; then
lesspipe.sh -- "$FILE_PATH" | head -n 200
elif command -v bat >/dev/null; then
BAT_THEME=ansi bat --color=always --style=plain --line-range=:200 -- "$FILE_PATH"
elif command -v batcat >/dev/null; then
BAT_THEME=ansi batcat --color=always --style=plain --line-range=:200 -- "$FILE_PATH"
elif command -v highlight >/dev/null; then
highlight --out-format=ansi --force -- "$FILE_PATH" | head -n 200
elif command -v less >/dev/null; then
less -- "$FILE_PATH" | head -n 200
else
cat -- "$FILE_PATH" | head -n 200
fi
EXIT_CODE=$?
# Ignore potential SIGPIPE from trimming the output
if (( EXIT_CODE == 0 )) || (( EXIT_CODE == 141 )); then
exit 5
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment