From 5db7f0e0671acfb9d03145d21a3645c80efc167e Mon Sep 17 00:00:00 2001 From: MikunoNaka Date: Sun, 4 Apr 2021 21:57:10 +0530 Subject: copied BrodieRobertson's preview scripts --- .config/lf/draw_img | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ .config/lf/image | 17 ++++++++++++++ .config/lf/preview | 2 +- 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100755 .config/lf/draw_img create mode 100755 .config/lf/image mode change 100644 => 100755 .config/lf/preview (limited to '.config') diff --git a/.config/lf/draw_img b/.config/lf/draw_img new file mode 100755 index 0000000..5a70d5e --- /dev/null +++ b/.config/lf/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/image b/.config/lf/image new file mode 100755 index 0000000..db64bf2 --- /dev/null +++ b/.config/lf/image @@ -0,0 +1,17 @@ +#!/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/preview b/.config/lf/preview old mode 100644 new mode 100755 index c90dba6..3069a06 --- a/.config/lf/preview +++ b/.config/lf/preview @@ -1,5 +1,4 @@ #!/bin/sh -# thank you brodie robertson for this # Clear the last preview (if any) $HOME/.config/lf/image clear @@ -46,3 +45,4 @@ case "$1" in ;; *) highlight --out-format ansi "$1" || cat "$1";; esac + -- cgit v1.2.3