Skip to content

Commit a962248

Browse files
committed
update
1 parent 632f7a2 commit a962248

File tree

7 files changed

+618
-679
lines changed

7 files changed

+618
-679
lines changed

cmd/goprompt/cmdQuery.go

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"time"
8+
9+
"github.com/spf13/cobra"
10+
)
11+
12+
var (
13+
cmdQuery = &cobra.Command{
14+
Use: "query",
15+
Short: "start the query that pulls data for the prompt",
16+
}
17+
flgQCmdStatus = cmdQuery.PersistentFlags().Int(
18+
"cmd-status", 0,
19+
"cmd status of previous command",
20+
)
21+
flgQPreexecTS = cmdQuery.PersistentFlags().Int(
22+
"preexec-ts", 0,
23+
"pre-execution timestamp to gauge how log execution took",
24+
)
25+
)
26+
27+
func init() {
28+
cmdQuery.RunE = cmdQueryRun
29+
}
30+
31+
func cmdQueryRun(_ *cobra.Command, _ []string) error {
32+
if *flgQCmdStatus != 0 {
33+
printPart("st", fmt.Sprintf("%#v", *flgQCmdStatus))
34+
}
35+
36+
wg := new(WaitGroupDispatcher)
37+
defer wg.Wait()
38+
39+
wg.Dispatch(func() {
40+
homeDir := os.Getenv("HOME")
41+
42+
if wd, err := os.Getwd(); err == nil {
43+
wdh := strings.Replace(wd, homeDir, "~", 1)
44+
45+
printPart("wd_full", wdh)
46+
printPart("wd", trimPath(wdh))
47+
}
48+
49+
nowTS := time.Now()
50+
printPart("ts", nowTS.Format("15:04:05 01/02/06"))
51+
52+
if *flgQPreexecTS != 0 {
53+
cmdTS := time.Unix(int64(*flgQPreexecTS), 0)
54+
55+
diff := nowTS.Sub(cmdTS).Round(time.Second)
56+
if diff > 1 {
57+
printPart("ds", diff)
58+
}
59+
}
60+
})
61+
62+
//wg.Dispatch(func() {
63+
// out, err := stringExec("git", "config", "--list")
64+
// printPart("debug_o", js(out))
65+
// if err != nil {
66+
// printPart("debug_e", js(err.Error()))
67+
// }
68+
//})
69+
70+
wg.Dispatch(func() {
71+
cwg := new(WaitGroupDispatcher)
72+
defer cwg.Wait()
73+
74+
if _, err := stringExec("git", "rev-parse", "--show-toplevel"); err == nil {
75+
printPart("vcs", "git")
76+
} else {
77+
return
78+
}
79+
80+
cwg.Dispatch(func() {
81+
if branch, err := stringExec("git", "branch", "--show-current"); err == nil {
82+
branch = trim(branch)
83+
if len(branch) > 0 {
84+
printPart("vcs_br", trim(branch))
85+
return
86+
}
87+
}
88+
89+
if branch, err := stringExec("git", "name-rev", "--name-only", "HEAD"); err == nil {
90+
branch = trim(branch)
91+
if len(branch) > 0 {
92+
printPart("vcs_br", trim(branch))
93+
return
94+
}
95+
}
96+
})
97+
98+
cwg.Dispatch(func() {
99+
if status, err := stringExec("git", "status", "--porcelain"); err == nil {
100+
if len(status) > 0 {
101+
printPart("vcs_dirty", 1)
102+
//printPart("vcs_dirty_st", js(status))
103+
} else {
104+
printPart("vsc_dirty", 0)
105+
}
106+
}
107+
})
108+
109+
cwg.Dispatch(func() {
110+
if status, err := stringExec("git", "rev-list", "--left-right", "--count", "HEAD...@{u}"); err == nil {
111+
parts := strings.SplitN(status, "\t", 2)
112+
if len(parts) < 2 {
113+
parts = []string{"0", "0"}
114+
}
115+
116+
printPart("vcs_log_ahead", parts[0])
117+
printPart("vcs_log_behind", parts[1])
118+
}
119+
})
120+
})
121+
122+
wg.Dispatch(func() {
123+
var err error
124+
125+
cwg := new(WaitGroupDispatcher)
126+
defer cwg.Wait()
127+
128+
var stgSeriesLen string
129+
if stgSeriesLen, err = stringExec("stg", "series", "-c"); err == nil {
130+
printPart("stg", "1")
131+
printPart("stg_qlen", stgSeriesLen)
132+
}
133+
134+
cwg.Dispatch(func() {
135+
if stgSeriesPos, err := stringExec("stg", "series", "-cA"); err == nil {
136+
printPart("stg_qpos", stgSeriesPos)
137+
}
138+
})
139+
140+
var stgPatchTop string
141+
if stgPatchTop, err = stringExec("stg", "top"); err == nil {
142+
printPart("stg_top", stgPatchTop)
143+
} else {
144+
return
145+
}
146+
147+
cwg.Dispatch(func() {
148+
gitSHA, _ := stringExec("stg", "id")
149+
stgSHA, _ := stringExec("stg", "id", stgPatchTop)
150+
151+
if gitSHA != stgSHA {
152+
printPart("stg_dirty", 1)
153+
} else {
154+
printPart("stg_dirty", 0)
155+
}
156+
})
157+
})
158+
159+
return nil
160+
}

