6
6
"encoding/json"
7
7
"fmt"
8
8
"github.com/spf13/cobra"
9
+ "io"
9
10
"os"
10
11
"strings"
11
12
"sync"
@@ -18,18 +19,56 @@ var (
18
19
cmd = & cobra.Command {
19
20
Use : "goprompt" ,
20
21
}
21
- flgCmdStatus = cmd .PersistentFlags ().Int (
22
+
23
+ cmdQuery = & cobra.Command {
24
+ Use : "query" ,
25
+ Short : "start the query that pulls data for the prompt" ,
26
+ }
27
+ flgQCmdStatus = cmdQuery .PersistentFlags ().Int (
22
28
"cmd-status" , 0 ,
23
29
"cmd status of previous command" ,
24
30
)
25
- flgPreexecTS = cmd .PersistentFlags ().Int (
31
+ flgQPreexecTS = cmdQuery .PersistentFlags ().Int (
26
32
"preexec-ts" , 0 ,
27
33
"pre-execution timestamp to gauge how log execution took" ,
28
34
)
35
+
36
+ cmdRender = & cobra.Command {
37
+ Use : "render" ,
38
+ Short : "render the prompt based on the results of query" ,
39
+ }
29
40
)
30
41
31
42
func init () {
32
- cmd .RunE = cmdExec
43
+ cmdQuery .RunE = cmdQueryRun
44
+ cmd .AddCommand (cmdQuery )
45
+
46
+ cmdRender .RunE = cmdRenderRun
47
+ cmd .AddCommand (cmdRender )
48
+ }
49
+
50
+ func cmdRenderRun (cmd * cobra.Command , args []string ) error {
51
+ if _ , err := os .Stdin .Stat (); err != nil {
52
+ fmt .Printf ("%#v" , err )
53
+ }
54
+
55
+ out , err := io .ReadAll (os .Stdin )
56
+ if err != nil {
57
+ panic (err )
58
+ }
59
+
60
+ lines := strings .Split (string (out ), "\n " )
61
+ vals := make (map [string ]string )
62
+ for _ , line := range lines {
63
+ key , value , ok := strings .Cut (line , "\t " )
64
+ if ok {
65
+ vals [key ] = value
66
+ }
67
+ }
68
+
69
+ fmt .Printf ("%#v\n " , vals )
70
+ fmt .Printf (">" )
71
+ return nil
33
72
}
34
73
35
74
// PROMPT PARTS:
@@ -40,9 +79,9 @@ func init() {
40
79
// (vsc-information)
41
80
// (timestamp)
42
81
43
- func cmdExec (cmd * cobra.Command , args []string ) error {
44
- if * flgCmdStatus != 0 {
45
- printPart ("st" , fmt .Sprintf ("%#v" , * flgCmdStatus ))
82
+ func cmdQueryRun (cmd * cobra.Command , args []string ) error {
83
+ if * flgQCmdStatus != 0 {
84
+ printPart ("st" , fmt .Sprintf ("%#v" , * flgQCmdStatus ))
46
85
}
47
86
48
87
wg := new (WaitGroupDispatcher )
@@ -61,8 +100,8 @@ func cmdExec(cmd *cobra.Command, args []string) error {
61
100
nowTS := time .Now ()
62
101
printPart ("ts" , nowTS .Format ("15:04:05 01/02/06" ))
63
102
64
- if * flgPreexecTS != 0 {
65
- cmdTS := time .Unix (int64 (* flgPreexecTS ), 0 )
103
+ if * flgQPreexecTS != 0 {
104
+ cmdTS := time .Unix (int64 (* flgQPreexecTS ), 0 )
66
105
67
106
diff := nowTS .Sub (cmdTS )
68
107
printPart ("ds" , diff .Round (time .Second ))
@@ -89,7 +128,19 @@ func cmdExec(cmd *cobra.Command, args []string) error {
89
128
90
129
cwg .Dispatch (func () {
91
130
if branch , err := stringExec ("git" , "branch" , "--show-current" ); err == nil {
92
- printPart ("vcs_br" , trim (string (branch )))
131
+ branch = trim (branch )
132
+ if len (branch ) > 0 {
133
+ printPart ("vcs_br" , trim (branch ))
134
+ return
135
+ }
136
+ }
137
+
138
+ if branch , err := stringExec ("git" , "name-rev" , "--name-only" , "HEAD" ); err == nil {
139
+ branch = trim (branch )
140
+ if len (branch ) > 0 {
141
+ printPart ("vcs_br" , trim (branch ))
142
+ return
143
+ }
93
144
}
94
145
})
95
146
@@ -103,17 +154,40 @@ func cmdExec(cmd *cobra.Command, args []string) error {
103
154
}
104
155
}
105
156
})
157
+
158
+ cwg .Dispatch (func () {
159
+ if status , err := stringExec ("git" , "rev-list" , "--left-right" , "--count" , "HEAD...@{u}" ); err == nil {
160
+ parts := strings .SplitN (status , "\t " , 2 )
161
+ if len (parts ) < 2 {
162
+ parts = []string {"0" , "0" }
163
+ }
164
+
165
+ printPart ("vcs_log_ahead" , parts [0 ])
166
+ printPart ("vcs_log_behind" , parts [1 ])
167
+ }
168
+ })
106
169
})
107
170
108
171
wg .Dispatch (func () {
172
+ var err error
173
+
109
174
cwg := new (WaitGroupDispatcher )
110
175
defer cwg .Wait ()
111
176
112
- var stgPatchTop string
113
- var err error
177
+ var stgSeriesLen string
178
+ if stgSeriesLen , err = stringExec ("stg" , "series" , "-c" ); err == nil {
179
+ printPart ("stg" , "1" )
180
+ printPart ("stg_qlen" , stgSeriesLen )
181
+ }
114
182
183
+ cwg .Dispatch (func () {
184
+ if stgSeriesPos , err := stringExec ("stg" , "series" , "-cA" ); err == nil {
185
+ printPart ("stg_qpos" , stgSeriesPos )
186
+ }
187
+ })
188
+
189
+ var stgPatchTop string
115
190
if stgPatchTop , err = stringExec ("stg" , "top" ); err == nil {
116
- printPart ("stg" , "1" )
117
191
printPart ("stg_top" , stgPatchTop )
118
192
} else {
119
193
return
@@ -129,18 +203,6 @@ func cmdExec(cmd *cobra.Command, args []string) error {
129
203
printPart ("stg_dirty" , 0 )
130
204
}
131
205
})
132
-
133
- cwg .Dispatch (func () {
134
- if stgPatchLen , err := stringExec ("stg" , "series" , "-c" ); err == nil {
135
- printPart ("stg_qlen" , stgPatchLen )
136
- }
137
- })
138
-
139
- cwg .Dispatch (func () {
140
- if stgPatchPos , err := stringExec ("stg" , "series" , "-cA" ); err == nil {
141
- printPart ("stg_qpos" , stgPatchPos )
142
- }
143
- })
144
206
})
145
207
146
208
return nil
0 commit comments