Skip to content

Commit f5e59af

Browse files
committed
cleanup patch
1 parent a962248 commit f5e59af

File tree

8 files changed

+313
-177
lines changed

8 files changed

+313
-177
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ prompt.source:
1111

1212
try: install
1313
ZSH_DISABLE_PROMPT=Y ZSH_EXTRA_SOURCE="$(ZSH_PROMPT_SETUP_SCRIPT)" zsh
14+
15+
exec: install
16+
ZSH_DISABLE_PROMPT=Y ZSH_EXTRA_SOURCE="$(ZSH_PROMPT_SETUP_SCRIPT)" exec zsh -l

cmd/goprompt/cmdQuery.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"time"
88

9+
ps "github.com/mitchellh/go-ps"
910
"github.com/spf13/cobra"
1011
)
1112

@@ -28,7 +29,14 @@ func init() {
2829
cmdQuery.RunE = cmdQueryRun
2930
}
3031

32+
func timeFMT(ts time.Time) string {
33+
return ts.Format("15:04:05 01/02/06")
34+
}
35+
3136
func cmdQueryRun(_ *cobra.Command, _ []string) error {
37+
nowTS := time.Now()
38+
printPart("ts", timeFMT(nowTS))
39+
3240
if *flgQCmdStatus != 0 {
3341
printPart("st", fmt.Sprintf("%#v", *flgQCmdStatus))
3442
}
@@ -42,13 +50,10 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
4250
if wd, err := os.Getwd(); err == nil {
4351
wdh := strings.Replace(wd, homeDir, "~", 1)
4452

45-
printPart("wd_full", wdh)
46-
printPart("wd", trimPath(wdh))
53+
printPart("wd", wdh)
54+
printPart("wd_trim", trimPath(wdh))
4755
}
4856

49-
nowTS := time.Now()
50-
printPart("ts", nowTS.Format("15:04:05 01/02/06"))
51-
5257
if *flgQPreexecTS != 0 {
5358
cmdTS := time.Unix(int64(*flgQPreexecTS), 0)
5459

@@ -59,6 +64,35 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
5964
}
6065
})
6166

67+
wg.Dispatch(func() {
68+
pidCurr := os.Getpid()
69+
var pidShell ps.Process
70+
71+
for i := 0; i < 3; i++ {
72+
var err error
73+
pidShell, err = ps.FindProcess(pidCurr)
74+
if err != nil {
75+
return
76+
}
77+
pidCurr = pidShell.PPid()
78+
}
79+
80+
if pidShell == nil {
81+
return
82+
}
83+
84+
printPart("pid_shell", pidShell.Pid())
85+
printPart("pid_shell_exec", pidShell.Executable())
86+
87+
pidShellParent, err := ps.FindProcess(pidShell.PPid())
88+
if err != nil {
89+
return
90+
}
91+
92+
printPart("pid_parent", pidShellParent.Pid())
93+
printPart("pid_parent_exec", pidShellParent.Executable())
94+
})
95+
6296
//wg.Dispatch(func() {
6397
// out, err := stringExec("git", "config", "--list")
6498
// printPart("debug_o", js(out))

cmd/goprompt/cmdRender.go

Lines changed: 108 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/gookit/color"
65
"io"
76
"os"
87
"strings"
8+
"time"
99

10+
"github.com/gookit/color"
1011
"github.com/spf13/cobra"
1112
)
1213

@@ -16,25 +17,65 @@ var (
1617
Short: "render the prompt based on the results of query",
1718
}
1819

19-
flgRIncomplete = cmdRender.PersistentFlags().Bool(
20-
"prompt-incomplete", false,
21-
"is prompt query done rendering",
20+
flgRLoading = cmdRender.PersistentFlags().Bool(
21+
"prompt-loading", false,
22+
"is prompt query not yet done rendering",
23+
)
24+
flgRColorMode = cmdRender.PersistentFlags().String(
25+
"color-mode", "none",
26+
"color rendering mode of the prompt (zsh, ascii, none)",
2227
)
2328
flgRMode = cmdRender.PersistentFlags().String(
2429
"prompt-mode", "normal",
2530
"mode of the prompt (normal, edit)",
2631
)
32+
33+
// DEPRECATED
2734
flgRNewline = cmdRender.PersistentFlags().String(
2835
"newline", "\n",
2936
"newline for the prompt",
3037
)
3138
)
3239

40+
var (
41+
redC = fmt.Sprint
42+
greenC = fmt.Sprint
43+
yellowC = fmt.Sprint
44+
blueC = fmt.Sprint
45+
magentaC = fmt.Sprint
46+
normalC = fmt.Sprint
47+
)
48+
49+
func setColorMode(mode string) {
50+
wrapC := func(pref, suff string) func (args ...interface{}) string {
51+
return func(args ...interface{}) string {
52+
return pref + fmt.Sprint(args...) + suff
53+
}
54+
}
55+
56+
if mode == "zsh" {
57+
redC = wrapC("%F{red}", "%F{reset}")
58+
greenC = wrapC("%F{green}", "%F{reset}")
59+
yellowC = wrapC("%F{yellow}", "%F{reset}")
60+
blueC = wrapC("%F{blue}", "%F{reset}")
61+
magentaC = wrapC("%F{magenta}", "%F{reset}")
62+
} else if mode == "ascii" {
63+
redC = color.Red.Render
64+
greenC = color.Green.Render
65+
yellowC = color.Yellow.Render
66+
blueC = color.Blue.Render
67+
magentaC = color.Magenta.Render
68+
}
69+
}
70+
71+
3372
func init() {
3473
cmdRender.RunE = cmdRenderRun
3574
}
3675