cmd/goprompt/cmdRender.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/gookit/color"
6+
"io"
7+
"os"
8+
"strings"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var (
14+
cmdRender = &cobra.Command{
15+
Use: "render",
16+
Short: "render the prompt based on the results of query",
17+
}
18+
19+
flgRIncomplete = cmdRender.PersistentFlags().Bool(
20+
"prompt-incomplete", false,
21+
"is prompt query done rendering",
22+
)
23+
flgRMode = cmdRender.PersistentFlags().String(
24+
"prompt-mode", "normal",
25+
"mode of the prompt (normal, edit)",
26+
)
27+
flgRNewline = cmdRender.PersistentFlags().String(
28+
"newline", "\n",
29+
"newline for the prompt",
30+
)
31+
)
32+
33+
func init() {
34+
cmdRender.RunE = cmdRenderRun
35+
}
36+
37+
func cmdRenderRun(_ *cobra.Command, _ []string) error {
38+
if _, err := os.Stdin.Stat(); err != nil {
39+
fmt.Printf("%#v", err)
40+
}
41+
42+
out, err := io.ReadAll(os.Stdin)
43+
if err != nil {
44+
return err
45+
}
46+
47+
lines := strings.Split(string(out), "\n")
48+
p := make(map[string]string)
49+
for _, line := range lines {
50+
key, value, ok := strings.Cut(line, "\t")
51+
if ok {
52+
p[key] = value
53+
}
54+
}
55+
56+
var partsTop []string
57+
if p["vcs"] == "git" {
58+
var gitParts []string
59+
60+
gitMark := fmt.Sprint("git")
61+
gitMarkC := color.Green.Render
62+
63+
gitBranch := fmt.Sprint(p["vcs_br"])
64+
gitBranchC := color.Green.Render
65+
66+
gitDirtyMarks := ""
67+
if p["vcs_dirty"] != "" && p["vcs_dirty"] != "0" {
68+
gitDirtyMarks = fmt.Sprint("&")
69+
gitMarkC = color.Yellow.Render
70+
}
71+
72+
distanceMarks := ""
73+
distanceAhead := strInt(p["vcs_log_ahead"])
74+
distanceBehind := strInt(p["vcs_log_ahead"])
75+
if distanceAhead > 0 || distanceBehind > 0 {
76+
distanceMarks = fmt.Sprintf("[+%v:-%v]", distanceAhead, distanceBehind)
77+
}
78+
79+
gitParts = append(gitParts, gitMarkC(gitMark))
80+
gitParts = append(gitParts, gitBranchC(gitBranch))
81+
if len(gitDirtyMarks) > 0 {
82+
gitParts = append(gitParts, gitDirtyMarks)
83+
}
84+
if len(distanceMarks) > 0 {
85+
gitParts = append(gitParts, distanceMarks)
86+
}
87+
88+
partsTop = append(partsTop, fmt.Sprintf("{%v}", strings.Join(gitParts, ":")))
89+
}
90+
91+
var partsBottom []string
92+
if strInt(p["st"]) > 0 {
93+
partsBottom = append(partsBottom, fmt.Sprintf("[%v]", p["st"]))
94+
}
95+
partsBottom = append(partsBottom, fmt.Sprintf("(%v)", p["wd"]))
96+
if p["ds"] != "" {
97+
partsBottom = append(partsBottom, fmt.Sprintf("%v", p["ds"]))
98+
}
99+
partsBottom = append(partsBottom, fmt.Sprintf("[%v]", p["ts"]))
100+
101+
promptMarker := fmt.Sprint(">")
102+
if *flgRMode == "edit" {
103+
promptMarker = fmt.Sprint("<")
104+
}
105+
106+
promptStatusMarker := ":: "
107+
if *flgRIncomplete {
108+
promptStatusMarker = ":? "
109+
}
110+
111+
promptLines := []string{"",
112+
promptStatusMarker + strings.Join(partsTop, " "),
113+
promptStatusMarker + strings.Join(partsBottom, " "),
114+
promptMarker,
115+
}
116+
117+
fmt.Print(strings.Join(promptLines, "\n"))
118+
119+
return nil
120+
}

0 commit comments

Comments
 (0)