v/GTT
1
0
mirror of https://github.com/eeeXun/GTT.git synced 2025-05-19 01:00:20 -07:00

feat: allow user to import their own themes

This commit is contained in:
eeeXun 2023-04-22 17:35:28 +08:00
parent fca1647d60
commit 035a92130a
3 changed files with 39 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/eeeXun/gtt/internal/style"
"github.com/eeeXun/gtt/internal/translate" "github.com/eeeXun/gtt/internal/translate"
config "github.com/spf13/viper" config "github.com/spf13/viper"
) )
@ -12,6 +13,7 @@ import (
func configInit() { func configInit() {
var ( var (
defaultConfigPath string defaultConfigPath string
themeConfig = config.New()
defaultConfig = map[string]interface{}{ defaultConfig = map[string]interface{}{
"hide_below": false, "hide_below": false,
"transparent": false, "transparent": false,
@ -34,13 +36,17 @@ func configInit() {
config.SetConfigName("gtt") config.SetConfigName("gtt")
config.SetConfigType("yaml") config.SetConfigType("yaml")
themeConfig.SetConfigName("theme")
themeConfig.SetConfigType("yaml")
if len(os.Getenv("XDG_CONFIG_HOME")) > 0 { if len(os.Getenv("XDG_CONFIG_HOME")) > 0 {
defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt" defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt"
config.AddConfigPath(defaultConfigPath) config.AddConfigPath(defaultConfigPath)
themeConfig.AddConfigPath(defaultConfigPath)
} else { } else {
defaultConfigPath = os.Getenv("HOME") + "/.config/gtt" defaultConfigPath = os.Getenv("HOME") + "/.config/gtt"
} }
config.AddConfigPath("$HOME/.config/gtt") config.AddConfigPath("$HOME/.config/gtt")
themeConfig.AddConfigPath("$HOME/.config/gtt")
// Create config file if not exists // Create config file if not exists
// Otherwise check if config value is missing // Otherwise check if config value is missing
@ -64,6 +70,19 @@ func configInit() {
config.WriteConfig() 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 // setup
for _, name := range translate.AllTranslator { for _, name := range translate.AllTranslator {

12
example/theme.yaml Normal file
View File

@ -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

View File

@ -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)
}
}