bin/b: add audio duration, improve compression & waveform display

This commit is contained in:
xeruf 2021-12-02 23:33:48 +01:00
parent 1cdb307e2a
commit 239f0c60ae
1 changed files with 34 additions and 21 deletions

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# [b]rowse - overview of given files or current directory # [b]rowse - overview of given files or current directory
# depends: tput stat bat checkaccess # depends: tput stat bat checkaccess(in dotfiles)
# optdepends: timg, neovim (compressed files), pdftoppm (PDF), mtn (video) # optdepends: timg, neovim (compressed files), pdftoppm (PDF), mtn (video)
# args: files to inspect # args: files to inspect
# #
@ -12,7 +12,7 @@
# - text files are displayed through bat # - text files are displayed through bat
# Automatically requests elevation through sudo when needed # Automatically requests elevation through sudo when needed
set -eo pipefail set -o pipefail
for last; do true; done for last; do true; done
last=${last:-.} last=${last:-.}
@ -23,10 +23,11 @@ checkperm() {
checkperm "$last" checkperm "$last"
fileinfo() { fileinfo() {
tput setaf 6
for arg for arg
do case "$arg" in (-*) continue;; esac do case "$arg" in (-*) continue;; esac
tput setaf 4 && $elevate file -E "$arg" $elevate file -E "$arg" && ffprobe "$arg" 2>&1 | grep Duration | sed 's/, start:[^,]\+,/,/'
tput setaf 3 && $elevate stat --format '%A size %sB, birth %.10w mod %.10y' "$arg" $elevate stat --format '%A size %sB, birth %.10w mod %.10y' "$arg"
done done
} }
@ -34,35 +35,43 @@ prefix=/tmp/b
mkdir -p "$prefix" mkdir -p "$prefix"
declare -a timg bat ls declare -a timg bat ls
for arg; do for arg; do
case "$arg" in (-*) args="$args $arg" continue;; esac case "$arg" in (-*) args="$args $arg"; continue;; esac
checkperm "$arg" checkperm "$arg"
grid=$(expr $(tput cols) / 20) grid=$(expr $(tput cols) / 20)
tmpfile="$prefix/$(basename "$arg")"
case "$mime" in case "$mime" in
(*\ application/pdf\;*) (*\ application/pdf\;*)
out="$prefix/$(basename "$arg")" test -f "$tmpfile-1.png" || pdftoppm -r 70 "$arg" "$tmpfile" -l $(expr $grid '*' 2)
test -f "$out-1.png" || pdftoppm -r 70 "$arg" "$out" -l $(expr $grid '*' 2) timg -W --grid=$grid "$tmpfile"*
timg -W --grid=$grid "$out"*
;; ;;
(*\ video/*) (*\ video/*)
suffix=_thumbs.jpg 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 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" 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/*) (*\ image/*)
timg+=("$arg"); continue;; timg+=("$arg"); continue;;
(*\ inode/directory\;*) (*\ inode/directory\;*)
ls+=("$arg"); continue;; ls+=("$arg"); continue;;
(*) (*)
case "$(file "$arg")" in case "$(file "$arg")" in
(*:\ *compress*) /usr/share/nvim/runtime/macros/less.sh "$arg";; (*\ ?udio*)
img="$tmpfile.png"
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 \
--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"
;;
(*:\ *compress*|*tar\ archive)
if test $# = 1
then nvim "$arg"
else case "$arg" in (*.part);; (*)
nvim -es "+2w$tmpfile|5,w>>$tmpfile" "$arg"
bat+=("$tmpfile");;
esac; fi
;;
(*) bat+=("$arg"); continue;; (*) bat+=("$arg"); continue;;
esac esac
;; ;;
@ -71,18 +80,22 @@ for arg; do
done done
if test "$timg"; then if test "$timg"; then
tput sgr0
$elevate timg --center -t0.3 $(test $# -gt 1 && $elevate timg --center -t0.3 $(test $# -gt 1 &&
echo "--title --grid=$(expr $(tput cols) / 30)" || echo "--title --grid=$(expr $(tput cols) / 30)" ||
echo "-g 60x$(expr $(tput lines) / 2)") "$@" 2>/dev/null || true echo "-g 60x$(expr $(tput lines) / 2)") "$@" 2>/dev/null
fileinfo "${timg[@]}" fileinfo "${timg[@]}"
fi fi
if test "$bat"; then if test "$bat"; then
tput sgr0
test $# -gt ${#bat[@]} && test -z "$args" &&
$elevate head "${bat[@]}" ||
$elevate bat --style header --pager 'less -RF' $args "${bat[@]}" $elevate bat --style header --pager 'less -RF' $args "${bat[@]}"
fileinfo "${bat[@]}" fileinfo "${bat[@]}"
fi fi
if test "$ls"; then if test "$ls"; then
tput sgr0 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 $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 fi