cfg.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package cfg
  2. import (
  3. "os"
  4. "fmt"
  5. "io/ioutil"
  6. "github.com/goccy/go-json"
  7. "github.com/akshaybharambe14/go-jsonc"
  8. // "github.com/tidwall/sjson"
  9. )
  10. var err error
  11. type Config struct {
  12. CurrentTheme string
  13. IconSize int
  14. Layer string
  15. Position string
  16. Blur string
  17. Spacing int
  18. Margin int
  19. }
  20. func GetDefaultConfig() Config {
  21. config := Config{}
  22. config.CurrentTheme = "lotos"
  23. config.IconSize = 21
  24. config.Layer = "auto"
  25. config.Position = "bottom"
  26. config.Blur = "on"
  27. config.Spacing = 8
  28. config.Margin = 8
  29. return config
  30. }
  31. func ConnectConfig(jsoncFile string, isTheme bool) Config {
  32. // Read jsonc
  33. file, err := os.Open(jsoncFile)
  34. if err != nil {
  35. fmt.Println("Config file not found!\n", err, "\nLoad default config")
  36. return GetDefaultConfig()
  37. }
  38. defer file.Close()
  39. decoder := jsonc.NewDecoder(file)
  40. res, err := ioutil.ReadAll(decoder)
  41. fmt.Println(json.Valid(res), jsoncFile)
  42. config := Config{}
  43. if err = json.Unmarshal(res, &config); err != nil {
  44. fmt.Println("Config is incorrect!\n", err, "\nLoad default config")
  45. return GetDefaultConfig()
  46. }
  47. // Set default values ​​if not specified
  48. if config.CurrentTheme == "" && !isTheme{
  49. config.CurrentTheme = GetDefaultConfig().CurrentTheme
  50. fmt.Println("The theme is not set, the default theme is currently used - \"lotos\"")
  51. }
  52. if config.Layer == "" {
  53. config.Layer = GetDefaultConfig().CurrentTheme
  54. }
  55. isCorrect := config.Position == "left" || config.Position == "right"
  56. isCorrect2 := config.Position == "top" || config.Position == "bottom"
  57. isCorrect3 := isCorrect || isCorrect2
  58. if config.Position == "" || !isCorrect3 {
  59. if !isTheme {
  60. fmt.Println("Position incorrect or empty\nDefault position set")
  61. }
  62. config.Position = GetDefaultConfig().Position
  63. }
  64. if config.Spacing == 0 {
  65. config.Spacing = GetDefaultConfig().Spacing
  66. }
  67. if config.IconSize == 0 {
  68. config.IconSize = GetDefaultConfig().IconSize
  69. }
  70. if config.Blur == "" {
  71. config.Blur = GetDefaultConfig().Blur
  72. }
  73. return config
  74. }
  75. type ItemList struct {
  76. Pinned []string
  77. }
  78. func ReadItemList(jsonFile string) []string {
  79. file, _ := os.Open(jsonFile)
  80. defer file.Close()
  81. decoder := json.NewDecoder(file)
  82. itemList := ItemList{}
  83. err := decoder.Decode(&itemList)
  84. if err != nil {
  85. fmt.Println("error: ", err)
  86. }
  87. return itemList.Pinned
  88. }