aboutsummaryrefslogtreecommitdiff
path: root/home/dot_local/share/bin/executable_preview
diff options
context:
space:
mode:
Diffstat (limited to 'home/dot_local/share/bin/executable_preview')
-rw-r--r--home/dot_local/share/bin/executable_preview75
1 files changed, 75 insertions, 0 deletions
diff --git a/home/dot_local/share/bin/executable_preview b/home/dot_local/share/bin/executable_preview
new file mode 100644
index 0000000..819938b
--- /dev/null
+++ b/home/dot_local/share/bin/executable_preview
@@ -0,0 +1,75 @@
+#!/bin/sh
+[ -n "$1" ] && FILE="$1" || {
+ echo "error: no input file"
+ exit 1
+}
+[ -n "$2" ] && WIDTH="$(( "$2" - 1 ))" || WIDTH="$(tput cols)"
+[ -n "$3" ] && HEIGHT="$3" || HEIGHT="$(tput lines)"
+[ -n "$4" ] && POS_X="$4" || POS_X="0"
+[ -n "$5" ] && POS_Y="$5" || POS_Y="0"
+
+MIMETYPE="$(file --mime-type -Lb "$FILE")"
+EXT="${FILE#*.}"
+
+if [ "$MIMETYPE" = "text/plain" ] ; then
+ [ "$EXT" = "md" ] && MIMETYPE="text/markdown"
+ [ "$EXT" = "rst" ] && MIMETYPE="text/x-rst"
+fi
+[ "$MIMETYPE" = "application/javascript" ] && MIMETYPE="text/javascript"
+
+render_manpage() {
+ exec groff -T utf8 -m man -rcR=1 -rIN=0 -rLL="${WIDTH}n" << EOF
+.nr an-suppress-header-and-footer 1
+.TH
+$(cat | preconv)
+EOF
+}
+
+case "$MIMETYPE" in
+ image/*)
+ if [ -z "$UB_SOCKET" ] ; then
+ echo "ueberzug not available"
+ else
+ ueberzugpp cmd \
+ --socket "$UB_SOCKET" \
+ --action add \
+ --identifier PREVIEW \
+ --file "$FILE" \
+ --max-width "$WIDTH" --max-height "$HEIGHT" \
+ --xpos "$POS_X" --ypos "$POS_Y"
+ exit 1 # <- disable cache
+ fi
+ ;;
+ text/markdown)
+ pandoc --from=gfm --to=man "$FILE" | render_manpage
+ ;;
+ text/x-rst)
+ pandoc --from=rst --to=man "$FILE" | render_manpage
+ ;;
+ text/*)
+ bat \
+ --terminal-width="$WIDTH" \
+ --paging=never \
+ --color=always \
+ -- "$FILE"
+ ;;
+ application/x-tar|\
+ application/x-bzip|\
+ application/x-bzip2|\
+ application/gzip|\
+ application/zip|\
+ application/x-7z-compressed|\
+ application/vnd.rar)
+ bsdtar -tf "$FILE"
+ ;;
+ application/json)
+ jq --color-output . "$FILE"
+ ;;
+ *)
+ echo "$MIMETYPE"
+ file -b "$FILE" | fold --width="$WIDTH" --spaces
+ ;;
+esac
+
+exit 0
+