3776
func cmdRenderRun(_ *cobra.Command, _ []string) error {
77+
setColorMode(*flgRColorMode)
78+
3879
if _, err := os.Stdin.Stat(); err != nil {
3980
fmt.Printf("%#v", err)
4081
}
@@ -57,19 +98,21 @@ func cmdRenderRun(_ *cobra.Command, _ []string) error {
5798
if p["vcs"] == "git" {
5899
var gitParts []string
59100

60-
gitMark := fmt.Sprint("git")
61-
gitMarkC := color.Green.Render
101+
gitMark := "git"
102+
gitMarkC := yellowC
62103

63104
gitBranch := fmt.Sprint(p["vcs_br"])
64-
gitBranchC := color.Green.Render
105+
gitBranchC := greenC
65106

66107
gitDirtyMarks := ""
108+
gitDirtyMarksC := redC
67109
if p["vcs_dirty"] != "" && p["vcs_dirty"] != "0" {
68-
gitDirtyMarks = fmt.Sprint("&")
69-
gitMarkC = color.Yellow.Render
110+
gitDirtyMarks = "&"
70111
}
71112

72113
distanceMarks := ""
114+
distanceMarksC := magentaC
115+
73116
distanceAhead := strInt(p["vcs_log_ahead"])
74117
distanceBehind := strInt(p["vcs_log_ahead"])
75118
if distanceAhead > 0 || distanceBehind > 0 {
@@ -79,32 +122,80 @@ func cmdRenderRun(_ *cobra.Command, _ []string) error {
79122
gitParts = append(gitParts, gitMarkC(gitMark))
80123
gitParts = append(gitParts, gitBranchC(gitBranch))
81124
if len(gitDirtyMarks) > 0 {
82-
gitParts = append(gitParts, gitDirtyMarks)
125+
gitParts = append(gitParts, gitDirtyMarksC(gitDirtyMarks))
83126
}
84127
if len(distanceMarks) > 0 {
85-
gitParts = append(gitParts, distanceMarks)
128+
gitParts = append(gitParts, distanceMarksC(distanceMarks))
86129
}
87130

88131
partsTop = append(partsTop, fmt.Sprintf("{%v}", strings.Join(gitParts, ":")))
89132
}
90133

134+
if p["stg"] != "" {
135+
var stgParts []string
136+
137+
stgMark := "stg"
138+
stgMarkC := yellowC
139+
140+
stgTopPatch := p["stg_top"]
141+
stgTopPatchC := greenC
142+
143+
stgQueueMark := ""
144+
stgQueueMarkC := normalC
145+
146+
stgQueueLen := strInt(p["stg_qlen"])
147+
stgQueuePos := strInt(p["stg_qpos"])
148+
if stgQueuePos > 0 {
149+
stgQueueMark = fmt.Sprintf("%d/%d", stgQueuePos, stgQueueLen)
150+
}
151+
152+
if strInt(p["stg_dirty"]) != 0 {
153+
stgTopPatchC = redC
154+
}
155+
156+
stgParts = append(stgParts, stgMarkC(stgMark))
157+
158+
if len(stgTopPatch) > 0 {
159+
stgParts = append(stgParts, stgTopPatchC(stgTopPatch))
160+
161+
}
162+
163+
if len(stgQueueMark) > 0 {
164+
stgParts = append(stgParts, stgQueueMarkC(stgQueueMark))
165+
}
166+
167+
partsTop = append(partsTop, fmt.Sprintf("{%v}", strings.Join(stgParts, ":")))
168+
}
169+
91170
var partsBottom []string
92171
if strInt(p["st"]) > 0 {
93-
partsBottom = append(partsBottom, fmt.Sprintf("[%v]", p["st"]))
172+
partsBottom = append(partsBottom, redC("["+p["st"]+"]"))
173+
}
174+
175+
if p["pid_parent_exec"] != "" {
176+
partsBottom = append(partsBottom, "("+p["pid_parent_exec"]+")")
94177
}
95-
partsBottom = append(partsBottom, fmt.Sprintf("(%v)", p["wd"]))
178+
179+
partsBottom = append(partsBottom, yellowC("(")+blueC(p["wd_trim"])+yellowC(")"))
180+
96181
if p["ds"] != "" {
97182
partsBottom = append(partsBottom, fmt.Sprintf("%v", p["ds"]))
98183
}
99-
partsBottom = append(partsBottom, fmt.Sprintf("[%v]", p["ts"]))
100184

101-
promptMarker := fmt.Sprint(">")
185+
nowTS := time.Now()
186+
cmdTS := timeFMT(nowTS)
187+
if len(p["ts"]) != 0 {
188+
cmdTS = p["ts"]
189+
}
190+
partsBottom = append(partsBottom, fmt.Sprintf("[%v]", cmdTS))
191+
192+
promptMarker := magentaC(">")
102193
if *flgRMode == "edit" {
103-
promptMarker = fmt.Sprint("<")
194+
promptMarker = redC("<")
104195
}
105196

106197
promptStatusMarker := ":: "
107-
if *flgRIncomplete {
198+
if *flgRLoading {
108199
promptStatusMarker = ":? "
109200
}
110201

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.18
44

55
require (
66
github.com/gookit/color v1.5.1
7+
github.com/mitchellh/go-ps v1.0.0
78
github.com/spf13/cobra v1.5.0
89
)
910

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/gookit/color v1.5.1 h1:Vjg2VEcdHpwq+oY63s/ksHrgJYCTo0bwWvmmYWdE9fQ=
55
github.com/gookit/color v1.5.1/go.mod h1:wZFzea4X8qN6vHOSP2apMb4/+w/orMznEzYsIHPaqKM=
66
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
77
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
8+
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
9+
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
810
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
911
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1012
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=

0 commit comments

Comments
 (0)