bin: improve image handling

Also add sqlite3 support to b
This commit is contained in:
xeruf 2021-12-11 01:59:45 +01:00
parent 2f9559afe6
commit 2c0e42dfa4
2 changed files with 36 additions and 22 deletions

View File

@ -12,6 +12,10 @@
# - text files are displayed through bat
# Automatically requests elevation through sudo when needed
test "$1" = "-v" &&
set -eo xtrace &&
shift
set -o pipefail
for last; do true; done
last=${last:-.}
@ -26,15 +30,18 @@ fileinfo() {
tput setaf 6
for arg
do case "$arg" in (-*) continue;; esac
$elevate file -E "$arg" &&
ffprobe "$arg" 2>&1 | grep Duration | grep -v '00:00:00.04' | sed 's/, start:[^,]\+,/,/' ||
$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:[^,]\+,/,/' ||
stat --format '%A %s - birth %.10w mod %.10y' "$arg" | numfmt --field=2 --to=iec-i --padding=6 --suffix=B
done
tput sgr0
}
prefix=/tmp/b
mkdir -p "$prefix"
declare -a timg bat ls
declare -a timg timga bat ls
for arg; do
case "$arg" in (-*) args="$args $arg"; continue;; esac
checkperm "$arg"
@ -47,7 +54,7 @@ for arg; do
;;
(*\ video/*)
suffix=_thumbs.jpg
mtn -i -t -W -D6 -b 0,6 -c $grid -w $(expr $(tput cols) '*' 20) \
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"
;;
@ -68,6 +75,7 @@ for arg; do
timg -g $(tput cols)x2 --auto-crop --upscale "$img"
}
fi
timga+=("$arg")
;;
(*:\ *compress*|*\ archive*)
if test $# = 1
@ -77,7 +85,10 @@ for arg; do
bat+=("$tmpfile");;
esac; fi
;;
(*) bat+=("$arg"); continue;;
(*:\ *database*) sqlite3 "$arg" ".tables";;
(*) bat+=("$arg")
timga+=("$arg")
continue;;
esac
;;
esac
@ -85,24 +96,25 @@ for arg; do
done
if test "$timg"; then
tput sgr0
$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
$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) / 20)x2") \
"${timg[@]}" "${timga[@]}" 2>/dev/null
tput setaf 6
for img in "${timg[@]}"
do printf "%11s %-30s %s\n" "$(identify -format "%wx%h" "$img")" "$img" "$(identify -precision 3 -format "%b %m %[bit-depth]-bit %[colorspace]" "$img")"
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
if test "$bat"; then
tput sgr0
test $# -gt ${#bat[@]} && test -z "$args" &&
$elevate head "${bat[@]}" ||
$elevate head "${bat[@]}" | cut -c-$(echo "$(tput cols)*1.9" | bc | cut -d. -f1) ||
$elevate bat --style header --pager 'less -RF' $args "${bat[@]}"
fileinfo "${bat[@]}"
fi
if test "$ls"; then
tput sgr0
$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

View File

@ -1,11 +1,13 @@
#!/bin/bash
#!/bin/sh
# Compare select terminal image viewers
# Args: <height> [images]...
runti() {
which "$1" >/dev/null &&
highlight "$*" && "$@"
}
cols="$(expr $(tput cols) / $1 / 3 \| 1)"
runti timg --rotate=exif --title --grid=${cols} -g $(tput cols)x$(expr $1 \* $cols + 4) "${@:2}"
runti viu --name -h $1 "${@:2}"
runti tiv -h $1 -c $(expr $cols + 1) "${@:2}"
set -x
size=10
if test $# -eq 0
then args=$DATA/images/*.png
else echo "$1" | grep -v -e "\." -e "/" && size=$1 && shift
fi
cols="$(expr $(tput cols) / $size / 3 \| 1)"
timg --rotate=exif --title --grid=${cols} -g $(tput cols)x$(expr $size \* $cols + 4) "$@" $args
viu --name -h $size "$@" $args
tiv -h $size -c $(expr $cols + 1) "$@" $args