commit 81a3956e83a44a1ef7d4590c9a5fa59c218c3c7f
Author: Simon Watson <spesk@pm.me>
Date: Tue Dec 5 16:24:19 2023 -0500
Working PoC
diff --git a/pest.cfg b/pest.cfg
new file mode 100644
index 0000000..9c3c215
--- /dev/null
+++ b/pest.cfg
@@ -0,0 +1,20 @@
+[git]
+display_head = true
+display_branch = true
+git_prefix = ""
+[git.colors]
+fg = [0, 120, 50]
+bg = [255, 255, 255]
+
+[prompt]
+display_user = false
+user_suffix = ""
+display_hostname = false
+hostname_suffix = ""
+display_pwd = true
+pwd_suffix = ""
+prompt_char = " λ "
+#prompt_char = " >>> "
+[prompt.colors]
+fg = [0, 0, 0]
+bg = [255, 255, 255]
\ No newline at end of file
diff --git a/pest.go b/pest.go
index 0a3a98d..30f990e 100644
--- a/pest.go
+++ b/pest.go
@@ -6,6 +6,8 @@ import (
"github.com/gookit/color"
"github.com/pelletier/go-toml/v2"
"os"
+ "os/user"
+ "strings"
)
// go-pest -- A reimplementation of cl-pest in Go, for the sake of learning a bit of Go
@@ -85,7 +87,7 @@ func parseTomlFromFile(filePath string) (PestConfig, error) {
}
}
-func gitGetHead(filePath string) string {
+func gitGetHead(filePath string) (string, string) {
gitObj, err := git.PlainOpen(filePath)
if err != nil {
panic(err)
@@ -96,8 +98,33 @@ func gitGetHead(filePath string) string {
panic(err)
}
- sha := headRef.Hash().String()
- return sha
+ sha := headRef.Hash().String()[:6]
+ branch := headRef.Name().Short()
+ return sha, branch
+}
+
+func getHostname() string {
+ hostname, err := os.Hostname()
+ if err != nil {
+ panic(err)
+ }
+ return hostname
+}
+
+func getUser() string {
+ username, err := user.Current()
+ if err != nil {
+ panic(err)
+ }
+ return username.Username
+}
+
+func getPwd() string {
+ pwd, err := os.Getwd()
+ if err != nil {
+ panic(err)
+ }
+ return pwd
}
type RGBColorInput struct {
@@ -114,26 +141,79 @@ func make_style(fg, bg RGBColorInput) color.Style {
return style
}
-func main() {
+func assemble_prompt(config *PestConfig) {
+ var prompt_builder strings.Builder
+ var git_prompt_builder strings.Builder
+
+ if config.Git.DisplayHead || config.Git.DisplayBranch {
+ sha, branch := gitGetHead(getPwd())
+ if config.Git.DisplayHead && config.Git.DisplayBranch {
+ git_prompt := sha + "|" + branch
+ git_prompt_builder.WriteString(config.Git.GitPrefix)
+ git_prompt_builder.WriteString(git_prompt)
+ } else if config.Git.DisplayHead {
+ git_prompt_builder.WriteString(config.Git.GitPrefix)
+ git_prompt_builder.WriteString(sha)
+ } else if config.Git.DisplayBranch {
+ git_prompt_builder.WriteString(config.Git.GitPrefix)
+ git_prompt_builder.WriteString(branch)
+ }
+ git_prompt_builder.WriteString(" ")
+ }
- toml, err := parseTomlFromFile("./pest.cfg")
- if err != nil {
- fmt.Println("Couldn't parse TOML const!")
+ git_prompt_color := make_style(
+ RGBColorInput{
+ uint8(config.Git.Colors.Fg[0]),
+ uint8(config.Git.Colors.Fg[1]),
+ uint8(config.Git.Colors.Fg[2]),
+ },
+ RGBColorInput{
+ uint8(config.Git.Colors.Bg[0]),
+ uint8(config.Git.Colors.Bg[1]),
+ uint8(config.Git.Colors.Bg[2]),
+ })
+
+ if config.Prompt.DisplayUser {
+ prompt_builder.WriteString(getUser())
+ prompt_builder.WriteString(config.Prompt.UserSuffix)
}
+ if config.Prompt.DisplayHostname {
+ prompt_builder.WriteString(getHostname())
+ prompt_builder.WriteString(config.Prompt.HostnameSuffix)
+ }
+
+ if config.Prompt.DisplayPwd {
+ prompt_builder.WriteString(getPwd())
+ prompt_builder.WriteString(config.Prompt.PwdSuffix)
+ }
+
+ prompt_builder.WriteString(config.Prompt.PromptChar)
+
prompt_color := make_style(
RGBColorInput{
- uint8(toml.Prompt.Colors.Fg[0]),
- uint8(toml.Prompt.Colors.Fg[1]),
- uint8(toml.Prompt.Colors.Fg[2]), // This will of course panic if this number is > 255
+ uint8(config.Prompt.Colors.Fg[0]),
+ uint8(config.Prompt.Colors.Fg[1]),
+ uint8(config.Prompt.Colors.Fg[2]), // This will of course panic if this number is > 255
},
RGBColorInput{
- uint8(toml.Prompt.Colors.Bg[0]),
- uint8(toml.Prompt.Colors.Bg[1]),
- uint8(toml.Prompt.Colors.Bg[2]),
+ uint8(config.Prompt.Colors.Bg[0]),
+ uint8(config.Prompt.Colors.Bg[1]),
+ uint8(config.Prompt.Colors.Bg[2]),
})
- prompt_color.Println("Foobar")
- prompt_color.Println(gitGetHead("."))
+ git_prompt_color.Printf(git_prompt_builder.String())
+ prompt_color.Printf(prompt_builder.String())
+
+}
+
+func main() {
+
+ toml, err := parseTomlFromFile("./pest.cfg")
+ if err != nil {
+ fmt.Println("Couldn't parse TOML const!")
+ }
+
+ assemble_prompt(&toml)
}