summaryrefslogtreecommitdiff
path: root/.config/lf/brodie
diff options
context:
space:
mode:
authorVikas Kushwaha <dev@vikas.rocks>2024-11-21 13:30:52 +0530
committerVikas Kushwaha <dev@vikas.rocks>2024-11-21 13:30:52 +0530
commit5c916d69d457101326803eb076a746060e3618cf (patch)
treed6fce3256eede1c1bf78fb6a1be75b9cc4b84cee /.config/lf/brodie
Moved from github
Diffstat (limited to '.config/lf/brodie')
-rw-r--r--.config/lf/brodie/draw_img67
-rw-r--r--.config/lf/brodie/image18
-rw-r--r--.config/lf/brodie/preview48
3 files changed, 133 insertions, 0 deletions
diff --git a/.config/lf/brodie/draw_img b/.config/lf/brodie/draw_img
new file mode 100644
index 0000000..5a70d5e
--- /dev/null
+++ b/.config/lf/brodie/draw_img
@@ -0,0 +1,67 @@
+#!/usr/bin/env bash
+
+clear_screen() {
+ printf '\e[%sH\e[9999C\e[1J%b\e[1;%sr' \
+ "$((LINES-2))" "${TMUX:+\e[2J}" "$max_items"
+}
+
+# Get a file's mime_type.
+mime_type=$(file -bi "$1")
+
+# File isn't an image file, give warning.
+if [[ $mime_type != image/* ]]; then
+ lf -remote "send $id echoerr 'Not an image'"
+ exit
+fi
+
+w3m_paths=(/usr/{local/,}{lib,libexec,lib64,libexec64}/w3m/w3mi*)
+read -r w3m _ < <(type -p w3mimgdisplay "${w3m_paths[@]}")
+read -r LINES COLUMNS < <(stty size)
+
+# Get terminal window size in pixels and set it to WIDTH and HEIGHT.
+export $(xdotool getactivewindow getwindowgeometry --shell)
+
+# Get the image size in pixels.
+read -r img_width img_height < <("$w3m" <<< "5;${CACHE:-$1}")
+
+((img_width > WIDTH)) && {
+ ((img_height=img_height*WIDTH/img_width))
+ ((img_width=WIDTH))
+}
+
+((img_height > HEIGHT)) && {
+ ((img_width=img_width*HEIGHT/img_height))
+ ((img_height=HEIGHT))
+}
+
+# Variable needed for centering image.
+HALF_HEIGHT=$(expr $HEIGHT / 2)
+HALF_WIDTH=$(expr $WIDTH / 2)
+HALF_IMG_HEIGHT=$(expr $img_height / 2)
+HALF_IMG_WIDTH=$(expr $img_width / 2)
+X_POS=$(expr $HALF_WIDTH - $HALF_IMG_WIDTH)
+Y_POS=$(expr $HALF_HEIGHT - $HALF_IMG_HEIGHT)
+
+clear_screen
+# Hide the cursor.
+printf '\e[?25l'
+
+# Display the image.
+printf '0;1;%s;%s;%s;%s;;;;;%s\n3;\n4\n' \
+ ${X_POS:-0} \
+ ${Y_POS:-0} \
+ "$img_width" \
+ "$img_height" \
+ "${CACHE:-$1}" | "$w3m" &>/dev/null
+
+# Wait for user input.
+read -ern 1
+
+# Clear the image.
+printf '6;%s;%s;%s;%s\n3;' \
+ "${X_POS:-0}" \
+ "${Y_POS:-0}" \
+ "$WIDTH" \
+ "$HEIGHT" | "$w3m" &>/dev/null
+
+clear_screen
diff --git a/.config/lf/brodie/image b/.config/lf/brodie/image
new file mode 100644
index 0000000..77ddb5b
--- /dev/null
+++ b/.config/lf/brodie/image
@@ -0,0 +1,18 @@
+#!/usr/bin/env bash
+readonly ID_PREVIEW="preview"
+main() {
+ case "$1" in
+ "clear")
+ declare -p -A cmd=([action]=remove [identifier]="$ID_PREVIEW") \
+ > "$FIFO_UEBERZUG"
+ ;;
+ "draw")
+ declare -p -A cmd=([action]=add [identifier]="$ID_PREVIEW" \
+ [x]="$3" [y]="$4" [max_width]="$5" [max_height]="$6" \
+ [path]="$2") > "$FIFO_UEBERZUG"
+ ;;
+ "*") echo "Unknown command: '$1', '$2'" ;;
+ esac
+}
+main "$@"
+
diff --git a/.config/lf/brodie/preview b/.config/lf/brodie/preview
new file mode 100644
index 0000000..3069a06
--- /dev/null
+++ b/.config/lf/brodie/preview
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+# Clear the last preview (if any)
+$HOME/.config/lf/image clear
+
+# Calculate where the image should be placed on the screen.
+num=$(printf "%0.f\n" "`echo "$(tput cols) / 2" | bc`")
+numb=$(printf "%0.f\n" "`echo "$(tput cols) - $num - 1" | bc`")
+numc=$(printf "%0.f\n" "`echo "$(tput lines) - 2" | bc`")
+
+case "$1" in
+ *.tgz|*.tar.gz) tar tzf "$1";;
+ *.tar.bz2|*.tbz2) tar tjf "$1";;
+ *.tar.txz|*.txz) xz --list "$1";;
+ *.tar) tar tf "$1";;
+ *.zip|*.jar|*.war|*.ear|*.oxt) unzip -l "$1";;
+ *.rar) unrar l "$1";;
+ *.7z) 7z l "$1";;
+ *.[1-8]) man "$1" | col -b ;;
+ *.o) nm "$1" | less ;;
+ *.torrent) transmission-show "$1";;
+ *.iso) iso-info --no-header -l "$1";;
+ *odt,*.ods,*.odp,*.sxw) odt2txt "$1";;
+ *.doc) catdoc "$1" ;;
+ *.docx) docx2txt "$1" - ;;
+ *.csv) cat "$1" | sed s/,/\\n/g ;;
+ *.pdf)
+ CACHE=$(mktemp /tmp/thumbcache.XXXXX)
+ pdftoppm -png -f 1 -singlefile "$1" "$CACHE"
+ $HOME/.config/lf/image draw "$CACHE.png" $num 1 $numb $numc
+ ;;
+ *.epub)
+ CACHE=$(mktemp /tmp/thumbcache.XXXXX)
+ epub-thumbnailer "$1" "$CACHE" 1024
+ $HOME/.config/lf/image draw "$CACHE" $num 1 $numb $numc
+ ;;
+ *.bmp|*.jpg|*.jpeg|*.png|*.xpm)
+ $HOME/.config/lf/image draw "$1" $num 1 $numb $numc
+ ;;
+ *.wav|*.mp3|*.flac|*.m4a|*.wma|*.ape|*.ac3|*.og[agx]|*.spx|*.opus|*.as[fx]|*.flac) exiftool "$1";;
+ *.avi|*.mp4|*.wmv|*.dat|*.3gp|*.ogv|*.mkv|*.mpg|*.mpeg|*.vob|*.fl[icv]|*.m2v|*.mov|*.webm|*.ts|*.mts|*.m4v|*.r[am]|*.qt|*.divx)
+ CACHE=$(mktemp /tmp/thumbcache.XXXXX)
+ ffmpegthumbnailer -i "$1" -o "$CACHE" -s 0
+ $HOME/.config/lf/image draw "$CACHE" $num 1 $numb $numc
+ ;;
+ *) highlight --out-format ansi "$1" || cat "$1";;
+esac
+