1
0
Fork 0

cleanup, prepping for sixel support in lf

This commit is contained in:
Luca Bilke 2024-01-29 17:11:15 +01:00
parent 03c3857f72
commit 994453b5e2
9 changed files with 67 additions and 83 deletions

View File

@ -1,5 +1,5 @@
# Basic vars
set ratios 1:2:4
set ratios 1:2:6
set shellopts '-eu'
set ifs "\n"
set findlen 1

View File

@ -5,32 +5,21 @@ RESET="\033[0m"
set -C -f
f=$1
w=$2
h=$3
x=$4
y=$5
f=$1 w=$2 h=$3 x=$4 y=$5
image() {
f=$1
w=$2
h=$3
x=$4
y=$5
# TODO: Maybe append some mediainfo output into the image with ffmpeg (when there's enough space)
# I assume that will take a bit of math that I'll try to implement in perl or python
if [ -f "$1" ] && [ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && command -V ueberzug >/dev/null 2>&1; then
printf '{"action": "add", "identifier": "PREVIEW", "x": "%s", "y": "%s", "width": "%s", "height": "%s", "scaler": "contain", "path": "%s"}\n' "$(($4 + 3))" "$(($5 + 1))" "$(($2 - 6))" "$(($3 - 2))" "$1" >"$FIFO_UEBERZUG"
f=$1 w=$2 h=$3 x=$4 y=$5
if [ -f "$f" ] && [ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ]; then
# chafa "$f" -f sixel -s "$(($2 - 2))x$3" --animate false
if command -V ueberzug >/dev/null 2>&1; then
printf '{"action": "add", "identifier": "PREVIEW", "x": "%s", "y": "%s", "width": "%s", "height": "%s", "scaler": "contain", "path": "%s"}\n' "$(($4 + 3))" "$(($5 + 1))" "$(($2 - 6))" "$(($3 - 2))" "$1" >"$FIFO_UEBERZUG"
fi
fi
exit 1
}
video() {
f=$1
w=$2
h=$3
x=$4
y=$5
f=$1 w=$2 h=$3 x=$4 y=$5
thumb="$(vidthumb "$f")" || (
printf "%b" "\033[31mvidthumb script not in path\033[0m"
return 1
@ -39,14 +28,14 @@ video() {
}
text() {
f=$1
w=$2
f=$1 w=$2
bat --color=always --style=plain --pager=never --terminal-width "$((w - 2))" "$f" && exit 1
cat "$f"; exit 1
cat "$f"
exit 1
}
run() {
#shellcheck disable=SC2068
#shellcheck disable=SC2068
if ! command -v "$1" >/dev/null 2>&1; then
printf "%b" "${RED}Cannot execute $1${RESET}"
exit 1
@ -59,57 +48,54 @@ run() {
}
mimetest() {
case "$1" in
text/*) text "$f" "$w" ;;
*/pdf)
[ ! -f "${CACHE}.jpg" ] && run pdftoppm -jpeg -f 1 -singlefile "$f" "$CACHE"
image "${CACHE}.jpg" "$w" "$h" "$x" "$y"
;;
*/x-bzip-compressed-tar | */x-compressed-tar | */x-xz-compressed-tar) run als -- "$f" ;;
*/x-7z-compressed | */vnd.rar | */x-tar | */zip | */x-java-archive | */x-xz | */gzip) run 7z l "$f" | sed 1,11d ;;
*/x-cd-image) run iso-info --no-header -l "$f" ;;
*/x-bittorrent) run transmission-show "$f" ;;
*/vnd.sun.xml.writer) run odt2txt "$f" ;;
*/msword) run catdoc "$f" ;;
*/vnd.openxmlformats-officedocument.wordprocessingml.document) run docx2txt <"$f" ;;
*/vnd.ms-excel) run ssconvert --export-type=Gnumeric_stf:stf_csv "$f" "fd://1" | text --language=csv ;;
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet) run ssconvert --export-type=Gnumeric_stf:stf_csv "$f" "fd://1" | text --language=csv ;;
*/epub+zip)
[ ! -f "$CACHE" ] && run epub-thumbnailer "$f" "$CACHE" 1024
image "$CACHE" "$w" "$h" "$x" "$y"
;;
*/pgp-encrypted) run gpg -d -- "$f" ;;
*/pkix-cert) run openssl x509 -text -noout -in "$f" ;;
image/svg+xml)
[ ! -f "$CACHE" ] && run inkscape --convert-dpi-method=none -o "$CACHE" --export-overwrite -D --export-png-color-mode=RGBA_16 "$f"
image "$CACHE" "$w" "$h" "$x" "$y"
;;
image/*) image "$f" "$w" "$h" "$x" "$y" ;;
video/*) video "$f" "$w" "$h" "$x" "$y" ;;
*opendocument*) run odt2txt "$f" ;;
*/markdown) run lowdown -Tterm "$f" ;;
*/html) run lynx -dump "$f" ;;
*) return 1 ;;
esac
case "$1" in
*/pdf)
[ ! -f "${CACHE}.jpg" ] && run pdftoppm -jpeg -f 1 -singlefile "$f" "$CACHE"
image "${CACHE}.jpg" "$w" "$h" "$x" "$y"
;;
*/x-bzip-compressed-tar | */x-compressed-tar | */x-xz-compressed-tar) run als -- "$f" ;;
*/x-7z-compressed | */vnd.rar | */x-tar | */zip | */x-java-archive | */x-xz | */gzip) run 7z l "$f" | sed 1,11d ;;
*/x-cd-image) run iso-info --no-header -l "$f" ;;
*/x-bittorrent) run transmission-show "$f" ;;
*/vnd.sun.xml.writer) run odt2txt "$f" ;;
*/msword) run catdoc "$f" ;;
*/vnd.openxmlformats-officedocument.wordprocessingml.document) run docx2txt <"$f" ;;
*/vnd.ms-excel) run ssconvert --export-type=Gnumeric_stf:stf_csv "$f" "fd://1" | text --language=csv ;;
*/vnd.openxmlformats-officedocument.spreadsheetml.sheet) run ssconvert --export-type=Gnumeric_stf:stf_csv "$f" "fd://1" | text --language=csv ;;
*/epub+zip)
[ ! -f "$CACHE" ] && run epub-thumbnailer "$f" "$CACHE" 1024
image "$CACHE" "$w" "$h" "$x" "$y"
;;
*/pgp-encrypted) run gpg -d -- "$f" ;;
*/pkix-cert) run openssl x509 -text -noout -in "$f" ;;
image/svg+xml)
[ ! -f "$CACHE" ] && run inkscape --convert-dpi-method=none -o "$CACHE" --export-overwrite -D --export-png-color-mode=RGBA_16 "$f"
image "$CACHE" "$w" "$h" "$x" "$y"
;;
image/*) image "$f" "$w" "$h" "$x" "$y" ;;
video/*) video "$f" "$w" "$h" "$x" "$y" ;;
*opendocument*) run odt2txt "$f" ;;
*/markdown) run mdcat "$f" ;;
*/html) run lynx -dump "$f" ;;
*) return 1 ;;
esac
}
CACHE="$HOME/.cache/lf/thumbnail-$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')"
CACHE="${XDG_CACHE_HOME}/lf/thumbnail-$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | awk '{print $1}')"
filerun="$(file --brief --dereference --mime-type "$f")"
mimetest "$filerun" && exit 1
if command -v gio 1>/dev/null 2>&1; then
giorun="$(gio info "$f" 2>/dev/null | grep standard::content-type | cut -d' ' -f4)"
mimetest "$giorun" && exit 1
giorun="$(gio info "$f" 2>/dev/null | grep standard::content-type | cut -d' ' -f4)"
mimetest "$giorun" && exit 1
fi
printf "%b" "\$(file --mime-type)\t\t\t : $filerun\n"
[ -n "$giorun" ] && printf "%b" "\$(gio info)\t\t\t\t : $giorun\n"
mediainfo "$1" | head -n-2 | grep -v -e "Complete name" -e "General"
case $(file -b "$f") in
*ascii* | *utf*) text "$f" "$w";;
*ASCII* | *UTF*) text "$f" "$w";;
*)
printf "%b" "\$(file --mime-type)\t\t\t : $filerun\n"
[ -n "$giorun" ] && printf "%b" "\$(gio info)\t\t\t\t : $giorun\n"
mediainfo "$1" | head -n -2 | grep -v -e "Complete name" -e "General"
;;
esac
exit 1

