terminal: Create module to consolidate term utils

Terminal application, a variety of shell configurations, terminal file
and session management all consolidated in one place.
This commit is contained in:
Marty Oehme 2023-01-07 16:11:40 +01:00
parent 2e0c992a54
commit 9781b26b22
Signed by: Marty
GPG key ID: 73BA40D5AFAF49C9
31 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,6 @@
This directory is dedicated for user-supplied scripts/executables.
vifm modifies its PATH environment variable to let user run those
scripts without specifying full path. All subdirectories are added
as well. File in a subdirectory overrules file with the same name
in parent directories. Restart might be needed to recognize files
in newly created or renamed subdirectories.

View file

@ -0,0 +1,25 @@
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage: $0 filename"
exit 1
fi
# upper limit of lines to display for text files
nlines=250
# upper limit of bytes to display for binary files
nbytes=2048
# language of text files
language=russian
# output encoding for text files
encoding=utf-8
info=$(head -$nlines "$1" | file --mime -)
charset=${info#*=}
# shellcheck disable=2268
if [ "x$charset" == "xbinary" ]; then
hexdump -e '"%08_ax: "' -e '8/1 "%02x " " " 8/1 "%02x "' -e '" |" 16/1 "%_p"' -e '"\n"' -v -n $nbytes "$1"
else
head -$nlines "$1" | enconv -g -L $language -x $encoding
fi

View file

@ -0,0 +1,62 @@
#!/bin/sh
[ -d "$HOME/.cache/vifm" ] || mkdir -p "$HOME/.cache/vifm"
# $1 action
action="$1"
# $2 panel width
panel_width=$(($2 * 6))
# $3 panel height
panel_height=$(($3 * 14))
# $4 image path
image_file="$4"
PCACHE="$HOME/.cache/vifm/thumbnail.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$PWD/$image_file")" | sha256sum | awk '{print $1}')"
cleanup() {
printf '\33[s\33[5A\33[2K\33[u'
clear
exit 0
}
# recieves image with height
image() {
montage "$1" -geometry "${2}x${3}" sixel:-
}
case "$action" in
"clear")
cleanup
;;
"draw")
[ ! -f "${PCACHE}.jpg" ] && convert "$image_file"'[0]' "${PCACHE}.jpg"
# FILE="$PWD/$image_file"
image "${PCACHE}.jpg" "$panel_width" "$panel_height"
;;
"video")
[ ! -f "${PCACHE}.jpg" ] &&
ffmpegthumbnailer -i "$4" -o "${PCACHE}.jpg" -s 0 -q 5
image "${PCACHE}.jpg" "$panel_width" "$panel_height"
;;
"epub")
[ ! -f "${PCACHE}.jpg" ] &&
epub-thumbnailer "$image_file" "$PCACHE" 1024
image "${PCACHE}.jpg" "$panel_width" "$panel_height"
;;
"pdf")
[ ! -f "${PCACHE}.jpg" ] &&
pdftoppm -jpeg -f 1 -singlefile "$image_file" "$PCACHE"
image "${PCACHE}.jpg" "$panel_width" "$panel_height"
;;
"audio")
[ ! -f "${PCACHE}.jpg" ] &&
ffmpeg -i "$image_file" "${PCACHE}.jpg" -y >/dev/null
image "${PCACHE}.jpg" "$panel_width" "$panel_height"
;;
"font")
[ ! -f "${PCACHE}.jpg" ] &&
fontpreview -i "$image_file" -o "${PCACHE}.jpg"
image "${PCACHE}.jpg" "$panel_width" "$panel_height"
;;
*) ;;
esac

View file

@ -0,0 +1,115 @@
#!/usr/bin/env bash
show_help() {
printf """
vifm-thumbnailer
Easily show images/thumbnails from the vifm file manager.
Usage: vifm-thumbnailer <current file>
Point it to a file whose directory you want thumbnailed.
If you point it to a directory itself, it will list its contents.
Options:
-h Display this help.
-r Show images recursively from current folder.
-t Open viewer in thumbnail mode.
-m Show thumbnails for video files in addition to image files.
Will take longer to load up and requires ffmpeg utilities.
Vifm:
If the file is on your path (e.g. in ~/.vifm/scripts) from within
vifm you can run the script by invoking it with:
\`:!vifm-thumbnailer \%%c\`
which will show all directory thumbnails with the current file
initially shown.
If you want to have vifm show a filtered view for selected files
afterwards add a \%%u to the command.
"""
}
TMPDIR="$(mktemp -d)"
valid_files_tmp="$TMPDIR/nsxiv_rifle_$$"
# get all files in directory
list_img_files() {
if exist fd; then
fd --max-depth="$maxdepth" -tf . "${1%/*}" | is_img_extension | sort | tee -a "$valid_files_tmp"
else
find -L "${1%/*}" -maxdepth "$maxdepth" -type f -print | is_img_extension | sort | tee -a "$valid_files_tmp"
fi
}
list_vid_files() {
# shellcheck disable=2016
find "${1%/*}" -mindepth 1 -maxdepth "$maxdepth" \( -type f -o -type l \) -printf "%P\0" |
xargs -r0 -n 20 -P 64 file -00 -L --mime-type -- |
xargs -r0 -n 2 sh -c '
case "$2" in
video/*) printf "%s\0" "$1";;
esac
' -- |
sort -z |
xargs -r0 -I FILE ffmpegthumbnailer -i FILE -o "$TMPDIR/FILE.jpg" -m -s 384
find -L "$TMPDIR/" -maxdepth "$maxdepth" -type f -print | sort | tee -a "$valid_files_tmp"
}
img_ext="jpe?g|png|gif|svg|webp|tiff|heif|avif|ico|bmp"
is_img_extension() {
grep -iE "\.($img_ext)$"
}
# TODO if marking video thumbnail - will use wrong temporary path on output, makes e.g. vifm %u not work
open() {
trap 'rm -rf -- $TMPDIR' TERM INT EXIT
current="$(list_img_files "$1" | grep -nF "$1")"
if [ "$videos" = true ]; then
list_vid_files "$1"
fi
[ "$thumbnails" = true ] && nsxiv_args="-t"
if [ -n "$current" ]; then
nsxiv "$nsxiv_args" -i -n "${current%%:*}" -o -- <"$valid_files_tmp"
else
# fallback incase file didn't have a valid extension, or we couldn't
# find it inside the list
nsxiv -o -i -- <"$valid_files_tmp"
fi
}
maxdepth=1
while getopts "mrt?h" opt; do
case "$opt" in
r)
maxdepth=100
;;
m)
videos=true
;;
t)
thumbnails=true
;;
h | \? | *)
show_help
exit 0
;;
esac
done
shift $((OPTIND - 1))
[ "$1" = '--' ] && shift
case "$1" in
"")
echo "Usage: ${0##*/} PICTURES" >&2
exit 1
;;
/*) open "$1" ;;
"~"/*) open "$HOME/${1#"~"/}" ;;
*) open "$PWD/$1" ;;
esac