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)
|
2022-07-18 10:56:48 +00:00
|
|
|
# optdepends: timg, neovim (compressed files), pdftoppm (PDF), mtn (video), audiowaveform, imagemagick (images)
|
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
|
2023-07-27 15:16:41 +00:00
|
|
|
# TODO .raw,.iso,.qcow2
|
2020-12-10 16:34:56 +00:00
|
|
|
|
2021-12-16 22:04:50 +00:00
|
|
|
set -o pipefail
|
2022-06-27 16:21:43 +00:00
|
|
|
|
2021-12-13 12:56:42 +00:00
|
|
|
inspect=false
|
2022-06-27 16:21:43 +00:00
|
|
|
|
|
|
|
opts='itvh'
|
|
|
|
while getopts "$opts" OPTION; do
|
2022-12-25 21:05:58 +00:00
|
|
|
case "$OPTION" in
|
|
|
|
# inspect: Show file info without preview
|
|
|
|
(i) inspect=true;;
|
|
|
|
(t) tree=true;;
|
|
|
|
(v) set -eo xtrace;
|
|
|
|
export PS4='+\#> ';;
|
|
|
|
(h|?) echo "Usage: $(basename $0) [-$opts] <paths...>" && exit 2;;
|
|
|
|
esac
|
2022-06-27 16:21:43 +00:00
|
|
|
done
|
|
|
|
shift "$(($OPTIND -1))"
|
2021-12-13 12:56:42 +00:00
|
|
|
|
2021-11-30 13:53:47 +00:00
|
|
|
checkperm() {
|
|
|
|
checkaccess -r "$@" || elevate=sudo
|
|
|
|
mime="$(test -n "$shifted" || $elevate file --dereference --mime "$@")"
|
|
|
|
}
|
|
|
|
fileinfo() {
|
2023-03-31 17:09:55 +00:00
|
|
|
tput setaf 4
|
2021-11-30 13:53:47 +00:00
|
|
|
for arg
|
|
|
|
do case "$arg" in (-*) continue;; esac
|
2023-03-31 17:09:55 +00:00
|
|
|
tput smso
|
2022-05-31 23:01:54 +00:00
|
|
|
$elevate file --exclude elf -E "$arg"
|
2023-03-31 17:09:55 +00:00
|
|
|
tput rmso
|
2022-11-09 11:45:38 +00:00
|
|
|
$elevate ssh-keygen -l -f "$arg" 2>/dev/null || true
|
2022-05-12 09:32:39 +00:00
|
|
|
# TODO do not grep bitrate but extract properly
|
2021-12-11 00:59:45 +00:00
|
|
|
#probe="$($elevate ffprobe "$arg" 2>&1)"
|
|
|
|
#echo $probe | grep -v -e '00:00:00.04' -e 'ansi' &&
|
2023-01-31 17:58:36 +00:00
|
|
|
$inspect ||
|
|
|
|
$elevate ffprobe -hide_banner "$arg" 2>&1 | grep "bitrate: ....\? " | sed 's/, start:[^,]\+,/,/' ||
|
2022-03-29 11:51:18 +00:00
|
|
|
$elevate stat --format "%U:%G %A %s $(
|
2021-12-13 12:56:42 +00:00
|
|
|
size="$($elevate unzip -l "$arg" 2>/dev/null | tail -1)" &&
|
|
|
|
echo "(uncompressed $(echo $size | cut -d' ' -f1 | numfmt --to=iec-i --suffix=B))"
|
2022-03-15 08:47:14 +00:00
|
|
|
) - birth %.10w mod %.10y" "$arg" | numfmt --field=3 --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
|
2022-04-12 17:24:29 +00:00
|
|
|
case "$arg" in (-*) flags="$flags $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"
|
2022-05-10 07:25:01 +00:00
|
|
|
then if test -h "$arg"
|
|
|
|
then fileinfo "$arg"
|
|
|
|
else echo "File not found: '$arg'" 1>&2
|
|
|
|
fi
|
2022-01-08 22:09:59 +00:00
|
|
|
continue
|
|
|
|
fi
|
2022-07-20 20:06:15 +00:00
|
|
|
# amount of columns in a grid
|
2022-05-10 08:11:30 +00:00
|
|
|
grid=$(expr $(tput cols) / \( 25 - \( $# / 2 \) \& $# \< 30 \| 5 \))
|
2022-12-25 21:05:58 +00:00
|
|
|
tmpfile="$prefix/$(basename "$arg")_$(dd "if=$arg" bs=512 count=10 2>/dev/null | md5sum | tr -d ' ' || true)"
|
2022-05-09 07:19:20 +00:00
|
|
|
mkdir -p "$prefix"
|
2021-12-01 15:50:46 +00:00
|
|
|
case "$mime" in
|
|
|
|
(*\ application/pdf\;*)
|
2022-09-27 19:58:26 +00:00
|
|
|
grid=$(expr $(tput cols) \* $# / $(tput lines))
|
|
|
|
grid=$(expr 3 \& $grid \< 3 \| $grid)
|
|
|
|
#limit=$(expr $grid \& \( $grid \> 3 \| $# \> 1 \) \| $grid '*' 2)
|
|
|
|
limit=$grid
|
2022-11-22 12:24:52 +00:00
|
|
|
if ! test -f "$tmpfile-1.ppm"
|
|
|
|
then echo Converting "$arg"
|
|
|
|
pdftoppm -forcenum -r 70 "$arg" "$tmpfile" -l $limit
|
|
|
|
fi
|
2022-09-27 19:58:26 +00:00
|
|
|
find "$prefix" -path "$tmpfile*.ppm" | sort | head -$limit | xargs -d'\n' timg -W --grid=$grid
|
2021-12-01 15:50:46 +00:00
|
|
|
;;
|
2022-09-27 19:58:26 +00:00
|
|
|
(*\ application/*document*|*.xlsx:\ *)
|
2022-01-15 21:01:41 +00:00
|
|
|
# https://ask.libreoffice.org/t/convert-to-command-line-parameter/840/4
|
|
|
|
echo Converting "$arg"
|
2022-04-10 09:37:26 +00:00
|
|
|
soffice --headless --convert-to png --outdir "$prefix" "$arg" >/dev/null
|
2022-07-18 10:56:48 +00:00
|
|
|
timg+=("${tmpfile%.*}.png");;
|
2022-01-15 21:01:41 +00:00
|
|
|
(*/x-xcf*)
|
|
|
|
echo Converting "$arg"
|
|
|
|
convert -flatten "$arg" png:"$tmpfile"
|
2022-07-18 10:56:48 +00:00
|
|
|
timg+=("$tmpfile");;
|
2021-12-01 15:50:46 +00:00
|
|
|
(*\ video/*)
|
|
|
|
suffix=_thumbs.jpg
|
2023-01-31 17:58:36 +00:00
|
|
|
mtn -q -i -t -W -r$(expr 5 - $# \& $# \< 4 \| 1) -D6 -b 0.6 -c $grid -w $(expr $(tput cols) '*' 20) \
|
2022-12-13 12:25:04 +00:00
|
|
|
-O "$prefix" -o "$suffix" "$arg" &&
|
2021-12-01 15:50:46 +00:00
|
|
|
timg -W "$prefix/$(basename "${arg%.*}")$suffix"
|
|
|
|
;;
|
|
|
|
(*\ image/*)
|
2022-07-18 10:56:48 +00:00
|
|
|
timg+=("$arg")
|
2022-08-04 21:54:21 +00:00
|
|
|
which identify >/dev/null && continue;;
|
2021-12-01 15:50:46 +00:00
|
|
|
(*\ inode/directory\;*)
|
2022-01-25 12:55:23 +00:00
|
|
|
ls+=("$arg")
|
|
|
|
test -L "$arg" || continue
|
|
|
|
;;
|
2021-12-01 15:50:46 +00:00
|
|
|
(*)
|
2022-05-03 09:22:10 +00:00
|
|
|
case "$(file --dereference "$arg")" in
|
2022-11-09 11:45:38 +00:00
|
|
|
(*\ ?udio*|*\ ADTS,*)
|
2022-04-22 11:13:09 +00:00
|
|
|
# TODO preconvert aac - |*\ ADTS\ *
|
2022-08-30 13:19:41 +00:00
|
|
|
if ! $inspect && which audiowaveform >/dev/null; then
|
2022-07-06 06:59:22 +00:00
|
|
|
img="$tmpfile.png"
|
|
|
|
case $TERM in (*-kitty) kitty=true; audioheight=2;; (*) audioheight=5;; esac
|
|
|
|
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" && {
|
2022-02-10 15:01:25 +00:00
|
|
|
test "$kitty" && timg -g x$audioheight "$arg" &&
|
|
|
|
printf "\\033[${audioheight}A%$(expr $audioheight \* 3)s"
|
|
|
|
timg -g x$audioheight --auto-crop --upscale "$img"
|
2021-12-08 10:37:18 +00:00
|
|
|
}
|
|
|
|
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"
|
2022-08-04 21:54:21 +00:00
|
|
|
case "$arg" in
|
|
|
|
(*.7z) test $# = 1 && 7z l "$arg";;
|
|
|
|
(*.tar*) tar --list --file "$arg";;
|
|
|
|
(*)
|
|
|
|
if test $# = 1
|
|
|
|
then nvim "$arg"
|
|
|
|
else case "$arg" in (*.part);; (*)
|
|
|
|
nvim -es "+2w$list|5,w>>$list" "$arg"
|
|
|
|
batplain+=("$list");;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
esac
|
2021-12-02 22:33:48 +00:00
|
|
|
;;
|
2022-12-13 12:25:04 +00:00
|
|
|
(*:\ SQLite\ *\ database*) highlight "Tables" && sqlite3 "$arg" ".tables";;
|
2022-05-03 09:22:10 +00:00
|
|
|
(*:\ data) ;;
|
2023-03-31 17:09:55 +00:00
|
|
|
(*\ key) bat+=("$arg");;
|
2021-12-11 00:59:45 +00:00
|
|
|
(*) bat+=("$arg")
|
|
|
|
timga+=("$arg")
|
2022-08-04 21:54:21 +00:00
|
|
|
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
|
2022-04-12 16:49:08 +00:00
|
|
|
# TODO Don't show info on all images for gifs
|
2022-07-20 20:06:15 +00:00
|
|
|
$inspect ||
|
2022-08-04 21:54:21 +00:00
|
|
|
if which timg >/dev/null
|
2022-07-20 20:06:15 +00:00
|
|
|
then $elevate timg $(test "$timga" && echo "-V") --rotate=exif -g $(tput cols)x$(expr $(tput lines) / 2) \
|
|
|
|
$(test $# -gt 1 &&
|
2022-11-22 12:24:52 +00:00
|
|
|
echo "-t0.2 --auto-crop --center $(test $# -lt 20 && echo "--title") --grid=$((grid < $# ? grid : $#))x2") \
|
2022-07-20 20:06:15 +00:00
|
|
|
"${timg[@]}" "${timga[@]}" 2>/dev/null || true
|
|
|
|
else for img in "${timg[@]}"
|
|
|
|
do catimg -H $(expr $(tput lines) / 2) $img
|
|
|
|
done
|
|
|
|
fi
|
2022-08-04 21:54:21 +00:00
|
|
|
if which identify >/dev/null && ( $inspect || test $# -lt 10 ); then
|
2021-12-12 10:51:24 +00:00
|
|
|
tput setaf 6
|
|
|
|
for img in "${timg[@]}"
|
2022-08-04 21:54:21 +00:00
|
|
|
do case $img in (*.gif) continue;; esac
|
|
|
|
ident="$(identify -ping -precision 3 -format "%wx%h %b %m %[bit-depth]-bit %[colorspace]" "$img")"
|
2021-12-12 10:51:24 +00:00
|
|
|
printf "%11s %-30s %s\n" "${ident%% *}" "$(basename "$img")" "${ident#* }"
|
|
|
|
done
|
|
|
|
tput sgr0
|
|
|
|
fi
|
2021-12-01 15:50:46 +00:00
|
|
|
fi
|
|
|
|
|
2023-06-12 09:08:37 +00:00
|
|
|
pager="${PAGER:-'less -rF'}"
|
2022-01-15 21:01:41 +00:00
|
|
|
# bat: unknown files
|
|
|
|
# batplain: files to print without header
|
2022-03-15 08:47:14 +00:00
|
|
|
if test "$bat" -o "$batplain"; then
|
2022-12-13 12:25:04 +00:00
|
|
|
# TODO allow plain less
|
2022-01-15 21:01:41 +00:00
|
|
|
test "$(bat --version | cut -d. -f2)" -gt 16 && rule=,rule
|
2022-04-12 17:24:29 +00:00
|
|
|
if test $# -gt ${#bat[@]} -a $# -gt ${#batplain[@]} && test -z "$flags"
|
2022-01-15 21:01:41 +00:00
|
|
|
then cut="--line-range :7"
|
2022-06-13 13:49:14 +00:00
|
|
|
batpager="cut -c-$(expr $(tput cols) \* 19 / 10 | cut -d. -f1)"
|
2022-01-15 21:01:41 +00:00
|
|
|
fi
|
2022-04-12 17:24:29 +00:00
|
|
|
batcommand="$elevate bat $cut $flags --pager"
|
2022-01-15 21:01:41 +00:00
|
|
|
batstyle="--style plain$rule"
|
2022-05-09 07:19:20 +00:00
|
|
|
test "$batplain" && $batcommand "${batpager:-$pager}" $batstyle "${batplain[@]}"
|
2022-08-30 13:19:41 +00:00
|
|
|
test "$bat" &&
|
|
|
|
if test "$cut" && ! $inspect
|
2022-01-15 21:01:41 +00:00
|
|
|
then case $TERM in (*kitty)
|
|
|
|
declare -a timgtxt
|
2022-05-09 07:19:20 +00:00
|
|
|
for file in "${bat[@]}"
|
|
|
|
do txt="$prefix/$(basename "$file").txt"
|
|
|
|
cp "$file" "$txt"
|
|
|
|
timgtxt+=("$txt")
|
|
|
|
done
|
2022-12-25 21:05:58 +00:00
|
|
|
timg -V --grid="$(expr 4 \& ${#bat[@]} \> 5 \| ${#bat[@]})" --title="%b" --frames=3 "${timgtxt[@]}" ||
|
|
|
|
head -3 "${timgtxt[@]}"
|
2022-01-15 21:01:41 +00:00
|
|
|
#fileinfo "${bat[@]}"
|
|
|
|
;;
|
|
|
|
(*) for file in "${bat[@]}"; do
|
2022-05-09 07:19:20 +00:00
|
|
|
$batcommand "${batpager:-$pager}" $batstyle "$file"
|
2022-01-15 21:01:41 +00:00
|
|
|
fileinfo "$file"
|
|
|
|
done;;
|
|
|
|
esac
|
2022-08-30 13:19:41 +00:00
|
|
|
else
|
|
|
|
if ! $inspect
|
|
|
|
then json=false
|
|
|
|
if which cj >/dev/null; then
|
|
|
|
json=true
|
|
|
|
for file in "${bat[@]}"; do
|
2022-10-13 19:57:22 +00:00
|
|
|
case $file in (*.json|*.geojson);; (*) json=false;; esac
|
2022-08-30 13:19:41 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
if $json
|
|
|
|
then cj "${bat[@]}"
|
|
|
|
else $batcommand "${batpager:-$pager}" $batstyle,header$(test $# -gt 1 && echo ",numbers") "${bat[@]}"
|
|
|
|
fi
|
|
|
|
fi
|
2022-12-13 12:25:04 +00:00
|
|
|
if $inspect || test $# -lt $(expr $(tput lines) / 10);
|
2022-08-30 13:19:41 +00:00
|
|
|
then fileinfo "${bat[@]}"
|
|
|
|
fi
|
2021-12-15 18:43:45 +00:00
|
|
|
fi
|
2021-12-01 15:50:46 +00:00
|
|
|
fi
|
|
|
|
|
2022-04-12 17:24:29 +00:00
|
|
|
if test "$ls" -o $# -le $(echo "$flags" | wc -w); then
|
2021-12-16 22:04:50 +00:00
|
|
|
checkperm .
|
2022-04-10 09:37:26 +00:00
|
|
|
# Alternative: find -exec ls -dl {} +
|
2022-04-12 17:24:29 +00:00
|
|
|
{
|
2022-07-06 06:59:22 +00:00
|
|
|
# TODO handle single quotes in filenames
|
2022-08-04 21:54:21 +00:00
|
|
|
# TODO listing sometimes doubles as exa prints partial listings while working
|
2022-05-09 07:19:20 +00:00
|
|
|
timeout .6s sh -c "
|
2022-06-27 16:21:43 +00:00
|
|
|
if test '$tree'
|
|
|
|
then $elevate tree -a --dirsfirst --du -h -C -L 3 $flags $(printf "'%s' " "${ls[@]:-.}")
|
|
|
|
elif which exa 2>/dev/null >&2
|
2022-12-13 12:25:04 +00:00
|
|
|
then $elevate exa --icons --color=always --long --group --classify --all --all --sort=modified --reverse $flags $(printf "'%s' " "${ls[@]:-.}")
|
2022-05-10 08:11:30 +00:00
|
|
|
else $elevate ls -l $(test $# -gt ${#ls[@]} && echo '-d') --color=always --human-readable --si --group-directories-first --file-type --dereference-command-line-symlink-to-dir --all $flags $(printf "'%s' " "${ls[@]:-.}")
|
2022-04-12 17:24:29 +00:00
|
|
|
fi
|
|
|
|
" || $elevate ls $(test $# -gt ${#ls[@]} && echo "-d") --color=always --human-readable --si --dereference-command-line --all --sort=none $flags "${ls[@]:-.}"
|
2022-05-03 09:22:10 +00:00
|
|
|
} | $pager --quit-if-one-screen
|
2021-12-01 15:50:46 +00:00
|
|
|
fi
|