From cabc0660fd8a900c34f48eb96b98ec2eb84ec423 Mon Sep 17 00:00:00 2001 From: makeworld Date: Tue, 28 Dec 2021 19:43:24 -0500 Subject: [PATCH] New theme key: `include` Fixes #154 --- CHANGELOG.md | 1 + config/config.go | 53 +++++++++++++++++------- config/default.go | 9 ++++ contrib/themes/amfora.toml | 2 +- contrib/themes/atelier-forest-light.toml | 2 +- contrib/themes/atelier-forest.toml | 2 +- contrib/themes/dracula-variant.toml | 2 +- contrib/themes/dracula.toml | 2 +- contrib/themes/greyscale-light.toml | 2 +- contrib/themes/gruvbox.toml | 2 +- contrib/themes/gruvbox_dark.toml | 2 +- contrib/themes/iceberg.toml | 2 +- contrib/themes/nord.toml | 2 +- contrib/themes/one_dark.toml | 2 +- contrib/themes/slimey.toml | 2 +- contrib/themes/solarized_dark.toml | 2 +- contrib/themes/solarized_light.toml | 2 +- contrib/themes/tokyo-night.toml | 2 +- default-config.toml | 9 ++++ 19 files changed, 72 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f327fde..4d53d7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `header` config option in `[subscriptions]` to allow disabling the header text on the subscriptions page (#191) - Selected link and scroll position stays for non-cached pages (#122) - Keybinding to open URL with URL handler instead of configured proxy (#143) +- `include` theme key to import themes from an external file (#154) ### Changed - Center text automatically, removing `left_margin` from the config (#233) diff --git a/config/config.go b/config/config.go index f30f6ae..2c20fc1 100644 --- a/config/config.go +++ b/config/config.go @@ -347,29 +347,52 @@ func Init() error { cache.SetMaxPages(viper.GetInt("cache.max_pages")) cache.SetTimeout(viper.GetInt("cache.timeout")) + setColor := func(k string, colorStr string) error { + colorStr = strings.ToLower(colorStr) + var color tcell.Color + if colorStr == "default" { + if strings.HasSuffix(k, "bg") { + color = tcell.ColorDefault + } else { + return fmt.Errorf(`"default" is only valid for a background color (color ending in "bg"), not "%s"`, k) + } + } else { + color = tcell.GetColor(colorStr) + if color == tcell.ColorDefault { + return fmt.Errorf(`invalid color format for "%s": %s`, k, colorStr) + } + } + SetColor(k, color) + return nil + } + // Setup theme configTheme := viper.Sub("theme") if configTheme != nil { + // Include key comes first + if incPath := configTheme.GetString("include"); incPath != "" { + incViper := viper.New() + incViper.SetConfigFile(incPath) + incViper.SetConfigType("toml") + err = incViper.ReadInConfig() + if err != nil { + return err + } + + for k2, v2 := range incViper.AllSettings() { + colorStr, ok := v2.(string) + if !ok { + return fmt.Errorf(`include: value for "%s" is not a string: %v`, k2, v2) + } + setColor(k2, colorStr) + } + } for k, v := range configTheme.AllSettings() { colorStr, ok := v.(string) if !ok { return fmt.Errorf(`value for "%s" is not a string: %v`, k, v) } - colorStr = strings.ToLower(colorStr) - var color tcell.Color - if colorStr == "default" { - if strings.HasSuffix(k, "bg") { - color = tcell.ColorDefault - } else { - return fmt.Errorf(`"default" is only valid for a background color (color ending in "bg"), not "%s"`, k) - } - } else { - color = tcell.GetColor(colorStr) - if color == tcell.ColorDefault { - return fmt.Errorf(`invalid color format for "%s": %s`, k, colorStr) - } - } - SetColor(k, color) + setColor(k, colorStr) } } if viper.GetBool("a-general.color") { diff --git a/config/default.go b/config/default.go index 3b81dd8..ab560f4 100644 --- a/config/default.go +++ b/config/default.go @@ -376,6 +376,15 @@ header = true # bottombar_bg # scrollbar: The scrollbar that appears on the right for long pages +# You can also set an 'include' key to process another TOML file that contains theme keys. +# Example: +# include = "my/path/to/special-theme.toml" +# +# Any other theme keys will override this external file. +# You can use this special key to switch between themes easily. +# Download other themes here: https://github.com/makeworld-the-better-one/amfora/tree/master/contrib/themes + + # hdg_1 # hdg_2 # hdg_3 diff --git a/contrib/themes/amfora.toml b/contrib/themes/amfora.toml index 0cff44a..792a0e2 100644 --- a/contrib/themes/amfora.toml +++ b/contrib/themes/amfora.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # Only the 256 xterm colors are used, so truecolor support is not needed diff --git a/contrib/themes/atelier-forest-light.toml b/contrib/themes/atelier-forest-light.toml index 1dd29bc..25e714d 100644 --- a/contrib/themes/atelier-forest-light.toml +++ b/contrib/themes/atelier-forest-light.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # atelier forest light diff --git a/contrib/themes/atelier-forest.toml b/contrib/themes/atelier-forest.toml index 36ed036..ac811bf 100644 --- a/contrib/themes/atelier-forest.toml +++ b/contrib/themes/atelier-forest.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # atelier forest diff --git a/contrib/themes/dracula-variant.toml b/contrib/themes/dracula-variant.toml index c0a0c4f..b56dcca 100644 --- a/contrib/themes/dracula-variant.toml +++ b/contrib/themes/dracula-variant.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] bg = "#282a36" tab_num = "#bd93f9" tab_divider = "#f8f8f2" diff --git a/contrib/themes/dracula.toml b/contrib/themes/dracula.toml index d2073be..8f1692e 100644 --- a/contrib/themes/dracula.toml +++ b/contrib/themes/dracula.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # This section is for changing the COLORS used in Amfora. # These colors only apply if 'color' is enabled above. # Colors can be set using a W3C color name, or a hex value such as "#ffffff". diff --git a/contrib/themes/greyscale-light.toml b/contrib/themes/greyscale-light.toml index 19a3710..b245589 100644 --- a/contrib/themes/greyscale-light.toml +++ b/contrib/themes/greyscale-light.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] bg = "#ffffff" tab_num = "#000000" tab_divider = "#000000" diff --git a/contrib/themes/gruvbox.toml b/contrib/themes/gruvbox.toml index 9f150d2..d4b95fd 100644 --- a/contrib/themes/gruvbox.toml +++ b/contrib/themes/gruvbox.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # This section is for changing the COLORS used in Amfora. # These colors only apply if 'color' is enabled above. # Colors can be set using a W3C color name, or a hex value such as "#ffffff". diff --git a/contrib/themes/gruvbox_dark.toml b/contrib/themes/gruvbox_dark.toml index e2052ad..d6ba8a3 100644 --- a/contrib/themes/gruvbox_dark.toml +++ b/contrib/themes/gruvbox_dark.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # Gruvbox Dark theme diff --git a/contrib/themes/iceberg.toml b/contrib/themes/iceberg.toml index 4baa260..2db2ac8 100644 --- a/contrib/themes/iceberg.toml +++ b/contrib/themes/iceberg.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # This section is for changing the COLORS used in Amfora. # These colors only apply if 'color' is enabled above. # Colors can be set using a W3C color name, or a hex value such as "#ffffff". diff --git a/contrib/themes/nord.toml b/contrib/themes/nord.toml index 0516b62..8b0182e 100644 --- a/contrib/themes/nord.toml +++ b/contrib/themes/nord.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # This section is for changing the COLORS used in Amfora. # These colors only apply if 'color' is enabled above. # Colors can be set using a W3C color name, or a hex value such as "#ffffff". diff --git a/contrib/themes/one_dark.toml b/contrib/themes/one_dark.toml index 227f641..9b99d43 100644 --- a/contrib/themes/one_dark.toml +++ b/contrib/themes/one_dark.toml @@ -1,7 +1,7 @@ # Atom One Dark theme ported to Amfora # by Serge Tymoshenko -[theme] +#[theme] # This section is for changing the COLORS used in Amfora. # These colors only apply if 'color' is enabled above. # Colors can be set using a W3C color name, or a hex value such as "#ffffff". diff --git a/contrib/themes/slimey.toml b/contrib/themes/slimey.toml index cde9602..4f26d1e 100644 --- a/contrib/themes/slimey.toml +++ b/contrib/themes/slimey.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # This section is for changing the COLORS used in Amfora. # These colors only apply if 'color' is enabled above. # Colors can be set using a W3C color name, or a hex value such as "#ffffff". diff --git a/contrib/themes/solarized_dark.toml b/contrib/themes/solarized_dark.toml index 68d20fc..8e26e5e 100644 --- a/contrib/themes/solarized_dark.toml +++ b/contrib/themes/solarized_dark.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # This section is for changing the COLORS used in Amfora. # These colors only apply if 'color' is enabled above. # Colors can be set using a W3C color name, or a hex value such as "#ffffff". diff --git a/contrib/themes/solarized_light.toml b/contrib/themes/solarized_light.toml index 12dfbb6..da879e3 100644 --- a/contrib/themes/solarized_light.toml +++ b/contrib/themes/solarized_light.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # This section is for changing the COLORS used in Amfora. # These colors only apply if 'color' is enabled above. # Colors can be set using a W3C color name, or a hex value such as "#ffffff". diff --git a/contrib/themes/tokyo-night.toml b/contrib/themes/tokyo-night.toml index 528b826..b2b8340 100644 --- a/contrib/themes/tokyo-night.toml +++ b/contrib/themes/tokyo-night.toml @@ -1,4 +1,4 @@ -[theme] +#[theme] # Tokyo Night diff --git a/default-config.toml b/default-config.toml index 3cbb7ba..64a7dce 100644 --- a/default-config.toml +++ b/default-config.toml @@ -373,6 +373,15 @@ header = true # bottombar_bg # scrollbar: The scrollbar that appears on the right for long pages +# You can also set an 'include' key to process another TOML file that contains theme keys. +# Example: +# include = "my/path/to/special-theme.toml" +# +# Any other theme keys will override this external file. +# You can use this special key to switch between themes easily. +# Download other themes here: https://github.com/makeworld-the-better-one/amfora/tree/master/contrib/themes + + # hdg_1 # hdg_2 # hdg_3