From 035a92130a40ca6b6fe46fd0dcce080a7a1291c0 Mon Sep 17 00:00:00 2001 From: eeeXun Date: Sat, 22 Apr 2023 17:35:28 +0800 Subject: [PATCH] feat: allow user to import their own themes --- config.go | 19 +++++++++++++++++++ example/theme.yaml | 12 ++++++++++++ internal/style/color.go | 8 ++++++++ 3 files changed, 39 insertions(+) create mode 100644 example/theme.yaml diff --git a/config.go b/config.go index a284137..fc9349c 100644 --- a/config.go +++ b/config.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/eeeXun/gtt/internal/style" "github.com/eeeXun/gtt/internal/translate" config "github.com/spf13/viper" ) @@ -12,6 +13,7 @@ import ( func configInit() { var ( defaultConfigPath string + themeConfig = config.New() defaultConfig = map[string]interface{}{ "hide_below": false, "transparent": false, @@ -34,13 +36,17 @@ func configInit() { config.SetConfigName("gtt") config.SetConfigType("yaml") + themeConfig.SetConfigName("theme") + themeConfig.SetConfigType("yaml") if len(os.Getenv("XDG_CONFIG_HOME")) > 0 { defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt" config.AddConfigPath(defaultConfigPath) + themeConfig.AddConfigPath(defaultConfigPath) } else { defaultConfigPath = os.Getenv("HOME") + "/.config/gtt" } config.AddConfigPath("$HOME/.config/gtt") + themeConfig.AddConfigPath("$HOME/.config/gtt") // Create config file if not exists // Otherwise check if config value is missing @@ -64,6 +70,19 @@ func configInit() { config.WriteConfig() } } + // import theme if file exists + if err := themeConfig.ReadInConfig(); err == nil { + var ( + palate = make(map[string]int32) + colors = []string{"bg", "fg", "gray", "red", "green", "yellow", "blue", "purple", "cyan", "orange"} + ) + for name := range themeConfig.AllSettings() { + for _, color := range colors { + palate[color] = themeConfig.GetInt32(fmt.Sprintf("%s.%s", name, color)) + } + style.NewTheme(name, palate) + } + } // setup for _, name := range translate.AllTranslator { diff --git a/example/theme.yaml b/example/theme.yaml new file mode 100644 index 0000000..08510c6 --- /dev/null +++ b/example/theme.yaml @@ -0,0 +1,12 @@ +# This file should located under $XDG_CONFIG_HOME/gtt/theme.yaml or $HOME/.config/gtt/theme.yaml +tokyonight: + bg: 0x1a1b26 + fg: 0xc0caf5 + gray: 0x414868 + red: 0xf7768e + green: 0x9ece6a + yellow: 0xe0af68 + blue: 0x7aa2f7 + purple: 0xbb9af7 + cyan: 0x7dcfff + orange: 0xff9e64 diff --git a/internal/style/color.go b/internal/style/color.go index a90eca4..b87c942 100644 --- a/internal/style/color.go +++ b/internal/style/color.go @@ -34,3 +34,11 @@ var ( }, } ) + +func NewTheme(name string, palette map[string]int32) { + AllTheme = append(AllTheme, name) + themes[name] = map[string]tcell.Color{} + for color, rgb := range palette { + themes[name][color] = tcell.NewHexColor(rgb) + } +}