Skip to content

Commit de76297

Browse files
committed
misc improvements for the first usable post-PoC version
* decrease jitter in prompt by only re-rendering on empty new-lines * granding mechanism to the binary to bundle updates in chunks * change the goprompt binary to bundle updates every 600 Milliseconds
1 parent f5e59af commit de76297

File tree

6 files changed

+230
-103
lines changed

6 files changed

+230
-103
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
22
CURRENT_DIR := $(patsubst %/,%,$(dir $(MKFILE_PATH)))
33

4-
ZSH_PROMPT_SETUP_SCRIPT := $(CURRENT_DIR)/zsh/prompt_asynczle_setup.zsh
4+
ZSH_PROMPT_SETUP_SCRIPT := $(CURRENT_DIR)/plugin/zsh/prompt_asynczle_setup.zsh
55

66
install:
77
go install ./cmd/goprompt

cmd/goprompt/cmdQuery.go

Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,37 @@ var (
2525
)
2626
)
2727

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+
2859
func init() {
2960
cmdQuery.RunE = cmdQueryRun
3061
}
@@ -34,11 +65,19 @@ func timeFMT(ts time.Time) string {
3465
}
3566

3667
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+
3776
nowTS := time.Now()
38-
printPart("ts", timeFMT(nowTS))
77+
printPart(_partTimestamp, timeFMT(nowTS))
3978

4079
if *flgQCmdStatus != 0 {
41-
printPart("st", fmt.Sprintf("%#v", *flgQCmdStatus))
80+
printPart(_partStatus, fmt.Sprintf("%#v", *flgQCmdStatus))
4281
}
4382

4483
wg := new(WaitGroupDispatcher)
@@ -50,16 +89,16 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
5089
if wd, err := os.Getwd(); err == nil {
5190
wdh := strings.Replace(wd, homeDir, "~", 1)
5291

53-
printPart("wd", wdh)
54-
printPart("wd_trim", trimPath(wdh))
92+
printPart(_partWorkDir, wdh)
93+
printPart(_partWorkDirShort, trimPath(wdh))
5594
}
5695

5796
if *flgQPreexecTS != 0 {
5897
cmdTS := time.Unix(int64(*flgQPreexecTS), 0)
5998

6099
diff := nowTS.Sub(cmdTS).Round(time.Second)
61100
if diff > 1 {
62-
printPart("ds", diff)
101+
printPart(_partDuration, diff)
63102
}
64103
}
65104
})
@@ -81,16 +120,16 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
81120
return
82121
}
83122

84-
printPart("pid_shell", pidShell.Pid())
85-
printPart("pid_shell_exec", pidShell.Executable())
123+
printPart(_partPidShell, pidShell.Pid())
124+
printPart(_partPidShellExec, pidShell.Executable())
86125

87126
pidShellParent, err := ps.FindProcess(pidShell.PPid())
88127
if err != nil {
89128
return
90129
}
91130

92-
printPart("pid_parent", pidShellParent.Pid())
93-
printPart("pid_parent_exec", pidShellParent.Executable())
131+
printPart(_partPidParent, pidShellParent.Pid())
132+
printPart(_partPidParentExec, pidShellParent.Executable())
94133
})
95134

96135
//wg.Dispatch(func() {
@@ -106,7 +145,7 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
106145
defer cwg.Wait()
107146

108147
if _, err := stringExec("git", "rev-parse", "--show-toplevel"); err == nil {
109-
printPart("vcs", "git")
148+
printPart(_partVcs, "git")
110149
} else {
111150
return
112151
}
@@ -115,29 +154,59 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
115154
if branch, err := stringExec("git", "branch", "--show-current"); err == nil {
116155
branch = trim(branch)
117156
if len(branch) > 0 {
118-
printPart("vcs_br", trim(branch))
157+
printPart(_partVcsBranch, trim(branch))
119158
return
120159
}
121160
}
122161

