-
Notifications
You must be signed in to change notification settings - Fork 3
/
ztrace.plugin.zsh
115 lines (92 loc) · 2.21 KB
/
ztrace.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
# Saved stdout descriptor
typeset -g ZTRACE_FD
# Path to directory where traces are saved
typeset -g ZTRACE_PATH="/tmp/ztrace-$$"
# Name of file being currently used
typeset -g ZTRACE_FNAME=""
# Decreased on each command execution
integer -g ZTRACE_COUNT=0
# Is Ztrace in progress?
integer -g ZTRACE_IN_PROGRESS=0
##
## User exposed functions {{{
##
ztstart() {
integer num="$1"
if [ "$ZTRACE_PATH" = "/tmp/" ]; then
print "Error occured, ZTRACE_PATH is just '/tmp'"
return 1
fi
# Create destination path for the process
command mkdir -p "$ZTRACE_PATH"
ZTRACE_COUNT=num
ZTRACE_IN_PROGRESS=1
local fname=$(date +%Y.%m.%d_%H:%M:%S)".ztrace"
ZTRACE_FNAME="$fname"
-zt-pinfo "Ztrace started"
# Save std out
exec {ZTRACE_FD}>&1
exec > >(tee -a "$ZTRACE_PATH/$fname")
}
ztstop() {
ZTRACE_IN_PROGRESS=0
exec >&$ZTRACE_FD && exec {ZTRACE_FD}>&-
-zt-pinfo "Ztrace stopped"
ZTRACE_FNAME=""
}
#
# Shows how many more commands will be catched
#
ztstatus() {
(( ZTRACE_COUNT ++ ))
print "Ztrace will catch $ZTRACE_COUNT following commands"
}
## }}}
##
## Private functions {{{
##
#
# Hook that counts number of commands executed
# and stops ztrace when it's time
#
-zt-precmd() {
(( ZTRACE_COUNT-- ))
if [[ "$ZTRACE_COUNT" -lt 0 && "$ZTRACE_IN_PROGRESS" = "1" ]]; then
ztstop
elif [ "$ZTRACE_IN_PROGRESS" = "1" ]; then
print "\\n-------------" >> "$ZTRACE_PATH/$ZTRACE_FNAME"
fi
}
## }}}
##
## Utility functions {{{
##
-zt-pinfo() { print "${fg_bold[green]}$*$reset_color"; }
-zt-perror() { print "${fg_bold[red]}$*$reset_color"; }
## }}}
##
## Load-time code
##
#
# Startup
#
-zt-init() {
setopt localoptions nullglob
# Cleary any possible left overs from previous sessions
typeset -a traces
traces=( "$ZTRACE_PATH"/*.ztrace )
if [[ "${#traces}" -gt 0 ]]; then
command rm -f "${traces[@]}"
fi
if [[ "$+fg_bold" -eq 0 ]]; then
autoload colors
colors
fi
autoload add-zsh-hook
add-zsh-hook precmd -zt-precmd
}
-zt-init
autoload h2-list h2-list-input h2-list-draw
autoload ztrace ztrace-usetty-wrapper ztrace-widget
zle -N ztrace-widget
bindkey "^G" ztrace-widget