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
|
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
|
2022-01-15 21:01:41 +00:00
|
|
|
# args: files to inspect, any arg starting with dash is passed on to bat
|
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-12-16 22:04:50 +00:00
|
|
|
set -o pipefail
|
2021-12-11 00:59:45 +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
|
|
|
|
|
2021-11-30 13:53:47 +00:00
|
|
|
checkperm() {
|
|
|
|
checkaccess -r "$@" || elevate=sudo
|
|
|
|
mime="$(test -n "$shifted" || $elevate file --dereference --mime "$@")"
|
|
|
|
}
|
|
|
|
fileinfo() {
|
2021-12-02 22:33:48 +00:00
|
|
|
tput setaf 6
|
2021-11-30 13:53:47 +00:00
|
|
|
for arg
|
|
|
|
do case "$arg" in (-*) continue;; esac
|
2021-12-11 00:59:45 +00:00
|
|
|
$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
|
2021-11-30 13:53:47 +00:00
|
|
|
done
|
2021-12-11 00:59:45 +00:00
|
|
|
tput sgr0
|
2021-11-30 13:53:47 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 15:50:46 +00:00
|
|
|
prefix=/tmp/b
|
|
|
|
mkdir -p "$prefix"
|
2022-01-15 21:01:41 +00:00
|
|
|
declare -a timg timga bat batplain ls
|
2021-12-01 15:50:46 +00:00
|
|
|
for arg; do
|
2021-12-02 22:33:48 +00:00
|
|
|
case "$arg" in (-*) args="$args $arg"; continue;; esac
|
2021-12-01 15:50:46 +00:00
|
|
|
checkperm "$arg"
|
2022-01-08 22:09:59 +00:00
|
|
|
if ! $elevate test -e "$arg"
|
|
|
|
then echo "File not found: '$arg'" 1>&2
|
|
|
|
continue
|
|
|
|
fi
|
2021-12-01 15:50:46 +00:00
|
|
|
grid=$(expr $(tput cols) / 20)
|
2021-12-02 22:33:48 +00:00
|
|
|
tmpfile="$prefix/$(basename "$arg")"
|
2021-12-01 15:50:46 +00:00
|
|
|
case "$mime" in
|
|
|
|
(*\ application/pdf\;*)
|
2022-01-15 21:01:41 +00:00
|
|
|
echo Converting "$arg"
|
2021-12-02 22:33:48 +00:00
|
|
|
test -f "$tmpfile-1.png" || pdftoppm -r 70 "$arg" "$tmpfile" -l $(expr $grid '*' 2)
|
|
|
|
timg -W --grid=$grid "$tmpfile"*
|
2021-12-01 15:50:46 +00:00
|
|
|
;;
|
2022-01-15 21:01:41 +00:00
|
|
|
(*\ application/*document*)
|
|
|
|
# https://ask.libreoffice.org/t/convert-to-command-line-parameter/840/4
|
|
|
|
echo Converting "$arg"
|
|
|
|
soffice --headless --convert-to jpg --outdir "$prefix" "$arg" >/dev/null 2>&1
|
|
|
|
timg+=("${tmpfile%.*}.jpg")
|
|
|
|
continue;;
|
|
|
|
(*/x-xcf*)
|
|
|
|
echo Converting "$arg"
|
|
|
|
convert -flatten "$arg" png:"$tmpfile"
|
|
|
|
timg+=("$tmpfile")
|
|
|
|
continue;;
|
2021-12-01 15:50:46 +00:00
|
|
|
(*\ video/*)
|
|
|
|
suffix=_thumbs.jpg
|
2021-12-11 00:59:45 +00:00
|
|
|
mtn -i -t -W -r2 -D6 -b 0,6 -c $grid -w $(expr $(tput cols) '*' 20) \
|
2021-12-10 15:00:27 +00:00
|
|
|
-O $prefix -o $suffix "$arg" 2>/dev/null
|
2021-12-01 15:50:46 +00:00
|
|
|
timg -W "$prefix/$(basename "${arg%.*}")$suffix"
|
|
|
|
;;
|
|
|
|
(*\ image/*)
|
|
|
|
timg+=("$arg"); continue;;
|
|
|
|
(*\ inode/directory\;*)
|
2022-01-25 12:55:23 +00:00
|
|
|
ls+=("$arg")
|
|
|
|
test -L "$arg" || continue
|
|
|
|
;;
|
2021-12-01 15:50:46 +00:00
|
|
|
(*)
|
|
|
|
case "$(file "$arg")" in
|
2021-12-02 22:33:48 +00:00
|
|
|
(*\ ?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 . ||
|
2021-12-02 22:33:48 +00:00
|
|
|
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
|
2021-12-11 00:59:45 +00:00
|
|
|
timga+=("$arg")
|
2021-12-02 22:33:48 +00:00
|
|
|
;;
|
2021-12-10 15:00:27 +00:00
|
|
|
(*:\ *compress*|*\ archive*)
|
2021-12-13 12:56:42 +00:00
|
|
|
list="$tmpfile-list.txt"
|
2021-12-02 22:33:48 +00:00
|
|
|
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"
|
2022-01-15 21:01:41 +00:00
|
|
|
batplain+=("$list");;
|
2021-12-02 22:33:48 +00:00
|
|
|
esac; fi
|
|
|
|
;;
|
2021-12-11 00:59:45 +00:00
|
|
|
(*:\ *database*) sqlite3 "$arg" ".tables";;
|
|
|
|
(*) bat+=("$arg")
|
|
|
|
timga+=("$arg")
|
|
|
|
continue;;
|
2021-11-30 13:53:47 +00:00
|
|
|
esac
|
2021-12-01 15:50:46 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fileinfo "$arg"
|
|
|
|
done
|
|
|
|
|
2022-01-15 21:01:41 +00:00
|
|
|
# timg: images
|
|
|
|
# timga: potentially viewable as image
|
2021-12-01 15:50:46 +00:00
|
|
|
if test "$timg"; then
|
2021-12-15 18:43:45 +00:00
|
|
|
$inspect || $elevate timg $(test "$timga" && echo "-V") --rotate=exif -g $(tput cols)x$(expr $(tput lines) / 2) \
|
2021-12-23 20:39:55 +00:00
|
|
|
$(test $# -gt 1 && grid=$(expr $(tput cols) / \( 30 - $# \& $# \< 25 \| 5 \)) &&
|
|
|
|
echo "-t0.2 --center $(test $# -lt 30 && echo "--title") --grid=$((grid < $# ? grid : $#))x2") \
|
2022-01-15 21:01:41 +00:00
|
|
|
"${timg[@]}" "${timga[@]}" 2>/dev/null || true
|
2022-01-12 11:44:28 +00:00
|
|
|
if $inspect || test $# -lt 10; then
|
2021-12-12 10:51:24 +00:00
|
|
|
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
|
2021-12-01 15:50:46 +00:00
|
|
|
fi
|
|
|
|
|
2022-01-15 21:01:41 +00:00
|
|
|
# bat: unknown files
|
|
|
|
# batplain: files to print without header
|
|
|
|
if test "$bat" -o "$batplain" && ! $inspect; then
|
|
|
|
test "$(bat --version | cut -d. -f2)" -gt 16 && rule=,rule
|
|
|
|
if test $# -gt ${#bat[@]} -a $# -gt ${#batplain[@]} && test -z "$args"
|
|
|
|
then cut="--line-range :7"
|
|
|
|
pager="cut -c-$(echo "$(tput cols)*1.9" | bc | cut -d. -f1)"
|
|
|
|
else pager="less -RF"
|
|
|
|
fi
|
|
|
|
batcommand="$elevate bat $cut $args --pager"
|
|
|
|
batstyle="--style plain$rule"
|
|
|
|
test "$batplain" && $batcommand $batstyle "${batplain[@]}"
|
|
|
|
test "$bat" && if test "$cut"
|
|
|
|
then case $TERM in (*kitty)
|
|
|
|
declare -a timgtxt
|
|
|
|
for file in "${bat[@]}"; do cp "$file" "$prefix/$file.txt"; timgtxt+=("$prefix/$file.txt"); done
|
|
|
|
timg -V --grid="$(expr 4 \& ${#bat[@]} \> 5 \| ${#bat[@]})" --title="%b" --frames=3 "${timgtxt[@]}"
|
|
|
|
#fileinfo "${bat[@]}"
|
|
|
|
;;
|
|
|
|
(*) for file in "${bat[@]}"; do
|
|
|
|
$batcommand "$pager" $batstyle "$file"
|
|
|
|
fileinfo "$file"
|
|
|
|
done;;
|
|
|
|
esac
|
|
|
|
else $batcommand "$pager" $batstyle,header$(test $# -gt 1 && echo ",numbers") "${bat[@]}"
|
|
|
|
fileinfo "${bat[@]}"
|
2021-12-15 18:43:45 +00:00
|
|
|
fi
|
2021-12-01 15:50:46 +00:00
|
|
|
fi
|
|
|
|
|
2021-12-16 22:04:50 +00:00
|
|
|
if test "$ls" -o $# -eq 0; then
|
|
|
|
checkperm .
|
|
|
|
$elevate ls -l $(test $# -gt ${#ls[@]} && echo "-d") --color=always --human-readable --si --group-directories-first --file-type --dereference-command-line --all "${ls[@]:-.}" | less -RF
|
2021-12-01 15:50:46 +00:00
|
|
|
fi
|