Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom themes #40

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ theme:
kc: "#009900 underline"
```

You can change non-syntax colors using the `styleOverrides` key:
```yaml
theme:
styleOverrides:
primary: "#c4b28a"
secondary: "#8992a7"
error: "#c4746e"
inactive: "#a6a69c"
success: "#87a987"
```

Themes are broken up into [light](#light-themes) and [dark](#dark-themes) themes. Light themes work best in terminals with a light background and dark themes work best in a terminal with a dark background. If no theme is specified or a non-existant theme is provided, the default theme is used, which was created to work with both terminals with a light and dark background.

### Light Themes
Expand Down
15 changes: 10 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ var rootCmd = &cobra.Command{
}
themeOverrides := viper.GetStringMapString(configKeysName.themeOverrides)

jqtheme, defaultTheme := theme.GetTheme(flags.theme)
// If not using the default theme,
styleOverrides := viper.GetStringMapString(configKeysName.styleOverrides)
jqtheme, defaultTheme := theme.GetTheme(flags.theme, styleOverrides)

// If not using the default theme,
// and if theme specified is the same as in the config,
// which happens if the theme flag was used,
// apply chroma style overrides.
Expand Down Expand Up @@ -112,10 +114,10 @@ func initConfig() {
// Search config in home directory
viper.AddConfigPath(home)

// register the config file
// register the config file
viper.SetConfigName(".jqp")

//only read from yaml files
//only read from yaml files
viper.SetConfigType("yaml")

}
Expand All @@ -139,10 +141,13 @@ var flagsName = struct {
}

var configKeysName = struct {
themeName, themeOverrides string
themeName string
themeOverrides string
styleOverrides string
}{
themeName: "theme.name",
themeOverrides: "theme.chromaStyleOverrides",
styleOverrides: "theme.styleOverrides",
}

var cfgFile string
Expand Down
49 changes: 45 additions & 4 deletions tui/theme/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ import (
"github.com/charmbracelet/lipgloss"
)

type CustomTheme struct {
Primary string
Secondary string
Inactive string
Success string
Error string
}

var CustomThemeKeys = CustomTheme{
Primary: "primary",
Secondary: "secondary",
Success: "success",
Inactive: "inactive",
Error: "error",
}

const (
BLUE = lipgloss.Color("69")
PINK = lipgloss.Color("#F25D94")
Expand Down Expand Up @@ -428,11 +444,36 @@ var themeMap = map[string]Theme{
}

// returns a theme by name, and true if default theme was returned
func GetTheme(theme string) (Theme, bool) {
lowercasedTheme := strings.ToLower(strings.TrimSpace(theme))
func GetTheme(themeName string, styleOverrides map[string]string) (Theme, bool) {
lowercasedTheme := strings.ToLower(strings.TrimSpace(themeName))

var isDefault bool
var theme Theme
if value, ok := themeMap[lowercasedTheme]; ok {
return value, false
theme = value
isDefault = false
} else {
return getDefaultTheme(), true
theme = getDefaultTheme()
isDefault = true
}

theme.SetOverrides(styleOverrides)

return theme, isDefault && len(styleOverrides) == 0
}

func (t *Theme) SetOverrides(overrides map[string]string) {
t.Primary = customColorOrDefault(overrides[CustomThemeKeys.Primary], t.Primary)
t.Secondary = customColorOrDefault(overrides[CustomThemeKeys.Secondary], t.Secondary)
t.Inactive = customColorOrDefault(overrides[CustomThemeKeys.Inactive], t.Inactive)
t.Success = customColorOrDefault(overrides[CustomThemeKeys.Success], t.Success)
t.Error = customColorOrDefault(overrides[CustomThemeKeys.Error], t.Error)
}

func customColorOrDefault(color string, def lipgloss.Color) lipgloss.Color {
if color == "" {
return def
}

return lipgloss.Color(color)
}