Skip to content

Commit

Permalink
cleanup patch
Browse files Browse the repository at this point in the history
  • Loading branch information
NonLogicalDev committed Jul 22, 2022
1 parent a962248 commit f5e59af
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 177 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ prompt.source:

try: install
ZSH_DISABLE_PROMPT=Y ZSH_EXTRA_SOURCE="$(ZSH_PROMPT_SETUP_SCRIPT)" zsh

exec: install
ZSH_DISABLE_PROMPT=Y ZSH_EXTRA_SOURCE="$(ZSH_PROMPT_SETUP_SCRIPT)" exec zsh -l
44 changes: 39 additions & 5 deletions cmd/goprompt/cmdQuery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"

ps "github.com/mitchellh/go-ps"
"github.com/spf13/cobra"
)

Expand All @@ -28,7 +29,14 @@ func init() {
cmdQuery.RunE = cmdQueryRun
}

func timeFMT(ts time.Time) string {
return ts.Format("15:04:05 01/02/06")
}

func cmdQueryRun(_ *cobra.Command, _ []string) error {
nowTS := time.Now()
printPart("ts", timeFMT(nowTS))

if *flgQCmdStatus != 0 {
printPart("st", fmt.Sprintf("%#v", *flgQCmdStatus))
}
Expand All @@ -42,13 +50,10 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
if wd, err := os.Getwd(); err == nil {
wdh := strings.Replace(wd, homeDir, "~", 1)

printPart("wd_full", wdh)
printPart("wd", trimPath(wdh))
printPart("wd", wdh)
printPart("wd_trim", trimPath(wdh))
}

nowTS := time.Now()
printPart("ts", nowTS.Format("15:04:05 01/02/06"))

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

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

wg.Dispatch(func() {
pidCurr := os.Getpid()
var pidShell ps.Process

for i := 0; i < 3; i++ {
var err error
pidShell, err = ps.FindProcess(pidCurr)
if err != nil {
return
}
pidCurr = pidShell.PPid()
}

if pidShell == nil {
return
}

printPart("pid_shell", pidShell.Pid())
printPart("pid_shell_exec", pidShell.Executable())

pidShellParent, err := ps.FindProcess(pidShell.PPid())
if err != nil {
return
}

printPart("pid_parent", pidShellParent.Pid())
printPart("pid_parent_exec", pidShellParent.Executable())
})

//wg.Dispatch(func() {
// out, err := stringExec("git", "config", "--list")
// printPart("debug_o", js(out))
Expand Down
125 changes: 108 additions & 17 deletions cmd/goprompt/cmdRender.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"fmt"
"github.com/gookit/color"
"io"
"os"
"strings"
"time"

"github.com/gookit/color"
"github.com/spf13/cobra"
)

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

flgRIncomplete = cmdRender.PersistentFlags().Bool(
"prompt-incomplete", false,
"is prompt query done rendering",
flgRLoading = cmdRender.PersistentFlags().Bool(
"prompt-loading", false,
"is prompt query not yet done rendering",
)
flgRColorMode = cmdRender.PersistentFlags().String(
"color-mode", "none",
"color rendering mode of the prompt (zsh, ascii, none)",
)
flgRMode = cmdRender.PersistentFlags().String(
"prompt-mode", "normal",
"mode of the prompt (normal, edit)",
)

// DEPRECATED
flgRNewline = cmdRender.PersistentFlags().String(
"newline", "\n",
"newline for the prompt",
)
)

var (
redC = fmt.Sprint
greenC = fmt.Sprint
yellowC = fmt.Sprint
blueC = fmt.Sprint
magentaC = fmt.Sprint
normalC = fmt.Sprint
)

func setColorMode(mode string) {
wrapC := func(pref, suff string) func (args ...interface{}) string {
return func(args ...interface{}) string {
return pref + fmt.Sprint(args...) + suff
}
}

if mode == "zsh" {
redC = wrapC("%F{red}", "%F{reset}")
greenC = wrapC("%F{green}", "%F{reset}")
yellowC = wrapC("%F{yellow}", "%F{reset}")
blueC = wrapC("%F{blue}", "%F{reset}")
magentaC = wrapC("%F{magenta}", "%F{reset}")
} else if mode == "ascii" {
redC = color.Red.Render
greenC = color.Green.Render
yellowC = color.Yellow.Render
blueC = color.Blue.Render
magentaC = color.Magenta.Render
}
}


func init() {
cmdRender.RunE = cmdRenderRun
}

