25
25
)
26
26
)
27
27
28
+ const (
29
+ _partStatus = "st"
30
+ _partTimestamp = "ts"
31
+ _partDuration = "ds"
32
+
33
+ _partWorkDir = "wd"
34
+ _partWorkDirShort = "wd_trim"
35
+
36
+ _partPidShell = "pid_shell"
37
+ _partPidShellExec = "pid_shell_exec"
38
+ _partPidParent = "pid_parent"
39
+ _partPidParentExec = "pid_parent_exec"
40
+
41
+ _partVcs = "vcs"
42
+ _partVcsBranch = "vcs_br"
43
+ _partVcsDirty = "vcs_dirty"
44
+
45
+ _partVcsLogAhead = "vcs_log_ahead"
46
+ _partVcsLogBehind = "vcs_log_behind"
47
+
48
+ _partVcsStg = "stg"
49
+ _partVcsStgQlen = "stg_qlen"
50
+ _partVcsStgQpos = "stg_qpos"
51
+ _partVcsStgTop = "stg_top"
52
+ _partVcsStgDirty = "stg_dirty"
53
+
54
+ _partVcsGitIdxTotal = "git_idx_total"
55
+ _partVcsGitIdxIncluded = "git_idx_incl"
56
+ _partVcsGitIdxExcluded = "git_idx_excl"
57
+ )
58
+
28
59
func init () {
29
60
cmdQuery .RunE = cmdQueryRun
30
61
}
@@ -34,11 +65,19 @@ func timeFMT(ts time.Time) string {
34
65
}
35
66
36
67
func cmdQueryRun (_ * cobra.Command , _ []string ) error {
68
+ printCH := make (chan shellKV )
69
+ defer close (printCH )
70
+
71
+ go shellKVStaggeredPrinter (printCH , 20 * time .Millisecond , 600 * time .Millisecond )
72
+ printPart := func (name string , value interface {}) {
73
+ printCH <- shellKV {name , value }
74
+ }
75
+
37
76
nowTS := time .Now ()
38
- printPart ("ts" , timeFMT (nowTS ))
77
+ printPart (_partTimestamp , timeFMT (nowTS ))
39
78
40
79
if * flgQCmdStatus != 0 {
41
- printPart ("st" , fmt .Sprintf ("%#v" , * flgQCmdStatus ))
80
+ printPart (_partStatus , fmt .Sprintf ("%#v" , * flgQCmdStatus ))
42
81
}
43
82
44
83
wg := new (WaitGroupDispatcher )
@@ -50,16 +89,16 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
50
89
if wd , err := os .Getwd (); err == nil {
51
90
wdh := strings .Replace (wd , homeDir , "~" , 1 )
52
91
53
- printPart ("wd" , wdh )
54
- printPart ("wd_trim" , trimPath (wdh ))
92
+ printPart (_partWorkDir , wdh )
93
+ printPart (_partWorkDirShort , trimPath (wdh ))
55
94
}
56
95
57
96
if * flgQPreexecTS != 0 {
58
97
cmdTS := time .Unix (int64 (* flgQPreexecTS ), 0 )
59
98
60
99
diff := nowTS .Sub (cmdTS ).Round (time .Second )
61
100
if diff > 1 {
62
- printPart ("ds" , diff )
101
+ printPart (_partDuration , diff )
63
102
}
64
103
}
65
104
})
@@ -81,16 +120,16 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
81
120
return
82
121
}
83
122
84
- printPart ("pid_shell" , pidShell .Pid ())
85
- printPart ("pid_shell_exec" , pidShell .Executable ())
123
+ printPart (_partPidShell , pidShell .Pid ())
124
+ printPart (_partPidShellExec , pidShell .Executable ())
86
125
87
126
pidShellParent , err := ps .FindProcess (pidShell .PPid ())
88
127
if err != nil {
89
128
return
90
129
}
91
130
92
- printPart ("pid_parent" , pidShellParent .Pid ())
93
- printPart ("pid_parent_exec" , pidShellParent .Executable ())
131
+ printPart (_partPidParent , pidShellParent .Pid ())
132
+ printPart (_partPidParentExec , pidShellParent .Executable ())
94
133
})
95
134
96
135
//wg.Dispatch(func() {
@@ -106,7 +145,7 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
106
145
defer cwg .Wait ()
107
146
108
147
if _ , err := stringExec ("git" , "rev-parse" , "--show-toplevel" ); err == nil {
109
- printPart ("vcs" , "git" )
148
+ printPart (_partVcs , "git" )
110
149
} else {
111
150
return
112
151
}
@@ -115,29 +154,59 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
115
154
if branch , err := stringExec ("git" , "branch" , "--show-current" ); err == nil {
116
155
branch = trim (branch )
117
156
if len (branch ) > 0 {
118
- printPart ("vcs_br" , trim (branch ))
157
+ printPart (_partVcsBranch , trim (branch ))
119
158
return
120
159
}
121
160
}
122
161
123
162
if branch , err := stringExec ("git" , "name-rev" , "--name-only" , "HEAD" ); err == nil {
124
163
branch = trim (branch )
125
164
if len (branch ) > 0 {
126
- printPart ("vcs_br" , trim (branch ))
165
+ printPart (_partVcsBranch , trim (branch ))
127
166
return
128
167
}
129
168
}
130
169
})
131
170
132
171
cwg .Dispatch (func () {
133
- if status , err := stringExec ("git" , "status" , "--porcelain" ); err == nil {
134
- if len (status ) > 0 {
135
- printPart ("vcs_dirty" , 1 )
136
- //printPart("vcs_dirty_st", js(status))
137
- } else {
138
- printPart ("vsc_dirty" , 0 )
172
+ status , err := stringExec ("git" , "status" , "--porcelain" );
173
+ if err != nil {
174
+ return
175
+ }
176
+
177
+ if len (status ) == 0 {
178
+ printPart (_partVcsDirty , 0 )
179
+ return
180
+ }
181
+
182
+ printPart (_partVcsDirty , 1 )
183
+
184
+ fTotal := 0
185
+ fInIndex := 0
186
+ fOutOfIndex := 0
187
+
188
+ lines := strings .Split (status , "\n " )
189
+ for _ , line := range lines {
190
+ if len (line ) < 2 {
191
+ continue
192
+ }
193
+
194
+ statusInIndex := line [0 ]
195
+ statusOutOfIndex := line [1 ]
196
+
197
+ if statusInIndex != ' ' {
198
+ fInIndex += 1
199
+ }
200
+ if statusOutOfIndex != ' ' {
201
+ fOutOfIndex += 1
139
202
}
203
+
204
+ fTotal += 1
140
205
}
206
+
207
+ printPart (_partVcsGitIdxTotal , fTotal )
208
+ printPart (_partVcsGitIdxIncluded , fInIndex )
209
+ printPart (_partVcsGitIdxExcluded , fOutOfIndex )
141
210
})
142
211
143
212
cwg .Dispatch (func () {
@@ -147,8 +216,9 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
147
216
parts = []string {"0" , "0" }
148
217
}
149
218
150
- printPart ("vcs_log_ahead" , parts [0 ])
151
- printPart ("vcs_log_behind" , parts [1 ])
219
+ printPart (_partVcsLogAhead , parts [0 ])
220
+ printPart (_partVcsLogBehind , parts [1 ])
221
+
152
222
}
153
223
})
154
224
})
@@ -161,19 +231,19 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
161
231
162
232
var stgSeriesLen string
163
233
if stgSeriesLen , err = stringExec ("stg" , "series" , "-c" ); err == nil {
164
- printPart ("stg" , "1" )
165
- printPart ("stg_qlen" , stgSeriesLen )
234
+ printPart (_partVcsStg , "1" )
235
+ printPart (_partVcsStgQlen , stgSeriesLen )
166
236
}
167
237
168
238
cwg .Dispatch (func () {
169
239
if stgSeriesPos , err := stringExec ("stg" , "series" , "-cA" ); err == nil {
170
- printPart ("stg_qpos" , stgSeriesPos )
240
+ printPart (_partVcsStgQpos , stgSeriesPos )
171
241
}
172
242
})
173
243
174
244
var stgPatchTop string
175
245
if stgPatchTop , err = stringExec ("stg" , "top" ); err == nil {
176
- printPart ("stg_top" , stgPatchTop )
246
+ printPart (_partVcsStgTop , stgPatchTop )
177
247
} else {
178
248
return
179
249
}
@@ -183,9 +253,9 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
183
253
stgSHA , _ := stringExec ("stg" , "id" , stgPatchTop )
184
254
185
255
if gitSHA != stgSHA {
186
- printPart ("stg_dirty" , 1 )
256
+ printPart (_partVcsStgDirty , 1 )
187
257
} else {
188
- printPart ("stg_dirty" , 0 )
258
+ printPart (_partVcsStgDirty , 0 )
189
259
}
190
260
})
191
261
})
0 commit comments