Skip to content

Commit e213290

Browse files
committed
fixup: EPOCHSECONDS requires zsh/datetime zsh module
This revision loads the zsh/datetime during prompt setup, and ensures that empty strings are not passed to query parameters. Also relax the int requirements on query parameters that are Shell driven.
1 parent 0fb40ec commit e213290

File tree

2 files changed

+50
-47
lines changed

2 files changed

+50
-47
lines changed

cmd/goprompt/cmdQuery.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/signal"
99
"os/user"
1010
"path/filepath"
11+
"strconv"
1112
"strings"
1213
"syscall"
1314
"time"
@@ -22,13 +23,13 @@ var (
2223
Use: "query",
2324
Short: "start the query that pulls data for the prompt",
2425
}
25-
flgQCmdStatus = cmdQuery.PersistentFlags().Int(
26-
"cmd-status", 0,
27-
"cmd status of previous command",
26+
flgQCmdStatus = cmdQuery.PersistentFlags().String(
27+
"cmd-status", "0",
28+
"cmd status of previous command (int)",
2829
)
29-
flgQPreexecTS = cmdQuery.PersistentFlags().Int(
30-
"preexec-ts", 0,
31-
"pre-execution timestamp to gauge how log execution took",
30+
flgQPreexecTS = cmdQuery.PersistentFlags().String(
31+
"preexec-ts", "0",
32+
"pre-execution timestamp to gauge how log execution took (int)",
3233
)
3334
flgQTimeout = cmdQuery.PersistentFlags().Duration(
3435
"timeout", 0,
@@ -164,8 +165,9 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
164165
nowTS := time.Now()
165166
printPart(_partTimestamp, timeFMT(nowTS))
166167

167-
if *flgQCmdStatus != 0 {
168-
printPart(_partStatus, fmt.Sprintf("%#v", *flgQCmdStatus))
168+
prevCMDStatus := trim(*flgQCmdStatus)
169+
if prevCMDStatus != "0" {
170+
printPart(_partStatus, fmt.Sprintf("%s", prevCMDStatus))
169171
}
170172

171173
tasks.Go(func(ctx context.Context) error {
@@ -188,12 +190,14 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
188190
printPart(_partSessionHostname, sessionHostname)
189191
}
190192

191-
if *flgQPreexecTS != 0 {
192-
cmdTS := time.Unix(int64(*flgQPreexecTS), 0)
193-
194-
diff := nowTS.Sub(cmdTS).Round(time.Second)
195-
if diff > 1 {
196-
printPart(_partDuration, diff)
193+
preexecTS := trim(*flgQPreexecTS)
194+
if preexecTS != "0" {
195+
if ts, err := strconv.Atoi(preexecTS); err == nil {
196+
cmdTS := time.Unix(int64(ts), 0)
197+
diff := nowTS.Sub(cmdTS).Round(time.Second)
198+
if diff > 1 {
199+
printPart(_partDuration, diff)
200+
}
197201
}
198202
}
199203

plugin/zsh/prompt_asynczle_setup.zsh

+32-33
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
# In a file `prompt_asynczle_setup` available on `fpath`
22
emulate -L zsh
33

4-
typeset -g C_PROMPT_NEWLINE=$'\n%{\r%}'
5-
6-
typeset -g G_LAST_STATUS=0
7-
typeset -g G_PREEXEC_TS=0
8-
typeset -g G_ASYNC_DONE=0
9-
10-
typeset -g G_PROMPT_DATA=""
11-
12-
typeset -g G_LAST_PROMPT=""
4+
typeset -g ZSH_ASYNC_PROMPT_TIMEOUT=${ZSH_ASYNC_PROMPT_TIMEOUT:-5s}
5+
typeset -g ZSH_ASYNC_PROMPT_EXEC=${GOPROMPT}
136

14-
declare -gA ZLE_ASYNC_FDS=()
7+
typeset -g ZSH_ASYNC_PROMPT_DATA=""
8+
typeset -g ZSH_ASYNC_PROMPT_LAST=""
159

16-
typeset -g ZSH_ASYNC_PROMPT_TIMEOUT=${ZSH_ASYNC_PROMPT_TIMEOUT:-5s}
10+
typeset -g ZSH_ASYNC_PROMPT_LAST_STATUS=0
11+
typeset -g ZSH_ASYNC_PROMPT_PREEXEC_TS=0
12+
typeset -g ZSH_ASYNC_PROMPT_QUERY_DONE=0
1713

18-
typeset -g G_GOPROMPT=${GOPROMPT}
14+
declare -gA __ZLE_ASYNC_FDS=()
15+
typeset -g __ZSH_ASYNC_PROMPT_NEWLINE=$'\n%{\r%}'
1916

2017
#-------------------------------------------------------------------------------
2118

@@ -31,20 +28,20 @@ __async_check_exec() {
3128
}
3229

3330
__async_prompt_query() {
34-
if ! __async_check_exec "${G_GOPROMPT}"; then
31+
if ! __async_check_exec "${ZSH_ASYNC_PROMPT_EXEC}"; then
3532
echo -n ""
3633
return
3734
fi
3835

39-
${G_GOPROMPT} query \
40-
--cmd-status "$G_LAST_STATUS" \
41-
--preexec-ts "$G_PREEXEC_TS" \
36+
${ZSH_ASYNC_PROMPT_EXEC} query \
37+
--cmd-status "${ZSH_ASYNC_PROMPT_LAST_STATUS:-0}" \
38+
--preexec-ts "${ZSH_ASYNC_PROMPT_PREEXEC_TS:-0}" \
4239
--pid-parent-skip 1 \
43-
--timeout "$ZSH_ASYNC_PROMPT_TIMEOUT"
40+
--timeout "${ZSH_ASYNC_PROMPT_TIMEOUT:-5s}"
4441
}
4542

4643
__async_prompt_render() {
47-
if ! __async_check_exec "${G_GOPROMPT}"; then
44+
if ! __async_check_exec "${ZSH_ASYNC_PROMPT_EXEC}"; then
4845
echo -n "?>"
4946
return
5047
fi
@@ -55,11 +52,11 @@ __async_prompt_render() {
5552
fi
5653

5754
local LOADING=1
58-
if [[ $G_ASYNC_DONE -eq 1 ]]; then
55+
if [[ $ZSH_ASYNC_PROMPT_QUERY_DONE -eq 1 ]]; then
5956
LOADING=0
6057
fi
6158

62-
${G_GOPROMPT} render \
59+
${ZSH_ASYNC_PROMPT_EXEC} render \
6360
--prompt-mode "$MODE" \
6461
--prompt-loading="$LOADING" \
6562
--color-mode "zsh"
@@ -68,34 +65,34 @@ __async_prompt_render() {
6865
#-------------------------------------------------------------------------------
6966

7067
__prompt_rerender() {
71-
local BR=$C_PROMPT_NEWLINE
68+
local BR=$__ZSH_ASYNC_PROMPT_NEWLINE
7269

73-
PROMPT="$(printf "%s\n" "$G_PROMPT_DATA" | __async_prompt_render) "
70+
PROMPT="$(printf "%s\n" "$ZSH_ASYNC_PROMPT_DATA" | __async_prompt_render) "
7471

75-
if [[ $PROMPT != $G_LAST_PROMPT ]]; then
72+
if [[ $PROMPT != $ZSH_ASYNC_PROMPT_LAST ]]; then
7673
zle && zle reset-prompt
7774
fi
7875

79-
G_LAST_PROMPT="$PROMPT"
76+
ZSH_ASYNC_PROMPT_LAST="$PROMPT"
8077
}
8178

8279
#-------------------------------------------------------------------------------
8380
# Command Handlers + Async Comm
8481
#-------------------------------------------------------------------------------
8582

8683
__prompt_preexec() {
87-
typeset -g G_PREEXEC_TS=$EPOCHSECONDS
84+
typeset -g ZSH_ASYNC_PROMPT_PREEXEC_TS=$EPOCHSECONDS
8885
}
8986

9087
__prompt_precmd() {
9188
# save the status of last command.
92-
G_LAST_STATUS=$?
89+
ZSH_ASYNC_PROMPT_LAST_STATUS=$?
9390

9491
# reset prompt state
95-
G_PROMPT_DATA=""
92+
ZSH_ASYNC_PROMPT_DATA=""
9693

9794
# set prompt status to rendering
98-
G_ASYNC_DONE=0
95+
ZSH_ASYNC_PROMPT_QUERY_DONE=0
9996

10097
__zle_async_dispatch __zle_async_fd_handler __async_prompt_query
10198

@@ -116,15 +113,15 @@ __zle_async_fd_handler() {
116113
# select marks this fd if we reach EOF,
117114
# so handle this specially.
118115
__zle_async_detach "$ZLE_FD"
119-
G_ASYNC_DONE=1
116+
ZSH_ASYNC_PROMPT_QUERY_DONE=1
120117

121-
G_PROMPT_DATA="${G_PROMPT_DATA}"$'\n'"${ASYNC_RESULT}"
118+
ZSH_ASYNC_PROMPT_DATA="${ZSH_ASYNC_PROMPT_DATA}"$'\n'"${ASYNC_RESULT}"
122119
__prompt_rerender
123120

124121
return 1
125122
fi
126123

127-
G_PROMPT_DATA="${G_PROMPT_DATA}"$'\n'"${ASYNC_RESULT}"
124+
ZSH_ASYNC_PROMPT_DATA="${ZSH_ASYNC_PROMPT_DATA}"$'\n'"${ASYNC_RESULT}"
128125
if [[ $ASYNC_RESULT == "" ]]; then
129126
__prompt_rerender
130127
fi
@@ -135,7 +132,7 @@ __zle_async_dispatch() {
135132
local command=( "$@" )
136133

137134
# Close existing file descriptor for this handler.
138-
local OLD_ZLE_FD=${ZLE_ASYNC_FDS["${dispatch_handler}"]}
135+
local OLD_ZLE_FD=${__ZLE_ASYNC_FDS["${dispatch_handler}"]}
139136
if [[ -n $OLD_ZLE_FD ]]; then
140137
__zle_async_detach "$OLD_ZLE_FD" 2>/dev/null
141138
fi
@@ -147,7 +144,7 @@ __zle_async_dispatch() {
147144

148145
# Attach file a ZLE handler to file descriptor.
149146
zle -F $ZLE_FD "${dispatch_handler}"
150-
ZLE_ASYNC_FDS["${dispatch_handler}"]="$ZLE_FD"
147+
__ZLE_ASYNC_FDS["${dispatch_handler}"]="$ZLE_FD"
151148
}
152149

153150
__zle_async_detach() {
@@ -162,6 +159,8 @@ __zle_async_detach() {
162159
#-------------------------------------------------------------------------------
163160

164161
prompt_asynczle_setup() {
162+
zmodload zsh/datetime || :
163+
165164
autoload -Uz +X add-zsh-hook 2>/dev/null
166165
autoload -Uz +X add-zle-hook-widget 2>/dev/null
167166

0 commit comments

Comments
 (0)