bin: all sorts of convenience fixes

This commit is contained in:
xeruf 2021-12-16 23:04:50 +01:00
parent 130790b92e
commit 1637508a45
6 changed files with 29 additions and 25 deletions

View File

@ -12,25 +12,19 @@
# - 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 -o pipefail
test "$1" = "-v" && test "$1" = "-v" &&
set -eo xtrace && set -eo xtrace &&
shift shift
inspect=false inspect=false
test "$1" = "-i" && test "$1" = "-i" &&
inspect=true && inspect=true &&
shift shift
set -o pipefail
for last; do true; done
last="${last:-.}"
checkperm() { checkperm() {
checkaccess -r "$@" || elevate=sudo checkaccess -r "$@" || elevate=sudo
mime="$(test -n "$shifted" || $elevate file --dereference --mime "$@")" mime="$(test -n "$shifted" || $elevate file --dereference --mime "$@")"
} }
checkperm "$last"
fileinfo() { fileinfo() {
tput setaf 6 tput setaf 6
for arg for arg
@ -106,7 +100,8 @@ done
if test "$timg"; then if test "$timg"; then
$inspect || $elevate timg $(test "$timga" && echo "-V") --rotate=exif -g $(tput cols)x$(expr $(tput lines) / 2) \ $inspect || $elevate timg $(test "$timga" && echo "-V") --rotate=exif -g $(tput cols)x$(expr $(tput lines) / 2) \
$(test $# -gt 1 && echo "-t0.2 --center --title --grid=$(expr $(tput cols) / \( 30 - $# \& $# \< 20 \| 10 \))x2") \ $(test $# -gt 1 && grid=$(expr $(tput cols) / \( 30 - $# \& $# \< 20 \| 10 \)) &&
echo "-t0.2 --center --title --grid=$((grid*3 < $# ? $#/3 + 1 : grid < $# ? grid : $#))x2") \
"${timg[@]}" "${timga[@]}" 2>/dev/null "${timg[@]}" "${timga[@]}" 2>/dev/null
if $inspect || test $# -lt 15; then if $inspect || test $# -lt 15; then
tput setaf 6 tput setaf 6
@ -128,6 +123,7 @@ if test "$bat" -o "$batl"; then
fileinfo "${bat[@]}" fileinfo "${bat[@]}"
fi fi
if test "$ls"; then if test "$ls" -o $# -eq 0; then
$elevate ls -l $(test $# -gt ${#ls[@]} && echo "-d") --color=always --human-readable --group-directories-first --file-type --dereference-command-line --all "${ls[@]}" | less -RF 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
fi fi

View File

@ -1,8 +1,8 @@
#!/bin/sh #!/bin/sh
# Edit a file with appropriate rights, creating parent directories if necessary # Edit a file with appropriate rights, creating parent directories if necessary
test ! -f "$1" && ( mkdir -p $(dirname "$1") || sudo mkdir -p $(dirname "$1") ) test ! -f "$1" && ( mkdir -p $(dirname "$1") || sudo mkdir -p $(dirname "$1") )
echo "Editing $1..." echo "Editing $@..."
if test -w "$1" || (test ! -f "$1" && test -w $(dirname "$1")) if test -w "$1" || (test ! -f "$1" && test -w $(dirname "$1"))
then $EDITOR "$1" then $EDITOR "$@"
else sudoedit "$1" else sudoedit "$@"
fi fi

View File

@ -8,9 +8,10 @@ case "$1" in
(git:*) echo "$1" ;; (git:*) echo "$1" ;;
(*) host=$1 (*) host=$1
case $1 in case $1 in
(socha) user=software-challenge; host=git@github.com;;
(hub) host=git@github.com;; (hub) host=git@github.com;;
(lab) host=git@gitlab.com;; (lab) host=git@gitlab.com;;
(*) host=$1;; (*) host=$1;;
esac esac
echo "${host:-git.jfischer.org}:${3:-$(git config user.name)}/${2:-$(basename $PWD)}.git" ;; echo "${host:-git.jfischer.org}:${3:-${user:-$(git config user.name)}}/${2:-$(basename $PWD)}.git" ;;
esac esac

View File

@ -10,10 +10,11 @@ case $1 in
LESS="$LESS +/^ *$last *\\[" man zshall;; LESS="$LESS +/^ *$last *\\[" man zshall;;
(vlc) unbuffer vlc --full-help "${@:2}" | $paginate;; (vlc) unbuffer vlc --full-help "${@:2}" | $paginate;;
(gh|chordpro|bat) unbuffer "$@" --help | $paginate;; (gh|chordpro|bat) unbuffer "$@" --help | $paginate;;
(plantuml) unbuffer "$@" -help | $paginate;;
(caddy|stretchly|go) "$1" help "${@:2}" | $paginate;; (caddy|stretchly|go) "$1" help "${@:2}" | $paginate;;
(*) man "$@" || (*) man "$@" ||
{ info "$1" -w | grep -v "manpages" && pinfo "$@"; } || { info "$1" -w | grep -v "manpages" && pinfo "$@"; } ||
if which "$1" >/dev/null; then "$@" --help || "$@" -help 2>&1 | $paginate; fi;; if which "$1" >/dev/null; then "$@" --help; "$@" -help 2>&1 | $paginate; fi;;
esac esac
docs="/usr/share/doc/$1" docs="/usr/share/doc/$1"
if test -d "$docs"; then if test -d "$docs"; then

View File

@ -28,9 +28,9 @@ case "$arg" in
esac esac
shift $(expr 2 \& $# \> 1 \| 1) shift $(expr 2 \& $# \> 1 \| 1)
#uid=$(id --user),gid=$(id --group), \ #uid=$(id --user),gid=$(id --group), \
mountpoint "$mountpoint" >/dev/null 2>&1 || if ! mountpoint "$mountpoint" 2>/dev/null
{ mp="/run/media/$USER/$arg" && test -e "$mp" && mountpoint="$mp"; } || then mp="/run/media/$USER/$arg" && test -e "$mp" && mountpoint="$mp"
sudo mount -vo users,X-mount.mkdir,noatime $partition $mountpoint "$@" sudo mount -vo users,X-mount.mkdir,noatime $partition $mountpoint "$@"
echo "Mounted at $mountpoint" fi
cd $mountpoint cd $mountpoint
exec $SHELL exec $SHELL

View File

@ -1,11 +1,17 @@
#!/bin/sh #!/bin/sh
# Lossy image compression using ImageMagick
# Tries to eliminate artifacts
while true while true
do case $1 in do case $1 in
(-o) out="$2"; shift 2;; (-o) out="$2"; shift 2;;
(-q) quality="$2"; shift 2;; (-q) quality="$2"; shift 2;;
(*) break;; (*) break;;
esac esac
done done
out="${out:-$1-shrinked.jpg}"
test $# -eq 0 && echo "Usage: $0 [-q quality (default 85)] [-o outfile] <images...>" && exit 1 test $# -eq 0 && echo "Usage: $0 [-q quality (default 85)] [-o outfile] <images...>" && exit 1
magick "$@" -strip -interlace Plane -define jpeg:dct-method=float -sampling-factor 4:2:0 -gaussian-blur 0.05 -quality "${quality:-85}" "$out" magick "$@" -auto-orient -strip -interlace Plane -define jpeg:dct-method=float -sampling-factor 4:2:0 -gaussian-blur 0.05 \
-quality "${quality:-85}" "$out"
printf "Shrinked $1(%s) to $out(%s) - reduced to %s%%\n" \
$(stat --format "%s" IMG_20211124_115930.jpg IMG_20211124_115930.jpg-shrinked.jpg | numfmt --to=iec-i --suffix=B) \
$(stat --format %s "$out" "$1" | sed 'N;s|\n|*100/|' | bc)