123162
if branch, err := stringExec("git", "name-rev", "--name-only", "HEAD"); err == nil {
124163
branch = trim(branch)
125164
if len(branch) > 0 {
126-
printPart("vcs_br", trim(branch))
165+
printPart(_partVcsBranch, trim(branch))
127166
return
128167
}
129168
}
130169
})
131170

132171
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
139202
}
203+
204+
fTotal += 1
140205
}
206+
207+
printPart(_partVcsGitIdxTotal, fTotal)
208+
printPart(_partVcsGitIdxIncluded, fInIndex)
209+
printPart(_partVcsGitIdxExcluded, fOutOfIndex)
141210
})
142211

143212
cwg.Dispatch(func() {
@@ -147,8 +216,9 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
147216
parts = []string{"0", "0"}
148217
}
149218

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+
152222
}
153223
})
154224
})
@@ -161,19 +231,19 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
161231

162232
var stgSeriesLen string
163233
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)
166236
}
167237

168238
cwg.Dispatch(func() {
169239
if stgSeriesPos, err := stringExec("stg", "series", "-cA"); err == nil {
170-
printPart("stg_qpos", stgSeriesPos)
240+
printPart(_partVcsStgQpos, stgSeriesPos)
171241
}
172242
})
173243

174244
var stgPatchTop string
175245
if stgPatchTop, err = stringExec("stg", "top"); err == nil {
176-
printPart("stg_top", stgPatchTop)
246+
printPart(_partVcsStgTop, stgPatchTop)
177247
} else {
178248
return
179249
}
@@ -183,9 +253,9 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
183253
stgSHA, _ := stringExec("stg", "id", stgPatchTop)
184254

185255
if gitSHA != stgSHA {
186-
printPart("stg_dirty", 1)
256+
printPart(_partVcsStgDirty, 1)
187257
} else {
188-
printPart("stg_dirty", 0)
258+
printPart(_partVcsStgDirty, 0)
189259
}
190260
})
191261
})

cmd/goprompt/cmdRender.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var (
4747
)
4848

