Last active
October 15, 2020 15:20
-
-
Save Provessor/dbb4a6d22945e7a42c3b904302ee273c to your computer and use it in GitHub Desktop.
Scripts used to get image previews used in lf with ueberzug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
>"${UEBERZUG_FIFO}" declare -A -p cmd=( \ | |
[action]=remove [identifier]="$UEBERZUG_IDEN" ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
FILE="$HOME/.cache/lf/err.png" | |
function hash_filename { | |
FILE="$HOME/.cache/lf/$(echo ${1%.*} | sed -e 's|/|\!|g').$2" | |
} | |
function draw_clear { | |
>"${UEBERZUG_FIFO}" declare -A -p cmd=( \ | |
[action]=remove [identifier]="$UEBERZUG_IDEN" ) | |
} | |
function draw_image { | |
# chafa -c full -s ${3}x${2} "$1" | |
>"${UEBERZUG_FIFO}" declare -A -p cmd=( \ | |
[action]=add [identifier]="$UEBERZUG_IDEN" \ | |
[x]="$4" [y]="$5" [width]="$3" [height]="$2" \ | |
[scaler]=fit_contain [scaling_position_x]=0.5 [scaling_position_y]=0.5 \ | |
[path]="$1" ) | |
exit 1 | |
} | |
function make_video { | |
if [ "${FILE}" -ot "$1" ]; then | |
ffmpegthumbnailer -t 0% -q 3 -s 0 \ | |
-c jpeg -i "$1" -o "${FILE}" | |
fi | |
} | |
function make_pdf { | |
if [ "${FILE}" -ot "$1" ]; then | |
pdftoppm -png -f 1 -l 1 -jpeg -tiffcompression jpeg \ | |
-scale-to-x -1 -scale-to-y 1080 \ | |
-singlefile "$1" "${FILE%.png}" | |
fi | |
} | |
case $(file -b --mime-type "$1") in | |
text/*) | |
draw_clear | |
cat "$1" | |
;; | |
image/*) | |
draw_image "$1" "$2" "$3" "$4" "$5" | |
;; | |
video/*) | |
hash_filename "$1" "jpg" | |
make_video "$1" "$2" "$3" | |
draw_image "${FILE}" "$2" "$3" "$4" "$5" | |
;; | |
audio/*) | |
draw_clear | |
exiftool "$1" | |
;; | |
application/pdf) | |
hash_filename "$1" "png" | |
make_pdf "$1" "$2" "$3" | |
draw_image "${FILE}" "$2" "$3" "$4" "$5" | |
;; | |
application/gzip|application/x-xz) | |
draw_clear | |
tar tf "$1" | |
;; | |
application/zip) | |
draw_clear | |
unzip -Z -1 "$1" | |
;; | |
application/x-sharedlib) | |
draw_clear | |
readelf -h "$1" | |
;; | |
*) | |
draw_clear | |
cat "$1" | |
;; | |
esac | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
declare -r -x UEBERZUG_FIFO="$(mktemp --dry-run --suffix "lf-$$-ueberzug")" | |
declare -r -x UEBERZUG_IDEN="${UEBERZUG_FIFO#*.}" | |
declare -r -x PREVIEW_TMP="$(mktemp -d --suffix "lf-$$-ueberzug")" | |
function check_cache { | |
if [ ! -d "$HOME/.cache/lf" ]; then | |
mkdir -p "$HOME/.cache/lf" | |
fi | |
} | |
function start_ueberzug { | |
mkfifo "${UEBERZUG_FIFO}" | |
<"${UEBERZUG_FIFO}" ueberzug layer --parser bash --silent & | |
3> "${UEBERZUG_FIFO}" exec | |
} | |
function remove_temp { | |
rm "${UEBERZUG_FIFO}" | |
rm -r "${PREVIEW_TMP}" | |
} | |
check_cache | |
start_ueberzug | |
lf "$@" | |
remove_temp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment