-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-execute-after-command.plugin.zsh
148 lines (121 loc) · 6.71 KB
/
zsh-execute-after-command.plugin.zsh
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Standardized $0 handling
# https://wiki.zshell.dev/community/zsh_plugin_standard#zero-handling
0="${ZERO:-${${0:#$ZSH_ARGZERO}:-${(%):-%N}}}"
0="${${(M)0:#/*}:-$PWD/$0}"
declare zshExecuteAfterCommandPluginDirectory="${0:h}"
function () {
# Standardize options
# https://wiki.zshell.dev/community/zsh_plugin_standard#standard-recommended-options
builtin emulate -L zsh ${=${options[xtrace]:#off}:+-o xtrace}
builtin setopt extended_glob warn_create_global typeset_silent no_short_loops rc_quotes no_auto_pushd
# Load ZSH modules
zmodload zsh/datetime
# User customizable settings
# Commented out ZSTYLE commands are to document settings that are used but not initialized here
zstyle ':execute-after-command:user-setting:*' 'error-log' '/dev/stderr'
zstyle ':execute-after-command:user-setting:*' 'check-active-window' 'no'
zstyle ':execute-after-command:user-setting:*' 'suppress-unable-check-active-window' 'no'
# zstyle ':execute-after-command:user-setting:*' 'function-list'
# Internal settings
# Commented out ZSTYLE commands are to document settings that are used but not initialized here
zstyle ':execute-after-command:internal:setting:*' 'plugin-directory' "${zshExecuteAfterCommandPluginDirectory}"
# zstyle ':execute-after-command:internal:runtime:*' 'last-command-start-time'
# zstyle ':execute-after-command:internal:runtime:*' 'last-command-as-typed'
# zstyle ':execute-after-command:internal:runtime:*' 'last-command-as-executed-limited'
# zstyle ':execute-after-command:internal:runtime:*' 'last-command-as-executed-full'
# zstyle ':execute-after-command:internal:runtime:*' 'last-command-window-id'
source "${zshExecuteAfterCommandPluginDirectory}/lib/main.zsh"
function zsh-execute-after-command-add-functions() {
local -a functionsRequestedToAdd=( "$@" )
local -a functionsToAdd=()
local -a currentFunctions
local functionToAdd
zstyle -a ':execute-after-command:user-setting:*' 'function-list' 'currentFunctions'
if [[ "${#currentFunctions[@]}" -eq 0 ]]; then
zstyle ':execute-after-command:user-setting:*' 'function-list' "${functionsRequestedToAdd[@]}"
else
for functionToAdd in "${functionsRequestedToAdd[@]}"; do
# Check if function already in list of current functions
if ((${currentFunctions[(Ie)${functionToAdd}]})); then
continue
else
functionsToAdd+=( "${functionToAdd}" )
fi
done
if [[ ${#functionsToAdd[@]} -gt 0 ]]; then
zstyle ':execute-after-command:user-setting:*' 'function-list' "${currentFunctions[@]}" "${functionsToAdd[@]}"
fi
fi
}
# This function will be executed after a command has been entered, but before
# a command is actually executed
function zsh-execute-after-command-preexec-hook() {
zstyle ':execute-after-command:internal:runtime:*' 'last-command-start-time' "${EPOCHSECONDS}"
zstyle ':execute-after-command:internal:runtime:*' 'last-command-as-typed' "${1}"
zstyle ':execute-after-command:internal:runtime:*' 'last-command-as-executed-limited' "${2}"
zstyle ':execute-after-command:internal:runtime:*' 'last-command-as-executed-full' "${3}"
}
# This function will be executed after a command has completed execution
function zsh-execute-after-command-precmd-hook() {
# Information from system
local lastCommandExitStatus="${?}"
local lastCommandStopTime="${EPOCHSECONDS}"
# Information from internal runtime
local lastCommandStartTime
local lastCommandAsTyped
local lastCommandAsExecutedLimited
local lastCommandAsExecutedFull
# Information from user settings
local errorLog
local checkActiveWindow
local -a functionList
# Information from internal settings
local pluginDirectory
# Procedural
local longestFunctionName
local func
local lastCommandExecutionSeconds
local windowFocused
zstyle -s ':execute-after-command:internal:runtime:*' 'last-command-start-time' 'lastCommandStartTime'
zstyle -s ':execute-after-command:internal:runtime:*' 'last-command-as-typed' 'lastCommandAsTyped'
zstyle -s ':execute-after-command:internal:runtime:*' 'last-command-as-executed-limited' 'lastCommandAsExecutedLimited'
zstyle -s ':execute-after-command:internal:runtime:*' 'last-command-as-executed-full' 'lastCommandAsExecutedFull'
zstyle -s ':execute-after-command:user-setting:*' 'error-log' 'errorLog'
zstyle -b ':execute-after-command:user-setting:*' 'check-active-window' 'checkActiveWindow'
zstyle -a ':execute-after-command:user-setting:*' 'function-list' 'functionList'
zstyle -s ':execute-after-command:internal:setting:*' 'plugin-directory' 'pluginDirectory'
# If there are no functions to execute, then there is nothing to do
if [[ "${#functionList[@]}" -eq 0 ]]; then
return 0
fi
touch "${errorLog}"
{
# Calculate elapsed time
(( lastCommandExecutionSeconds = lastCommandStopTime - lastCommandStartTime ))
# Determine if the window was focused
if [[ "${checkActiveWindow}" = 'yes' ]]; then
.zeac-is-terminal-active
windowFocused=$?
else
windowFocused=2
fi
for func in "${functionList[@]}"; do
"${func}" \
${lastCommandAsTyped} \
${lastCommandAsExecutedLimited} \
${lastCommandAsExecutedFull} \
${lastCommandExitStatus} \
${lastCommandExecutionSeconds} \
${windowFocused}
done
} 2>&1 | sed 's|^|zsh-execute-after-command: |' >> "${errorLog}"
zstyle -d ':execute-after-command:internal:runtime:*' 'last-command-start-time'
zstyle -d ':execute-after-command:internal:runtime:*' 'last-command-as-typed'
zstyle -d ':execute-after-command:internal:runtime:*' 'last-command-as-executed-limited'
zstyle -d ':execute-after-command:internal:runtime:*' 'last-command-as-executed-full'
}
autoload -U add-zsh-hook
add-zsh-hook preexec zsh-execute-after-command-preexec-hook
add-zsh-hook precmd zsh-execute-after-command-precmd-hook
}
unset zshExecuteAfterCommandPluginDirectory