From 4aa96ec3de9a8ecdf192d51e04c069f1eefa53ae Mon Sep 17 00:00:00 2001 From: Xun <58657914+eeeXun@users.noreply.github.com> Date: Fri, 12 May 2023 21:57:40 +0800 Subject: [PATCH] feat: allow user to import their own themes (#17) * feat: allow user to import their own themes * refactor: use make to create map * add catppuccin example * fix: theme importing should before reading config file * docs: add create theme in README * docs: style --- README.md | 14 ++++++++++++++ config.go | 18 ++++++++++++++++++ example/theme.yaml | 30 ++++++++++++++++++++++++++++++ internal/style/color.go | 8 ++++++++ 4 files changed, 70 insertions(+) create mode 100644 example/theme.yaml diff --git a/README.md b/README.md index ad4b250..5b7792a 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,20 @@ go install github.com/eeeXun/gtt@latest docker run -it eeexun/gtt ``` +## Create a theme + +You can create a theme with theme name. And you must provide the color of `bg`, `fg`, `gray`, `red`, `green`, `yellow`, `blue`, `purple`, `cyan`, `orange`. + +And note that: + +- `bg` is for background color +- `fg` is for foreground color +- `gray` is for selected color +- `yellow` is for label color +- `orange` is for KeyMap menu color + +See the example in [theme.yaml](example/theme.yaml) file. This file should located under `$XDG_CONFIG_HOME/gtt/theme.yaml` or `$HOME/.config/gtt/theme.yaml` + ## Language in argument You can pass `-src` and `-dst` in argument to set source and destination language. diff --git a/config.go b/config.go index 7cddd07..7dba1fe 100644 --- a/config.go +++ b/config.go @@ -13,6 +13,7 @@ import ( func configInit() { var ( defaultConfigPath string + themeConfig = config.New() defaultConfig = map[string]interface{}{ "hide_below": false, "transparent": false, @@ -37,14 +38,31 @@ 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") + // 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) + } + } // Create config file if it does not exist // Otherwise check if config value is missing if err := config.ReadInConfig(); err != nil { diff --git a/example/theme.yaml b/example/theme.yaml new file mode 100644 index 0000000..60c5fe5 --- /dev/null +++ b/example/theme.yaml @@ -0,0 +1,30 @@ +# This file should located under $XDG_CONFIG_HOME/gtt/theme.yaml or $HOME/.config/gtt/theme.yaml +# You have to provide theme name. e.g. tokyonight +# And colors: bg, fg, gray, red, green, yellow, blue, purple, cyan, orange +# bg is for background color +# fg is for foreground color +# gray is for selected color +# yellow is for label color +# orange is for KeyMap menu color +tokyonight: + bg: 0x1a1b26 + fg: 0xc0caf5 + gray: 0x414868 + red: 0xf7768e + green: 0x9ece6a + yellow: 0xe0af68 + blue: 0x7aa2f7 + purple: 0xbb9af7 + cyan: 0x7dcfff + orange: 0xff9e64 +catppuccin: + bg: 0x24273A + fg: 0xCAD3F5 + gray: 0x5B6078 + red: 0xED8796 + green: 0xA6DA95 + yellow: 0xEED49F + blue: 0x8AADF4 + purple: 0xF5BDE6 + cyan: 0x8BD5CA + orange: 0xF5A97F diff --git a/internal/style/color.go b/internal/style/color.go index 29069c4..2ed2c60 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] = make(map[string]tcell.Color) + for color, rgb := range palette { + themes[name][color] = tcell.NewHexColor(rgb) + } +}