pest.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/go-git/go-git/v5"
  5. "github.com/gookit/color"
  6. "github.com/pelletier/go-toml/v2"
  7. "os"
  8. )
  9. // go-pest -- A reimplementation of cl-pest in Go, for the sake of learning a bit of Go
  10. // See: https://git.spwbk.site/swatson/cl-pest/src/master/pest.lisp
  11. type PestConfig struct {
  12. Git GitConfig `toml:"git"`
  13. Prompt PromptConfig `toml:"prompt"`
  14. }
  15. type GitConfig struct {
  16. DisplayHead bool `toml:"display_head"`
  17. DisplayBranch bool `toml:"display_branch"`
  18. GitPrefix string `toml:"git_prefix"`
  19. Colors Colors `toml:"colors"`
  20. }
  21. type PromptConfig struct {
  22. DisplayUser bool `toml:"display_user"`
  23. UserSuffix string `toml:"user_suffix"`
  24. DisplayHostname bool `toml:"display_hostname"`
  25. HostnameSuffix string `toml:"hostname_suffix"`
  26. DisplayPwd bool `toml:"display_pwd"`
  27. PwdSuffix string `toml:"pwd_suffix"`
  28. PromptChar string `toml:"prompt_char"`
  29. Colors Colors `toml:"colors"`
  30. }
  31. type Colors struct {
  32. Fg []int `toml:"fg"`
  33. Bg []int `toml:"bg"`
  34. }
  35. const tomlData = `
  36. [git]
  37. display_head = false
  38. display_branch = false
  39. git_prefix = ""
  40. [git.colors]
  41. fg = [0, 120, 50]
  42. bg = [0, 0, 0]
  43. [prompt]
  44. display_user = false
  45. user_suffix = ""
  46. display_hostname = false
  47. hostname_suffix = ""
  48. display_pwd = true
  49. pwd_suffix = ""
  50. prompt_char = " λ "
  51. [prompt.colors]
  52. fg = [255, 255, 255]
  53. bg = [0, 0, 0]
  54. `
  55. func parseTomlFromString(tomlString string) (PestConfig, error) {
  56. var config PestConfig
  57. err := toml.Unmarshal([]byte(tomlString), &config)
  58. return config, err
  59. }
  60. func parseTomlFromFile(filePath string) (PestConfig, error) {
  61. var cfg PestConfig
  62. fileContent, err := os.ReadFile(filePath)
  63. switch err {
  64. case nil:
  65. {
  66. err = toml.Unmarshal(fileContent, &cfg)
  67. return cfg, err
  68. }
  69. default:
  70. {
  71. // We had some kind of error, use the builtin config
  72. cfg, err = parseTomlFromString(tomlData)
  73. return cfg, err
  74. }
  75. }
  76. }
  77. func gitGetHead(filePath string) string {
  78. gitObj, err := git.PlainOpen(filePath)
  79. if err != nil {
  80. panic(err)
  81. }
  82. headRef, err := gitObj.Head()
  83. if err != nil {
  84. panic(err)
  85. }
  86. sha := headRef.Hash().String()
  87. return sha
  88. }
  89. type RGBColorInput struct {
  90. r uint8
  91. g uint8
  92. b uint8
  93. }
  94. // make_style -- given a set of RGBColorInputs return a color.RGBColor pair
  95. func make_style(fg, bg RGBColorInput) color.Style {
  96. fg_s := color.RGB(fg.r, fg.g, fg.b)
  97. bg_s := color.RGB(bg.r, bg.g, bg.b, true)
  98. style := color.New(fg_s.Color(), bg_s.Color())
  99. return style
  100. }
  101. func main() {
  102. toml, err := parseTomlFromFile("./pest.cfg")
  103. if err != nil {
  104. fmt.Println("Couldn't parse TOML const!")
  105. }
  106. prompt_color := make_style(
  107. RGBColorInput{
  108. uint8(toml.Prompt.Colors.Fg[0]),
  109. uint8(toml.Prompt.Colors.Fg[1]),
  110. uint8(toml.Prompt.Colors.Fg[2]), // This will of course panic if this number is > 255
  111. },
  112. RGBColorInput{
  113. uint8(toml.Prompt.Colors.Bg[0]),
  114. uint8(toml.Prompt.Colors.Bg[1]),
  115. uint8(toml.Prompt.Colors.Bg[2]),
  116. })
  117. prompt_color.Println("Foobar")
  118. prompt_color.Println(gitGetHead("."))
  119. }