dotfiles/.local/bin/scripts/b

136 lines
4.2 KiB
Plaintext
Raw Normal View History

2021-09-20 09:14:34 +00:00
#!/bin/bash
# [b]rowse - overview of given files or current directory
2021-12-08 10:37:18 +00:00
# depends: tput stat bat checkaccess(in my dotfiles)
# optdepends: timg, neovim (compressed files), pdftoppm (PDF), mtn (video), audiowaveform
2021-11-02 12:00:02 +00:00
# args: files to inspect
#
# Supports:
# - listing directories
# - listing contents of any compressed file with neovim
# - visual files are displayed with timg
# video thumbnails via mtn, pdf pages from pdftoppm
# - text files are displayed through bat
# Automatically requests elevation through sudo when needed
2020-12-10 16:34:56 +00:00
test "$1" = "-v" &&
set -eo xtrace &&
shift
2021-12-13 12:56:42 +00:00
inspect=false
test "$1" = "-i" &&
inspect=true &&
shift
set -o pipefail
2020-11-16 11:29:31 +00:00
for last; do true; done
2021-12-13 12:56:42 +00:00
last="${last:-.}"
2020-12-10 16:34:56 +00:00
checkperm() {
checkaccess -r "$@" || elevate=sudo
mime="$(test -n "$shifted" || $elevate file --dereference --mime "$@")"
}
checkperm "$last"
2020-12-10 16:34:56 +00:00
fileinfo() {
tput setaf 6
for arg
do case "$arg" in (-*) continue;; esac
$elevate file -E "$arg"
#probe="$($elevate ffprobe "$arg" 2>&1)"
#echo $probe | grep -v -e '00:00:00.04' -e 'ansi' &&
$elevate ffprobe "$arg" 2>&1 | grep "bitrate: ....\? " | sed 's/, start:[^,]\+,/,/' ||
2021-12-13 12:56:42 +00:00
stat --format "%A %s $(
size="$($elevate unzip -l "$arg" 2>/dev/null | tail -1)" &&
echo "(uncompressed $(echo $size | cut -d' ' -f1 | numfmt --to=iec-i --suffix=B))"
) - birth %.10w mod %.10y" "$arg" | numfmt --field=2 --to=iec-i --padding=6 --suffix=B
done
tput sgr0
}
prefix=/tmp/b
mkdir -p "$prefix"
2021-12-13 12:56:42 +00:00
declare -a timg timga bat batl ls
for arg; do
case "$arg" in (-*) args="$args $arg"; continue;; esac
checkperm "$arg"
grid=$(expr $(tput cols) / 20)
tmpfile="$prefix/$(basename "$arg")"
case "$mime" in
(*\ application/pdf\;*)
test -f "$tmpfile-1.png" || pdftoppm -r 70 "$arg" "$tmpfile" -l $(expr $grid '*' 2)
timg -W --grid=$grid "$tmpfile"*
;;
(*\ video/*)
suffix=_thumbs.jpg
mtn -i -t -W -r2 -D6 -b 0,6 -c $grid -w $(expr $(tput cols) '*' 20) \
-O $prefix -o $suffix "$arg" 2>/dev/null
timg -W "$prefix/$(basename "${arg%.*}")$suffix"
;;
(*\ image/*)
timg+=("$arg"); continue;;
(*\ inode/directory\;*)
ls+=("$arg"); continue;;
(*)
case "$(file "$arg")" in
(*\ ?udio*)
img="$tmpfile.png"
2021-12-08 10:37:18 +00:00
if which audiowaveform 2>/dev/null >&2
then find "$img" -not -empty 2>/dev/null | grep --quiet . ||
audiowaveform --quiet --pixels-per-second 2 --height 36 --width 2000 --amplitude-scale auto \
--background-color 000000 --waveform-color 99BBFF --axis-label-color 000000 \
2021-12-08 10:37:18 +00:00
--input-filename "$arg" --output-format png >"$img" && {
timg -g $(tput cols)x2 "$arg" && printf "\\033[2A "
timg -g $(tput cols)x2 --auto-crop --upscale "$img"
}
fi
timga+=("$arg")
;;
(*:\ *compress*|*\ archive*)
2021-12-13 12:56:42 +00:00
list="$tmpfile-list.txt"
if test $# = 1
then nvim "$arg"
else case "$arg" in (*.part);; (*)
2021-12-13 12:56:42 +00:00
nvim -es "+2w$list|5,w>>$list" "$arg"
batl+=("$list");;
esac; fi
;;
(*:\ *database*) sqlite3 "$arg" ".tables";;
(*) bat+=("$arg")
timga+=("$arg")
continue;;
esac
;;
esac
fileinfo "$arg"
done
2021-12-13 12:56:42 +00:00
if $inspect; then exit $?; fi
if test "$timg"; then
$elevate timg -V --rotate=exif -g $(tput cols)x$(expr $(tput lines) / 2) \
$(test $# -gt 1 && echo "-t0.2 --center --title --grid=$(expr $(tput cols) / \( 30 - $# \& $# \< 20 \| 10 \))x2") \
"${timg[@]}" "${timga[@]}" 2>/dev/null
if test $# -lt 15; then
tput setaf 6
for img in "${timg[@]}"
do ident="$(identify -ping -precision 3 -format "%wx%h %b %m %[bit-depth]-bit %[colorspace]" "$img")"
printf "%11s %-30s %s\n" "${ident%% *}" "$(basename "$img")" "${ident#* }"
done
tput sgr0
fi
fi
2021-12-13 12:56:42 +00:00
if test "$bat" -o "$batl"; then
2021-12-14 13:31:05 +00:00
test "$(bat --version | cut -d. -f2)" -gt 16 && rule=,rule
2021-12-13 12:56:42 +00:00
if test $# -gt ${#bat[@]} -a $# -gt ${#batl[@]} && test -z "$args"
then $elevate head "${batl[@]}" "${bat[@]}" | cut -c-$(echo "$(tput cols)*1.9" | bc | cut -d. -f1)
2021-12-14 13:45:30 +00:00
else test "$bat" && $elevate bat --style numbers,header$rule --pager 'less -RF' $args "${bat[@]}"
test "$batl" && $elevate bat --style plain$rule --pager 'less -RF' $args "${batl[@]}"
2021-12-13 12:56:42 +00:00
fi
fileinfo "${bat[@]}"
fi
if test "$ls"; then
$elevate ls -l $(test $# -gt ${#ls[@]} && echo "-d") --color=always --human-readable --group-directories-first --file-type --dereference-command-line --all "${ls[@]}" | less -RF
fi