all repos — dotfiles @ a779bb5b6a1eac31c26474c5fafa7ff826bef8d2

linux dotfiles

add lf
Prithu Goswami pg@prithu.dev
Mon, 29 Apr 2024 14:20:15 +0530
commit

a779bb5b6a1eac31c26474c5fafa7ff826bef8d2

parent

c8063e710115d077769c36ff9643d65bd12ea11c

4 files changed, 182 insertions(+), 0 deletions(-)

jump to
A bin/scripts/lf_

@@ -0,0 +1,24 @@

+#!/bin/sh +set -e + +if [ -n "$DISPLAY" ]; then + export FIFO_UEBERZUG="${TMPDIR:-/tmp}/lf-ueberzug-$$" + + cleanup() { + exec 3>&- + rm "$FIFO_UEBERZUG" + } + + mkfifo "$FIFO_UEBERZUG" + ueberzug layer -s <"$FIFO_UEBERZUG" & + exec 3>"$FIFO_UEBERZUG" + trap cleanup EXIT + + if ! [ -d "$HOME/.cache/lf" ]; then + mkdir -p "$HOME/.cache/lf" + fi + + lf "$@" 3>&- +else + exec lf "$@" +fi
A config/lf/cls

@@ -0,0 +1,5 @@

+#!/bin/sh +set -euf +if [ -n "${FIFO_UEBERZUG-}" ]; then + printf '{"action":"remove","identifier":"preview"}\n' >"$FIFO_UEBERZUG" +fi
A config/lf/lfrc

@@ -0,0 +1,2 @@

+set previewer ~/.config/lf/pv +set cleaner ~/.config/lf/cls
A config/lf/pv

@@ -0,0 +1,151 @@

+#!/bin/sh +set -euf + +draw() { + jq -nc --argjson x "$x" --argjson y "$y" \ + --argjson width "$width" --argjson height "$height" \ + --arg path "$1" ' + { + "action": "add", + "identifier": "preview", + "x": $x, + "y": $y, + "width": $width, + "height": $height, + "scaler": "contain", + "scaling_position_x": 0.5, + "scaling_position_y": 0.5, + "path": $path + }' >"$FIFO_UEBERZUG" + exit 1 +} + +hash() { + printf '%s/.cache/lf/%s' "$HOME" \ + "$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f -- "$1")" | sha256sum | cut -d' ' -f1)" +} + +cache() { + if [ -f "$1" ]; then + draw "$1" + fi +} + +file="$1" +width="$2" +height="$3" +x="$4" +y="$5" + +if ! [ -f "$file" ] && ! [ -h "$file" ]; then + exit +fi + +default_x="1920" +default_y="1080" + +ext="$(printf '%s' "$file" | tr '[:upper:]' '[:lower:]')" +ext="${ext##*.}" +case "$ext" in + 7z|a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|\ + lha|lrz|lz|lzh|lzma|lzo|rar|rpm|rz|t7z|tar|tbz|\ + tbz2|tgz|tlz|txz|tZ|tzo|war|xz|Z|zip) + als -- "$file" + exit 0 + ;; + [1-8]) + COLUMNS="$width" man -- "$file" | col -b + exit 0 + ;; + pdf) + if [ -n "${FIFO_UEBERZUG-}" ]; then + cache="$(hash "$file")" + cache "$cache.jpg" + pdftoppm -f 1 -l 1 \ + -scale-to-x "$default_x" \ + -scale-to-y -1 \ + -singlefile \ + -jpeg \ + -- "$file" "$cache" + draw "$cache.jpg" + else + pdftotext -nopgbrk -q -- "$file" - + exit 0 + fi + ;; + djvu|djv) + if [ -n "${FIFO_UEBERZUG-}" ]; then + cache="$(hash "$file").tiff" + cache "$cache" + ddjvu -format=tiff -quality=90 -page=1 -size="${default_x}x${default_y}" \ + - "$cache" <"$file" + draw "$cache" + else + djvutxt - <"$file" + exit 0 + fi + ;; + docx|odt|epub) + pandoc -s -t plain -- "$file" + exit 0 + ;; + htm|html|xhtml) + lynx -dump -- "$file" + exit 0 + ;; + svg) + if [ -n "${FIFO_UEBERZUG-}" ]; then + cache="$(hash "$file").jpg" + cache "$cache" + convert -- "$file" "$cache" + draw "$cache" + fi + ;; +esac + +mime="$(file -Lb --mime-type -- "$file")" +case "$mime" in + text/*) + source-highlight -q --outlang-def=esc.outlang --style-file=esc.style -i "$file" || cat -- "$file" + exit 0 + ;; + image/*) + if [ -n "${FIFO_UEBERZUG-}" ]; then + orientation="$(identify -format '%[EXIF:Orientation]\n' -- "$file")" + if [ -n "$orientation" ] && [ "$orientation" != 1 ]; then + cache="$(hash "$file").jpg" + cache "$cache" + convert -- "$file" -auto-orient "$cache" + draw "$cache" + else + draw "$file" + fi + fi + ;; + video/*) + if [ -n "${FIFO_UEBERZUG-}" ]; then + cache="$(hash "$file").jpg" + cache "$cache" + ffmpegthumbnailer -i "$file" -o "$cache" -s 0 + draw "$cache" + fi + ;; +esac + +header_text="File Type Classification" +header="" +len="$(( (width - (${#header_text} + 2)) / 2 ))" +if [ "$len" -gt 0 ]; then + for i in $(seq "$len"); do + header="-$header" + done + header="$header $header_text " + for i in $(seq "$len"); do + header="$header-" + done +else + header="$header_text" +fi +printf '\033[7m%s\033[0m\n' "$header" +file -Lb -- "$file" | fold -s -w "$width" +exit 0