View File

@ -1,4 +1,3 @@
# vim: set ft=sh
[ ! -f $XDG_DATA_HOME/zsh/history ] && mkdir -p $XDG_DATA_HOME/zsh && touch $XDG_DATA_HOME/zsh/history
setopt HIST_IGNORE_ALL_DUPS HIST_REDUCE_BLANKS HIST_VERIFY BANG_HIST interactive_comments autocd noflowcontrol longlistjobs nonomatch notify hash_list_all share_history histignorespace PROMPT_SUBST

View File

@ -1,4 +1,3 @@
# vim: set ft=sh
HISTSIZE=10000000
SAVEHIST=10000000
HISTFILE="$XDG_DATA_HOME/zsh/history"

View File

@ -1,4 +1,4 @@
# vim: set ft=sh:
#!/bin/zsh
venv_prompt() {
[[ -n ${VIRTUAL_ENV} ]] || return
echo -e "%F{yellow}%f ${VIRTUAL_ENV:t:gs/%/%%}"
@ -16,7 +16,7 @@ function reset_beam() { echo -ne '\e[5 q'; }
function lfwrap () {
LF_DIRFILE="/tmp/lfdir.$(uuidgen)"
lfub -last-dir-path="$LF_DIRFILE"
lf-ueberzug -last-dir-path="$LF_DIRFILE"
dir="$(cat "$LF_DIRFILE")"
rm "$LF_DIRFILE"
if [ -d "$dir" ]; then

View File

@ -1,9 +0,0 @@
#!/bin/sh
# Reads file names from stdin and selects them in lf.
while read -r file; do
[ -z "$file" ] && continue
lf -remote "send select \"$file\""
lf -remote "send toggle"
done

9
.local/bin/lf-sixel Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
set -e
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
lf "$@"
else
[ ! -d "$HOME/.cache/lf" ] && mkdir --parents "$HOME/.cache/lf"
lf "$@" 3>&-
fi

View File

@ -1,5 +1,4 @@
#!/bin/sh
set -e
clean() {
@ -11,10 +10,10 @@ if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
command lf "$@"
else
[ ! -d "$XDG_CACHE_HOME/lf" ] && mkdir --parents "$XDG_CACHE_HOME/lf"
export FIFO_UEBERZUG="/tmp/ueberzug-$$"
export FIFO_UEBERZUG="$XDG_CACHE_HOME/lf/ueberzug-$$"
mkfifo "$FIFO_UEBERZUG"
ueberzug layer -s -p json <"$FIFO_UEBERZUG" &
exec 3>"$FIFO_UEBERZUG"
trap clean HUP INT QUIT TERM PWR EXIT
trap clean EXIT
command lf "$@" 3>&-
fi

View File

@ -1,2 +1,3 @@
- [ ] [Simplify dotfile management](https://www.atlassian.com/git/tutorials/dotfiles)
- [ ] Refactor Neovim config
- [ ] Plumbing script! something to handle URLs, filepaths, and other stuff