2021-09-20 09:14:34 +00:00
|
|
|
#!/bin/bash
|
2021-11-30 13:53:47 +00:00
|
|
|
# [b]rowse - overview of given files or current directory
|
|
|
|
# depends: tput stat bat checkaccess
|
|
|
|
# optdepends: timg, neovim (compressed files), pdftoppm (PDF), mtn (video)
|
2021-11-02 12:00:02 +00:00
|
|
|
# args: files to inspect
|
2021-11-30 13:53:47 +00:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
2021-07-13 10:15:58 +00:00
|
|
|
set -eo pipefail
|
2020-11-16 11:29:31 +00:00
|
|
|
for last; do true; done
|
|
|
|
last=${last:-.}
|
2020-12-10 16:34:56 +00:00
|
|
|
|
2021-11-30 13:53:47 +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
|
|
|
|
2021-11-30 13:53:47 +00:00
|
|
|
fileinfo() {
|
|
|
|
for arg
|
|
|
|
do case "$arg" in (-*) continue;; esac
|
|
|
|
tput setaf 4 && $elevate file -E "$arg"
|
|
|
|
tput setaf 3 && $elevate stat --format '%A size %sB, birth %.10w mod %.10y' "$arg"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:50:46 +00:00
|
|
|
prefix=/tmp/b
|
|
|
|
mkdir -p "$prefix"
|
|
|
|
declare -a timg bat ls
|
|
|
|
for arg; do
|
|
|
|
case "$arg" in (-*) args="$args $arg" continue;; esac
|
|
|
|
checkperm "$arg"
|
|
|
|
grid=$(expr $(tput cols) / 20)
|
|
|
|
case "$mime" in
|
|
|
|
(*\ application/pdf\;*)
|
|
|
|
out="$prefix/$(basename "$arg")"
|
|
|
|
test -f "$out-1.png" || pdftoppm -r 70 "$arg" "$out" -l $(expr $grid '*' 2)
|
|
|
|
timg -W --grid=$grid "$out"*
|
|
|
|
;;
|
|
|
|
(*\ video/*)
|
|
|
|
suffix=_thumbs.jpg
|
|
|
|
mtn -i -t -W -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"
|
|
|
|
;;
|
|
|
|
(*\ audio/*)
|
|
|
|
img="$prefix/$arg.png"
|
|
|
|
find "$img" -not -empty 2>/dev/null | grep --quiet . ||
|
|
|
|
audiowaveform --quiet --pixels-per-second 5 --height 40 --width 2000 \
|
|
|
|
--background-color 000000 --waveform-color 99BBFF --axis-label-color 000000 \
|
|
|
|
--input-filename "$arg" --output-format png >"$img" &&
|
|
|
|
timg -g $(tput cols)x2 --upscale "$img"
|
|
|
|
;;
|
|
|
|
(*\ image/*)
|
|
|
|
timg+=("$arg"); continue;;
|
|
|
|
(*\ inode/directory\;*)
|
|
|
|
ls+=("$arg"); continue;;
|
|
|
|
(*)
|
|
|
|
case "$(file "$arg")" in
|
|
|
|
(*:\ *compress*) /usr/share/nvim/runtime/macros/less.sh "$arg";;
|
|
|
|
(*) bat+=("$arg"); continue;;
|
2021-11-30 13:53:47 +00:00
|
|
|
esac
|
2021-12-01 15:50:46 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fileinfo "$arg"
|
|
|
|
done
|
|
|
|
|
|
|
|
if test "$timg"; then
|
|
|
|
$elevate timg --center -t0.3 $(test $# -gt 1 &&
|
|
|
|
echo "--title --grid=$(expr $(tput cols) / 30)" ||
|
|
|
|
echo "-g 60x$(expr $(tput lines) / 2)") "$@" 2>/dev/null || true
|
|
|
|
fileinfo "${timg[@]}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test "$bat"; then
|
|
|
|
$elevate bat --style header --pager 'less -RF' $args "${bat[@]}"
|
|
|
|
fileinfo "${bat[@]}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test "$ls"; then
|
|
|
|
tput sgr0
|
|
|
|
$(test -x "$arg" || echo "sudo") ls -l $(test $# -gt ${#ls[@]} && echo "-d") --color=always --human-readable --group-directories-first --file-type --dereference-command-line --all "${ls[@]}" | less -RF
|
|
|
|
fi
|