1
+ # In a file `prompt_goprompt_setup` available on `fpath`:
2
+
3
+ typeset -g GOPROMPT_NEWLINE=$' \n %{\r %}'
4
+
5
+ typeset -g GOPROMPT_LAST_STATUS=0
6
+ typeset -g GOPROMPT_PREEXEC_TS=0
7
+
8
+ typeset -gA GOPROMPT_PARTS=()
9
+
10
+ # -------------------------------------------------------------------------------
11
+
12
+ autoload -Uz add-zsh-hook
13
+
14
+ # -------------------------------------------------------------------------------
15
+
16
+ __goprompt_update_handler () {
17
+ local BR=$GOPROMPT_NEWLINE
18
+ local -A P=( " ${(@ kv)GOPROMPT_PARTS} " )
19
+
20
+ local GOPROMPT_PARTS_BOTTOM=()
21
+ local GOPROMPT_PARTS_TOP=()
22
+
23
+ local -a prompt_parts=(
24
+ " :: (git: ${P[git_st]} )"
25
+ " :: ${P[wd]} "
26
+ " # "
27
+ )
28
+
29
+ PROMPT=" $BR ${(pj.$BR .)prompt_parts} "
30
+ zle && zle reset-prompt
31
+ }
32
+
33
+ __goprompt_async_handler () {
34
+ local ZLE_FD=$1
35
+
36
+ if ! IFS=$' \n ' read -r ASYNC_RESULT < & " $ZLE_FD " ; then
37
+ # select marks this fd if we reach EOF,
38
+ # so handle this specially.
39
+ __zle_async_detach " $ZLE_FD "
40
+ return 1
41
+ fi
42
+
43
+ # split by tab char
44
+ local KV=( " ${(@ s/ / )ASYNC_RESULT} " )
45
+
46
+ GOPROMPT_PARTS[${KV[1]} ]=${KV[2]}
47
+
48
+ __goprompt_update_handler
49
+ }
50
+
51
+ __goprompt_preexec () {
52
+ typeset -g GOPROMPT_PREEXEC_TS=$EPOCHSECONDS
53
+ }
54
+
55
+ __goprompt_precmd () {
56
+ GOPROMPT_LAST_STATUS=$?
57
+ GOPROMPT_PARTS=()
58
+
59
+ __goprompt_update_handler
60
+ __zle_async_dispatch __goprompt_async_handler \
61
+ __goprompt_run query \
62
+ --cmd-status " $GOPROMPT_LAST_STATUS " \
63
+ --preexec-ts " $GOPROMPT_PREEXEC_TS "
64
+ }
65
+
66
+ # -------------------------------------------------------------------------------
67
+ # ZLE Async
68
+ # -------------------------------------------------------------------------------
69
+
70
+ declare -A ZLE_ASYNC_FDS=()
71
+
72
+ __zle_async_dispatch () {
73
+ local dispatch_handler=" $1 " ; shift 1
74
+ local command=( " $@ " )
75
+
76
+ # Close existing file descriptor for this handler.
77
+ local OLD_ZLE_FD=${ZLE_ASYNC_FDS["${dispatch_handler}"]}
78
+ if [[ -n $OLD_ZLE_FD ]]; then
79
+ __zle_async_detach " $OLD_ZLE_FD " 2> /dev/null
80
+ fi
81
+
82
+ # Create File Descriptor and attach to async command
83
+ exec {ZLE_FD}< <( " ${command[@]} " )
84
+
85
+ # Attach file a ZLE handler to file descriptor.
86
+ zle -F $ZLE_FD " ${dispatch_handler} "
87
+ ZLE_ASYNC_FDS[" ${dispatch_handler} " ]=" $ZLE_FD "
88
+ }
89
+
90
+ __zle_async_detach () {
91
+ local ZLE_FD=$1
92
+ # Close stdout.
93
+ exec {ZLE_FD}< & -
94
+ # Close the file-descriptor.
95
+ zle -F " $ZLE_FD "
96
+ }
97
+
98
+ # -------------------------------------------------------------------------------
99
+
100
+ __goprompt_run () {
101
+ if ! (( $+ commands[goprompt] )) ; then
102
+ echo -n " [ERROR: goprompt binary missing]"
103
+ fi
104
+ goprompt " $@ "
105
+ }
106
+
107
+
108
+ prompt_goprompt_setup () {
109
+ add-zsh-hook precmd __goprompt_precmd
110
+ add-zsh-hook preexec __goprompt_preexec
111
+ }
112
+
113
+ prompt_goprompt_setup " $@ "
0 commit comments