From 8ae9c465242c5e379877a15832043113021d714e Mon Sep 17 00:00:00 2001 From: Adam Hovorka Date: Mon, 4 Sep 2017 13:42:23 -0600 Subject: Switch from stowsh to dotbot --- base/zsh/tipz.zsh | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 base/zsh/tipz.zsh (limited to 'base/zsh/tipz.zsh') 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 -- cgit v1.2.3-70-g09d2