func cmdRenderRun(_ *cobra.Command, _ []string) error {
setColorMode(*flgRColorMode)

if _, err := os.Stdin.Stat(); err != nil {
fmt.Printf("%#v", err)
}
Expand All @@ -57,19 +98,21 @@ func cmdRenderRun(_ *cobra.Command, _ []string) error {
if p["vcs"] == "git" {
var gitParts []string

gitMark := fmt.Sprint("git")
gitMarkC := color.Green.Render
gitMark := "git"
gitMarkC := yellowC

gitBranch := fmt.Sprint(p["vcs_br"])
gitBranchC := color.Green.Render
gitBranchC := greenC

gitDirtyMarks := ""
gitDirtyMarksC := redC
if p["vcs_dirty"] != "" && p["vcs_dirty"] != "0" {
gitDirtyMarks = fmt.Sprint("&")
gitMarkC = color.Yellow.Render
gitDirtyMarks = "&"
}

distanceMarks := ""
distanceMarksC := magentaC

distanceAhead := strInt(p["vcs_log_ahead"])
distanceBehind := strInt(p["vcs_log_ahead"])
if distanceAhead > 0 || distanceBehind > 0 {
Expand All @@ -79,32 +122,80 @@ func cmdRenderRun(_ *cobra.Command, _ []string) error {
gitParts = append(gitParts, gitMarkC(gitMark))
gitParts = append(gitParts, gitBranchC(gitBranch))
if len(gitDirtyMarks) > 0 {
gitParts = append(gitParts, gitDirtyMarks)
gitParts = append(gitParts, gitDirtyMarksC(gitDirtyMarks))
}
if len(distanceMarks) > 0 {
gitParts = append(gitParts, distanceMarks)
gitParts = append(gitParts, distanceMarksC(distanceMarks))
}

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

if p["stg"] != "" {
var stgParts []string

stgMark := "stg"
stgMarkC := yellowC

stgTopPatch := p["stg_top"]
stgTopPatchC := greenC

stgQueueMark := ""
stgQueueMarkC := normalC

stgQueueLen := strInt(p["stg_qlen"])
stgQueuePos := strInt(p["stg_qpos"])
if stgQueuePos > 0 {
stgQueueMark = fmt.Sprintf("%d/%d", stgQueuePos, stgQueueLen)
}

if strInt(p["stg_dirty"]) != 0 {
stgTopPatchC = redC
}

stgParts = append(stgParts, stgMarkC(stgMark))

if len(stgTopPatch) > 0 {
stgParts = append(stgParts, stgTopPatchC(stgTopPatch))

}

if len(stgQueueMark) > 0 {
stgParts = append(stgParts, stgQueueMarkC(stgQueueMark))
}

partsTop = append(partsTop, fmt.Sprintf("{%v}", strings.Join(stgParts, ":")))
}

var partsBottom []string
if strInt(p["st"]) > 0 {
partsBottom = append(partsBottom, fmt.Sprintf("[%v]", p["st"]))
partsBottom = append(partsBottom, redC("["+p["st"]+"]"))
}

if p["pid_parent_exec"] != "" {
partsBottom = append(partsBottom, "("+p["pid_parent_exec"]+")")
}
partsBottom = append(partsBottom, fmt.Sprintf("(%v)", p["wd"]))

partsBottom = append(partsBottom, yellowC("(")+blueC(p["wd_trim"])+yellowC(")"))

if p["ds"] != "" {
partsBottom = append(partsBottom, fmt.Sprintf("%v", p["ds"]))
}
partsBottom = append(partsBottom, fmt.Sprintf("[%v]", p["ts"]))

promptMarker := fmt.Sprint(">")
nowTS := time.Now()
cmdTS := timeFMT(nowTS)
if len(p["ts"]) != 0 {
cmdTS = p["ts"]
}
partsBottom = append(partsBottom, fmt.Sprintf("[%v]", cmdTS))

promptMarker := magentaC(">")
if *flgRMode == "edit" {
promptMarker = fmt.Sprint("<")
promptMarker = redC("<")
}

promptStatusMarker := ":: "
if *flgRIncomplete {
if *flgRLoading {
promptStatusMarker = ":? "
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/gookit/color v1.5.1
github.com/mitchellh/go-ps v1.0.0
github.com/spf13/cobra v1.5.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/gookit/color v1.5.1 h1:Vjg2VEcdHpwq+oY63s/ksHrgJYCTo0bwWvmmYWdE9fQ=
github.com/gookit/color v1.5.1/go.mod h1:wZFzea4X8qN6vHOSP2apMb4/+w/orMznEzYsIHPaqKM=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
Loading

0 comments on commit f5e59af

Please sign in to comment.