Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
NonLogicalDev committed Jun 28, 2022
1 parent 167e2d7 commit 266f995
Show file tree
Hide file tree
Showing 7 changed files with 603 additions and 287 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
CURRENT_DIR := $(patsubst %/,%,$(dir $(MKFILE_PATH)))

ZSH_PROMPT_SETUP_SCRIPT := $(CURRENT_DIR)/zsh/prompt_goprompt_setup.zsh
ZSH_PROMPT_SETUP_SCRIPT := $(CURRENT_DIR)/zsh/prompt_asynczle_setup.zsh

install:
go install ./cmd/goprompt
Expand Down
249 changes: 172 additions & 77 deletions cmd/goprompt/main.go
Original file line number Diff line number Diff line change
@@ -1,116 +1,149 @@
package main

import (
"EXP/pkg/shellout"
"context"
"encoding/json"
"fmt"
"github.com/codeskyblue/go-sh"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"os"
"strings"
"sync"
"time"
)

type CobraCallbackE func(cmd *cobra.Command, args []string) error
var bgctx = context.Background()

var (
cmd = &cobra.Command{
Use: "goprompt",
PersistentPreRunE: bindEnvironmentFlags("GOPROMPT"),
Use: "goprompt",
}
cmdQuery = &cobra.Command{
Use: "query",
RunE: cmdQueryExec,
}

cmdQueryStatus = cmd.PersistentFlags().Int(
flgCmdStatus = cmd.PersistentFlags().Int(
"cmd-status", 0,
"cmd status of previous command",
)
cmdQueryPreexecTS = cmd.PersistentFlags().String(
"preexec-ts", "0",
flgPreexecTS = cmd.PersistentFlags().Int(
"preexec-ts", 0,
"pre-execution timestamp to gauge how log execution took",
)
)

func init() {
cmd.AddCommand(cmdQuery)

cmd.RunE = cmdExec
}

func bindEnvironmentFlags(prefix string) CobraCallbackE {
return func(cmd *cobra.Command, args []string) (outErr error) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if !f.Changed {
envKey := prefix + "_" + strings.ReplaceAll(f.Name, "-", "_")
if value, ok := os.LookupEnv(strings.ToUpper(envKey)); ok {
if err := cmd.Flags().Set(f.Name, value); err != nil {
outErr = err
return
}
}
}
})
return nil
}
}
// PROMPT PARTS:
// (exit-status: if > 0)
// (parent-process)
// (hostname: if remote connection)
// (current-dir-path)
// (vsc-information)
// (timestamp)

