Skip to content

Commit

Permalink
feat(zsh): print alias when ran via widget
Browse files Browse the repository at this point in the history
To enable, run: `enable_print_alias`
To disable, run: `disable_print_alias`

Signed-off-by: Vladislav Doster <mvdoster@gmail.com>
  • Loading branch information
vladdoster committed Mar 27, 2024
1 parent 1b0fb3c commit 3f7f058
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions zsh/.config/zsh/rc.d/02-widget.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,53 @@ function reset_broken_terminal() {
autoload -Uz add-zsh-hook
add-zsh-hook -Uz precmd reset_broken_terminal

local cmd_alias=""

# ---------------------
# Reveal Executed Alias
# ---------------------
# Note: disable by removing it from the hook.
# add-zsh-hook -d preexec pre_validation
alias_for() {
[[ $1 =~ '[[:punct:]]' ]] && return
local search=${1}
local found="$( alias $search )"
if [[ -n $found ]]; then
found=${found//\\//} # replace backslash with slash
found=${found%\'} # remove end single quote
found=${found#"$search="} # remove alias name
found=${found#"'"} # remove first single quote
echo "${found} ${2}" | xargs # return found value (with parameters)
else
echo ""
fi
}

expand_command_line() {
first=$(echo "$1" | awk '{print $1;}')
rest=$(echo ${${1}/"${first}"/})

if [[ -n "${first//-//}" ]]; then
cmd_alias="$(alias_for "${first}" "${rest:1}")" # check if there's an alias for the command
if [[ -n $cmd_alias ]] && [[ "${cmd_alias:0:1}" != "." ]]; then # if there was and not start with dot
echo "${T_GREEN}${T_YELLOW}${cmd_alias}${F_RESET}" # otherwise print it
fi
fi
}

pre_validation() {
[[ $# -eq 0 ]] && return
expand_command_line "$@"
}

function enable_print_alias () {
autoload -U add-zsh-hook # Load the zsh hook module.
add-zsh-hook preexec pre_validation
}


function disable_print_alias () {
add-zsh-hook -d preexec pre_validation
}

# vim: set expandtab filetype=zsh shiftwidth=2 softtabstop=2 tabstop=2:

0 comments on commit 3f7f058

Please sign in to comment.