소스 검색

Working PoC

Simon Watson 11 달 전
부모
커밋
81a3956e83
2개의 변경된 파일115개의 추가작업 그리고 15개의 파일을 삭제
  1. 20 0
      pest.cfg
  2. 95 15
      pest.go

+ 20 - 0
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]

+ 95 - 15
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)
 
 }