func cmdQueryExec(cmd *cobra.Command, args []string) error {
if *cmdQueryStatus != 0 {
printPart("st", fmt.Sprintf("%#v", *cmdQueryStatus))
func cmdExec(cmd *cobra.Command, args []string) error {
if *flgCmdStatus != 0 {
printPart("st", fmt.Sprintf("%#v", *flgCmdStatus))
}

wg := new(sync.WaitGroup)
wg := new(WaitGroupDispatcher)
defer wg.Wait()

wg.Add(1)
go func() {
defer wg.Done()
wg.Dispatch(func() {
homeDir := os.Getenv("HOME")

if wd, err := os.Getwd(); err == nil {
printPart("wd", trimPathLast(wd, 2))
wdh := strings.Replace(wd, homeDir, "~", 1)

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

wg.Add(1)
go func() {
defer wg.Done()
nowTS := time.Now()
printPart("ts", nowTS.Format("15:04:05 01/02/06"))

if branch, err := sh.Command("git", "branch", "--show-current").Output(); err == nil {
printPart("git_br", trim(string(branch)))
if *flgPreexecTS != 0 {
cmdTS := time.Unix(int64(*flgPreexecTS), 0)

diff := nowTS.Sub(cmdTS)
printPart("ds", diff.Round(time.Second))
}
})

//wg.Dispatch(func() {
// out, err := stringExec("git", "config", "--list")
// printPart("debug_o", js(out))
// if err != nil {
// printPart("debug_e", js(err.Error()))
// }
//})

wg.Dispatch(func() {
cwg := new(WaitGroupDispatcher)
defer cwg.Wait()

if _, err := stringExec("git", "rev-parse", "--show-toplevel"); err == nil {
printPart("vcs", "git")
} else {
return
}
}()

wg.Add(1)
go func() {
defer wg.Done()
cwg.Dispatch(func() {
if branch, err := stringExec("git", "branch", "--show-current"); err == nil {
printPart("vcs_br", trim(string(branch)))
}
})

if status, err := sh.Command("git", "status", "--porcelain").Output(); err == nil {
if len(status) > 0 {
printPart("git_st", "dirty")
} else {
printPart("git_st", "clean")
cwg.Dispatch(func() {
if status, err := stringExec("git", "status", "--porcelain"); err == nil {
if len(status) > 0 {
printPart("vcs_dirty", 1)
printPart("vcs_dirty_st", js(status))
} else {
printPart("vsc_dirty", 0)
}
}
})
})

wg.Dispatch(func() {
cwg := new(WaitGroupDispatcher)
defer cwg.Wait()

var stgPatchTop string
var err error

if stgPatchTop, err = stringExec("stg", "top"); err == nil {
printPart("stg", "1")
printPart("stg_top", stgPatchTop)
} else {
return
}
}()

wg.Wait()
return nil
}
cwg.Dispatch(func() {
gitSHA, _ := stringExec("stg", "id")
stgSHA, _ := stringExec("stg", "id", stgPatchTop)

func trimPathLast(s string, n int) string {
return s
}
if gitSHA != stgSHA {
printPart("stg_dirty", 1)
} else {
printPart("stg_dirty", 0)
}
})

func intMax(a, b int) int {
if a > b {
return a
} else {
return b
}
}
cwg.Dispatch(func() {
if stgPatchLen, err := stringExec("stg", "series", "-c"); err == nil {
printPart("stg_qlen", stgPatchLen)
}
})

func trim(s string) string {
return strings.Trim(s, "\n\t ")
cwg.Dispatch(func() {
if stgPatchPos, err := stringExec("stg", "series", "-cA"); err == nil {
printPart("stg_qpos", stgPatchPos)
}
})
})

return nil
}

func printPart(name string, value interface{}) {
Expand All @@ -120,17 +153,79 @@ func printPart(name string, value interface{}) {
fmt.Printf("%s\t%v\n", name, value)
}

// PROMPT PARTS:
// (exit-status: if > 0)
// (parent-process)
// (hostname: if remote connection)
// (current-dir-path)
// (vsc-information)
// (timestamp)
//------------------------------------------------------------------------------

func main() {
err := cmd.ExecuteContext(context.Background())
if err != nil {
panic(err)
}
}

//------------------------------------------------------------------------------

type WaitGroupDispatcher struct {
wg sync.WaitGroup
}

func (d *WaitGroupDispatcher) Dispatch(fn func()) {
d.wg.Add(1)
go func() {
defer d.wg.Done()
fn()
}()
}

func (d *WaitGroupDispatcher) Wait() {
d.wg.Wait()
}

func stringExec(path string, args ...string) (string, error) {
out, err := shellout.New(bgctx,
shellout.Args(path, args...),
shellout.EnvSet(map[string]string{
"GIT_OPTIONAL_LOCKS": "0",
}),
).RunString()
return trim(out), err
}

func js(v interface{}) string {
b, _ := json.Marshal(v)
return string(b)
}

func trimPath(s string) string {
var out []string

parts := strings.Split(s, "/")
for i, part := range parts {
if i == len(parts)-1 {
out = append(out, part)
} else {
out = append(out, part[0:intMin(len(part), 1)])
}
}

return strings.Join(out, "/")
}

func intMax(a, b int) int {
if a > b {
return a
} else {
return b
}
}

func intMin(a, b int) int {
if a < b {
return a
} else {
return b
}
}

func trim(s string) string {
return strings.Trim(s, "\n\t ")
}
Loading

0 comments on commit 266f995

Please sign in to comment.