diff options
Diffstat (limited to 'base/zsh/tipz.zsh')
-rw-r--r-- | base/zsh/tipz.zsh | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/base/zsh/tipz.zsh b/base/zsh/tipz.zsh new file mode 100644 index 0000000..bb1e224 --- /dev/null +++ b/base/zsh/tipz.zsh @@ -0,0 +1,84 @@ +### +# Search the defined aliases for a match +### +function _tipz_find_match() { + local bits alias command result="" + local -a aliases args; args="$@" + + # Load the current aliases into an array + local oldIFS=$IFS + IFS=$'\n' aliases=($(alias)) + IFS=$oldIFS + + # Loop through each of the aliases + for line in "${aliases[@]}"; do + # Split the line on '=' to separate the command + # and its alias + bits=("${(s/=/)line}") + alias=$bits[1] + command=$bits[2] + + # Create a regex that finds an exact match for + # the current argument string + args="${(@)args[@]}" + local pattern=$'^[\'\"]?'${args//([\{\}\(\)\[\]\*\?\:\\\.\|])/\\\$1}$'[\'\"]?$' + + # Check if the command matches the regex + if [[ "$command" =~ $pattern ]]; then + # Ensure that the longest matching command is stored + if [[ ${#command} > ${#result} ]]; then + result=$alias + fi + fi + done + + # If a result has been found, output it + if [[ -n $result ]]; then + echo $result + return 0 + fi + + return 1 +} + +### +# Search for alias tips for the currently executing command +### +function _tipz_process { + local -a cmd; cmd=($@) + integer i=$(( ${#cmd} + 1 )) + + # Loop for the length of the argument list, knocking + # an argument from the end of the list each time, and + # then using the remaining arguments to search for aliases + while [[ $i > 0 ]]; do + # Check the current string for a match + result=$(_tipz_find_match "${(@)cmd:0:$i}") + + # If the search exited successfully, + # output the tip to the user + if [[ $? -eq 0 ]]; then + print -P "%B%F{8}Alias tip: %b$result ${(@)cmd:$i}%f" + return 0 + fi + + # Decrement the counter + i=$(( i - 1 )) + done + + return 1 +} + +### +# A small function to filter out strange arguments +# sent from the add-zsh-hook preexec hook +### +function _tipz_prexec() { + _tipz_process $(echo $1) +} + +### +# Register the preexec hook +### +autoload -Uz add-zsh-hook +add-zsh-hook preexec _tipz_prexec |