4949
func setColorMode(mode string) {
50-
wrapC := func(pref, suff string) func (args ...interface{}) string {
50+
wrapC := func(pref, suff string) func(args ...interface{}) string {
5151
return func(args ...interface{}) string {
5252
return pref + fmt.Sprint(args...) + suff
5353
}
@@ -68,7 +68,6 @@ func setColorMode(mode string) {
6868
}
6969
}
7070

71-
7271
func init() {
7372
cmdRender.RunE = cmdRenderRun
7473
}
@@ -95,26 +94,30 @@ func cmdRenderRun(_ *cobra.Command, _ []string) error {
9594
}
9695

9796
var partsTop []string
98-
if p["vcs"] == "git" {
97+
if p[_partVcs] == "git" {
9998
var gitParts []string
10099

101100
gitMark := "git"
102101
gitMarkC := yellowC
103102

104-
gitBranch := fmt.Sprint(p["vcs_br"])
103+
gitBranch := fmt.Sprint(p[_partVcsBranch])
105104
gitBranchC := greenC
106105

107106
gitDirtyMarks := ""
108107
gitDirtyMarksC := redC
109-
if p["vcs_dirty"] != "" && p["vcs_dirty"] != "0" {
108+
if p[_partVcsDirty] != "" && p[_partVcsDirty] != "0" {
110109
gitDirtyMarks = "&"
110+
111+
if p[_partVcsGitIdxExcluded] == "0" {
112+
gitDirtyMarksC = greenC
113+
}
111114
}
112115

113116
distanceMarks := ""
114117
distanceMarksC := magentaC
115118

116-
distanceAhead := strInt(p["vcs_log_ahead"])
117-
distanceBehind := strInt(p["vcs_log_ahead"])
119+
distanceAhead := strInt(p[_partVcsLogAhead])
120+
distanceBehind := strInt(p[_partVcsLogBehind])
118121
if distanceAhead > 0 || distanceBehind > 0 {
119122
distanceMarks = fmt.Sprintf("[+%v:-%v]", distanceAhead, distanceBehind)
120123
}
@@ -131,25 +134,25 @@ func cmdRenderRun(_ *cobra.Command, _ []string) error {
131134
partsTop = append(partsTop, fmt.Sprintf("{%v}", strings.Join(gitParts, ":")))
132135
}
133136

134-
if p["stg"] != "" {
137+
if p[_partVcsStg] != "" {
135138
var stgParts []string
136139

137140
stgMark := "stg"
138141
stgMarkC := yellowC
139142

140-
stgTopPatch := p["stg_top"]
143+
stgTopPatch := p[_partVcsStgTop]
141144
stgTopPatchC := greenC
142145

143146
stgQueueMark := ""
144147
stgQueueMarkC := normalC
145148

146-
stgQueueLen := strInt(p["stg_qlen"])
147-
stgQueuePos := strInt(p["stg_qpos"])
149+
stgQueueLen := strInt(p[_partVcsStgQlen])
150+
stgQueuePos := strInt(p[_partVcsStgQpos])
148151
if stgQueuePos > 0 {
149152
stgQueueMark = fmt.Sprintf("%d/%d", stgQueuePos, stgQueueLen)
150153
}
151154

152-
if strInt(p["stg_dirty"]) != 0 {
155+
if strInt(p[_partVcsStgDirty]) != 0 {
153156
stgTopPatchC = redC
154157
}
155158

@@ -168,24 +171,24 @@ func cmdRenderRun(_ *cobra.Command, _ []string) error {
168171
}
169172

170173
var partsBottom []string
171-
if strInt(p["st"]) > 0 {
172-
partsBottom = append(partsBottom, redC("["+p["st"]+"]"))
174+
if strInt(p[_partStatus]) > 0 {
175+
partsBottom = append(partsBottom, redC("["+p[_partStatus]+"]"))
173176
}
174177

175-
if p["pid_parent_exec"] != "" {
176-
partsBottom = append(partsBottom, "("+p["pid_parent_exec"]+")")
178+
if p[_partPidParentExec] != "" {
179+
partsBottom = append(partsBottom, "("+p[_partPidParentExec]+")")
177180
}
178181

179-
partsBottom = append(partsBottom, yellowC("(")+blueC(p["wd_trim"])+yellowC(")"))
182+
partsBottom = append(partsBottom, yellowC("(")+blueC(p[_partWorkDirShort])+yellowC(")"))
180183

181-
if p["ds"] != "" {
182-
partsBottom = append(partsBottom, fmt.Sprintf("%v", p["ds"]))
184+
if p[_partDuration] != "" {
185+
partsBottom = append(partsBottom, fmt.Sprintf("%v", p[_partDuration]))
183186
}
184187

185188
nowTS := time.Now()
186189
cmdTS := timeFMT(nowTS)
187-
if len(p["ts"]) != 0 {
188-
cmdTS = p["ts"]
190+
if len(p[_partTimestamp]) != 0 {
191+
cmdTS = p[_partTimestamp]
189192
}
190193
partsBottom = append(partsBottom, fmt.Sprintf("[%v]", cmdTS))
191194

@@ -199,11 +202,14 @@ func cmdRenderRun(_ *cobra.Command, _ []string) error {
199202
promptStatusMarker = ":? "
200203
}
201204

202-
promptLines := []string{"",
203-
promptStatusMarker + strings.Join(partsTop, " "),
204-
promptStatusMarker + strings.Join(partsBottom, " "),
205-
promptMarker,
205+
promptLines := []string{""}
206+
if len(partsTop) > 0 {
207+
promptLines = append(promptLines, promptStatusMarker+strings.Join(partsTop, " "))
208+
}
209+
if len(partsBottom) > 0 {
210+
promptLines = append(promptLines, promptStatusMarker+strings.Join(partsBottom, " "))
206211
}
212+
promptLines = append(promptLines, promptMarker)
207213

208214
fmt.Print(strings.Join(promptLines, "\n"))
209215

0 commit comments

Comments
 (0)