From d6a9f59544bd9c3702c03eb750ac08417352838a Mon Sep 17 00:00:00 2001 From: Gal Szkolnik Date: Wed, 5 Oct 2022 21:49:54 -0400 Subject: [PATCH] Improvements --- _home/dot_sz.shrc.d/01_util.functions | 32 +++++++++++++++---- _home/dot_sz.shrc.d/23_sudo | 1 + _home/dot_sz.shrc.d/97_chezmoi_aliases | 7 +++- .../bash.post/10_oh-my-posh.hook | 4 +-- _home/dot_sz.shrc.d/bash.pre/21_aliases | 6 +++- _home/dot_sz.shrc.d/zsh.pre/21_aliases | 10 +++++- 6 files changed, 48 insertions(+), 12 deletions(-) diff --git a/_home/dot_sz.shrc.d/01_util.functions b/_home/dot_sz.shrc.d/01_util.functions index f811c94..d7d1bd3 100644 --- a/_home/dot_sz.shrc.d/01_util.functions +++ b/_home/dot_sz.shrc.d/01_util.functions @@ -1,15 +1,28 @@ +ANSI_Print() { printf "\E[${ANSI_Code}m%s\E[0m" "$@"; } +ANSI_Red() { ANSI_Code=31 ANSI_Print "$@"; } +ANSI_Green() { ANSI_Code=32 ANSI_Print "$@"; } +ANSI_Yellow() { ANSI_Code=33 ANSI_Print "$@"; } +ANSI_Blue() { ANSI_Code=34 ANSI_Print "$@"; } +ANSI_Indiego() { ANSI_Code=35 ANSI_Print "$@"; } +ANSI_Cyan() { ANSI_Code=36 ANSI_Print "$@"; } + warn() { - (>&2 printf '\E[34mWARNING:\E[0m %s\n' "$@" ) + (>&2 printf '%s %s\n' $(ANSI_Yellow 'WARNING:') "$(echo "$@")" ) } +# The way to call this is: +# eval $(error 1 "message") || exit $? +# where 1 is the return/exit code and "message" is the message to be +# displayed error() { - (>&2 printf '\E[31mERROR:\E[0m %s\n' "$@" ) + printf "(>&2 \ + printf '%%s %%b' '$(ANSI_Red 'ERROR:')' '$(printf "%b " "${@:2}")') \ + && echo '' && return ${1} 2>/dev/null || exit ${1}" } require_root() { if [[ $EUID -ne 0 ]]; then - error "This script should only be run using sudo or as the root user" - exit 1 + eval $(error 1 "This script should only be run using sudo or as the root user") fi } @@ -22,9 +35,14 @@ recommend_root() { function setacl() { if [[ $# -lt 4 ]]; then - error "setacl missing arguments, called with '$1' ${@:2}" - printf "%s\n" "Usage:" "\tsetacl [...]" - return 1 + eval $(error 1 \ + "setacl missing arguments, called with '$1' ${@:2}" \ + $(printf "%s\n" \ + "Usage:" \ + "\tsetacl [...]" \ + ) + ) + return 1 fi local Options=${1} local Ownership=${2} diff --git a/_home/dot_sz.shrc.d/23_sudo b/_home/dot_sz.shrc.d/23_sudo index c48ebe9..028f15c 100644 --- a/_home/dot_sz.shrc.d/23_sudo +++ b/_home/dot_sz.shrc.d/23_sudo @@ -1,4 +1,5 @@ #alias sudo="/usr/bin/sudo " +alias _sudo="env sudo " function sudo() { local cli="$(command -v "${1}")" [[ -x "$cli" ]] || cli="${1}" diff --git a/_home/dot_sz.shrc.d/97_chezmoi_aliases b/_home/dot_sz.shrc.d/97_chezmoi_aliases index 67c9e9a..84f362d 100644 --- a/_home/dot_sz.shrc.d/97_chezmoi_aliases +++ b/_home/dot_sz.shrc.d/97_chezmoi_aliases @@ -1,3 +1,8 @@ +if [[ -z "$(which-command chezmoi)" && -x "$HOME/bin/chezmoi" ]]; then + function chezmoi() { + "$HOME/bin/chezmoi" "${@}" + } +fi if [[ -n "$(which-command chezmoi)" ]]; then export CHEZMOIROOT="$(chezmoi source-path)" export CZ="$CHEZMOIROOT" @@ -21,7 +26,7 @@ if [[ -n "$(which-command chezmoi)" ]]; then alias czu="cz upgrade && cz update && exec $SHELL " alias czxu="cz upgrade && cz update && czx apply && exec $SHELL " - alias cz-refresh="czs | cut -d\ -f2 | grep '^\.sz\.shrc\.d' | xargs cz forget --force; cz add ~/.sz.shrc.d --recursive; cz status; cz git status" + alias cz-refresh="czs | cut -d\ -f2 | grep '^\.sz\.shrc\.d' | xargs chezmoi forget --force; cz add ~/.sz.shrc.d --recursive; cz status; cz git status" alias cz-commit="cz git -- commit -a " function cz-remove-missing() { diff --git a/_home/dot_sz.shrc.d/bash.post/10_oh-my-posh.hook b/_home/dot_sz.shrc.d/bash.post/10_oh-my-posh.hook index cb1d1d6..a598361 100644 --- a/_home/dot_sz.shrc.d/bash.post/10_oh-my-posh.hook +++ b/_home/dot_sz.shrc.d/bash.post/10_oh-my-posh.hook @@ -1,6 +1,6 @@ if [[ -n "$(which-command oh-my-posh)" ]]; then OHMYPOSH_CONFIG='' - [[ -r ~/.poshtheme.omp.json ]] && OHMYPOSH_CONFIG="--config ~/.poshtheme.omp.json" - eval "$(oh-my-posh init bash $OHMYPOSH_CONFIG)" + [[ -r ~/.poshtheme.omp.json ]] && OHMYPOSH_CONFIG="~/.poshtheme.omp.json" + eval "$(oh-my-posh init bash --config $OHMYPOSH_CONFIG)" unset OHMYPOSH_CONFIG fi diff --git a/_home/dot_sz.shrc.d/bash.pre/21_aliases b/_home/dot_sz.shrc.d/bash.pre/21_aliases index 0fda67a..45c7ebb 100644 --- a/_home/dot_sz.shrc.d/bash.pre/21_aliases +++ b/_home/dot_sz.shrc.d/bash.pre/21_aliases @@ -1,5 +1,9 @@ -alias which-command="type -fP " function which-command() { + local ANSWER=$(type -t "${@}") || return $? + [[ $ANSWER == "function" ]] && printf "function " && type "${@}" | tail +2 && return + [[ $ANSWER == "alias" ]] && { + alias "${@}" + } type -fP "${@}" } diff --git a/_home/dot_sz.shrc.d/zsh.pre/21_aliases b/_home/dot_sz.shrc.d/zsh.pre/21_aliases index b8859c6..6322c4d 100644 --- a/_home/dot_sz.shrc.d/zsh.pre/21_aliases +++ b/_home/dot_sz.shrc.d/zsh.pre/21_aliases @@ -1 +1,9 @@ -alias which-command="whence " +function which-command() { + local ANSWER=$(whence -w "${@}") || return $? + ANSWER=${ANSWER##*: } + [[ $ANSWER == "function" ]] && printf "function " && whence -f "${@}" && return + [[ $ANSWER == "alias" ]] && { + alias "${@}" + } + whence "${@}" +}