blob: fdcc0589e148e5d3ca3b4764cd2fe4d8c6c0ada8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
_tipz_ignore_list=("run-help")
function _tipz_contains() {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
###
# 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]
if _tipz_contains "$alias" "${_tipz_ignore_list[@]}"; then continue; fi
# 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
|