config/shell/functions: generify resolving alias and get more help

This commit is contained in:
xeruf 2022-08-11 14:15:24 +02:00
parent 856b33132b
commit 4893de59ac
2 changed files with 25 additions and 15 deletions

View File

@ -42,7 +42,8 @@ fi 2>/dev/null
ulimit -c unlimited # Enable core dumps ulimit -c unlimited # Enable core dumps
which lsb_release >/dev/null && export DIST=$(lsb_release --id | cut -d' ' -f2) || true which lsb_release >/dev/null &&
export DIST=$(lsb_release --id | cut -d' ' -f2)
unalias rd 2>/dev/null unalias rd 2>/dev/null
@ -51,11 +52,16 @@ unalias rd 2>/dev/null
compdef help=man compdef help=man
alias info='info --vi-keys' alias info='info --vi-keys'
resolvealias() {
alias "$1" 2>/dev/null | sed "s/.*='\?\([-A-z]*\)'\?\$/\1/" || echo $1
}
h() { h() {
help "$@" arg="$1"
# TODO resolve aliases in 'h' alias local alias=$(resolvealias "$arg")
# TODO call 'wh' on scripts instead shift
# TODO integrate tldr help "$alias" "$@" || wh "$arg" "$@"
# TODO call 'wh' on scripts rather than passing potentially hazardous args (as in clean)
} }
xtrace() { xtrace() {
@ -66,7 +72,7 @@ xtrace() {
# Shows source code for command, resolving nested aliases # Shows source code for command, resolving nested aliases
wh() { wh() {
res=$(which "$@") || return $? local res=$(which "$@") || return $?
# only works in zsh, not bash # only works in zsh, not bash
if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null if expr "$res" : "${@:$#}: aliased to" >/dev/null && ! expr "$res" : ".*builtin" >/dev/null
then echo "$res" | bat --style=plain --language=sh && then echo "$res" | bat --style=plain --language=sh &&
@ -120,8 +126,7 @@ edit-shell() {
# Edit an executable in the PATH # Edit an executable in the PATH
edit-bin() { edit-bin() {
alias=$(alias "$1" 2>/dev/null | sed "s/.*='\?\([-A-z]*\)'\?\$/\1/") local toedit="$(resolvealias "$1")"
toedit=${alias:-$1}
if f="$(which "$toedit" 2>/dev/null)" && test -f "$f" if f="$(which "$toedit" 2>/dev/null)" && test -f "$f"
then edit "$f" then edit "$f"
else edit-shell -f "$toedit" else edit-shell -f "$toedit"
@ -151,6 +156,8 @@ alias syslog='less +F /var/log/syslog'
alias sc='sudo systemctl' alias sc='sudo systemctl'
alias sce='sudo systemctl enable --now' # TODO or show log alias sce='sudo systemctl enable --now' # TODO or show log
alias scd='sudo systemctl disable --now' alias scd='sudo systemctl disable --now'
unalias scs 2>/dev/null
scs() { scs() {
( (
export SYSTEMD_COLORS=true export SYSTEMD_COLORS=true
@ -194,7 +201,7 @@ alias jcj='jce -o json-pretty --unit' # JSON View
# Shorthands # Shorthands
alias v='edit' alias v='edit'
alias st='synct' alias st='synct'
which dtrx >/dev/null && alias ex='dtrx' command -v dtrx >/dev/null && alias ex='dtrx'
alias expr='noglob expr' alias expr='noglob expr'
alias get='noglob =' alias get='noglob ='
@ -222,15 +229,15 @@ del() {
u() { u() {
if test "$(stat /etc/pacman.d/mirrorlist --printf=%y | cut -d' ' -f1)" != "$(date +%F)" if test "$(stat /etc/pacman.d/mirrorlist --printf=%y | cut -d' ' -f1)" != "$(date +%F)"
then which pacman-mirrors >/dev/null && then command -v pacman-mirrors >/dev/null &&
sudo pacman-mirrors --geoip sudo pacman-mirrors --geoip
if which pacman >/dev/null; then if command -v pacman >/dev/null; then
sudo pacman -Syy --needed base gnupg archlinux-keyring sudo pacman -Syy --needed base gnupg archlinux-keyring
sudo pacman-key --populate sudo pacman-key --populate
sudo pacman-key --refresh-keys sudo pacman-key --refresh-keys
fi fi
fi fi
if which topgrade >/dev/null if command -v topgrade >/dev/null
then then
topgrade topgrade
if test -d "$XDG_CONFIG_HOME/emacs" if test -d "$XDG_CONFIG_HOME/emacs"
@ -398,7 +405,7 @@ killJava() {
alias l="ls -l --almost-all --human-readable --group-directories-first --file-type" alias l="ls -l --almost-all --human-readable --group-directories-first --file-type"
if which fd >/dev/null if which fd >/dev/null
then fn() { $(which fd >/dev/null && echo fd || echo fdfind) --hidden --no-ignore-vcs --one-file-system "$@" | less -F; } # [F]ind [n]o ignore then fn() { $(command -v fd || echo fdfind) --hidden --no-ignore-vcs --one-file-system "$@" | less -F; } # [F]ind [n]o ignore
alias ff="noglob fn --color=always " # [F]ind [F]ile alias ff="noglob fn --color=always " # [F]ind [F]ile
compdef ff=fd compdef ff=fd
compdef fn=fd compdef fn=fd

View File

@ -36,9 +36,11 @@ case "$cmd" in
(pandoc) man pandoc; highlight "Extensions" && pandoc --list-extensions | paste -s -d' ';; (pandoc) man pandoc; highlight "Extensions" && pandoc --list-extensions | paste -s -d' ';;
(swaymsg) test $# -gt 1 && shift && man sway "$@" || man swaymsg;; (swaymsg) test $# -gt 1 && shift && man sway "$@" || man swaymsg;;
(swaymsg-*) man sway "${1#swaymsg-}";; (swaymsg-*) man sway "${1#swaymsg-}";;
(*) { showinfo "$@" || man "$@"; } || (*) showinfo "$@" || man "$@"
if which "$cmd" >/dev/null return=$?
if test $return -gt 0 && which "$cmd" >/dev/null
then { "$@" --help || { test $? == 1 && "$@" -help; } || { test $? == 1 && "$@" -h; }; } 2>&1 | $paginate then { "$@" --help || { test $? == 1 && "$@" -help; } || { test $? == 1 && "$@" -h; }; } 2>&1 | $paginate
return=$?
fi;; fi;;
esac esac
docs="/usr/share/doc/$cmd" docs="/usr/share/doc/$cmd"
@ -57,3 +59,4 @@ if test -d "$docs"; then
firefox "file://$(find $docs -name index.html | shortest | grep . || find $docs -name $1*.html | shortest)" firefox "file://$(find $docs -name index.html | shortest | grep . || find $docs -name $1*.html | shortest)"
fi fi
fi fi
exit $return