Skip to content

Commit

Permalink
fix: Consistent warning messages for invalid styles.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalton committed Feb 2, 2022
1 parent baf319f commit 0deca5d
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 38 deletions.
3 changes: 1 addition & 2 deletions docs/docs/reference/modules.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ Where:

The number of unmerged paths is not shown if it is 0. The "unstaged" and "staged" halves of this are also hidden if all values are zero. By default, unstaged counts are shown in red an staged in green, to mimic the output colors of `git status`.

TODO: Show how you could use a template to make this look like starship prompt.

Configuration:

- `indexStyle (string)` is the style to use for the staged status.
Expand Down Expand Up @@ -189,6 +187,7 @@ The project module works out what kind of project the current folder represents,
Configuration:

- `projects` is a map where keys are project names, and values are `{ style, toolSymbol, packageManagerSymbol }` objects, which can be used to provide a custom style and symbols for existing projects on a theme-by-theme basis.
- `defaultProjectStyle` is the style to use if no project-specific style is specified.

Outputs:

Expand Down
6 changes: 1 addition & 5 deletions internal/kitsch/modules/commonConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/jwalton/kitsch/internal/kitsch/condition"
"github.com/jwalton/kitsch/internal/kitsch/log"
"gopkg.in/yaml.v3"
)

Expand All @@ -28,10 +27,7 @@ type CommonConfig struct {
// Validate checks for common configuration errors in the CommonConfig, and prints
// errors to the log if any are found.
func (config *CommonConfig) Validate(context *Context, prefix string) {
_, err := context.Styles.Get(config.Style)
if err != nil {
log.Warn(fmt.Sprintf("%s: Error parsing style: %v", prefix, err))
}
context.GetStyle(config.Style)
}

func getCommonConfig(node *yaml.Node) (CommonConfig, error) {
Expand Down
13 changes: 13 additions & 0 deletions internal/kitsch/modules/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/jwalton/kitsch/internal/gitutils"
"github.com/jwalton/kitsch/internal/kitsch/env"
"github.com/jwalton/kitsch/internal/kitsch/getters"
"github.com/jwalton/kitsch/internal/kitsch/log"
"github.com/jwalton/kitsch/internal/kitsch/projects"
"github.com/jwalton/kitsch/internal/kitsch/styling"
"golang.org/x/term"
Expand Down Expand Up @@ -160,6 +161,18 @@ func (context *Context) Git() gitutils.Git {
return context.git
}

// GetStyle returns the specified style, or logs a warning and returns an empty style
// if the style string cannot be parsed.
func (context *Context) GetStyle(styleString string) *styling.Style {
style, err := context.Styles.Get(styleString)
if err != nil {
log.Warn(`Unable to parse style: "`+styleString+`":`, err.Error())
style, _ = context.Styles.Get("")
}

return style
}

// NewContext creates a new Context object for executing modules.
func NewContext(
globals Globals,
Expand Down
2 changes: 0 additions & 2 deletions internal/kitsch/modules/git_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ type GitStateModule struct {
Bisecting string `yaml:"bisecting"`
}

// FIXME: Let user configure the names fo rebase/cherry-pick/merge/etc...

type gitStateResult struct {
// State is the current state of this repo.
State gitutils.RepositoryStateType `yaml:"state"`
Expand Down
6 changes: 3 additions & 3 deletions internal/kitsch/modules/git_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func (mod GitStatusModule) renderDefault(
indexTotal := stats.Index.Added + stats.Index.Modified + stats.Index.Deleted
unstagedTotal := stats.Unstaged.Added + stats.Unstaged.Modified + stats.Unstaged.Deleted

indexStyle := defaultStyle(context, mod.IndexStyle, "green")
unstagedStyle := defaultStyle(context, mod.UnstagedStyle, "red")
stashStyle := defaultStyle(context, mod.StashStyle, "brightRed")
indexStyle := context.GetStyle(mod.IndexStyle)
unstagedStyle := context.GetStyle(mod.UnstagedStyle)
stashStyle := context.GetStyle(mod.StashStyle)

if (indexTotal) > 0 || stats.Unmerged > 0 {
indexPart := mod.renderStats(stats.Index)
Expand Down
6 changes: 1 addition & 5 deletions internal/kitsch/modules/moduleWrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,7 @@ func processModuleResult(
if moduleResult.StyleOverride != "" {
styleStr = moduleResult.StyleOverride
}
style, err := context.Styles.Get(styleStr)
if err != nil {
style = nil
log.Warn(err)
}
style := context.GetStyle(styleStr)

text := moduleResult.DefaultText
startStyle := moduleResult.StartStyle
Expand Down
16 changes: 0 additions & 16 deletions internal/kitsch/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package modules

import (
"github.com/jwalton/kitsch/internal/kitsch/log"
"github.com/jwalton/kitsch/internal/kitsch/styling"
"github.com/jwalton/kitsch/internal/perf"
)
Expand Down Expand Up @@ -59,18 +58,3 @@ func defaultString(value string, def string) string {
}
return def
}

func defaultStyle(context *Context, styleString string, defStyle string) *styling.Style {
style, err := context.Styles.Get(styleString)
if err != nil {
log.Warn(err.Error())
}
if styleString == "" || err != nil {
style, err = context.Styles.Get(defStyle)
if err != nil {
panic("Error parsing default style: " + err.Error())
}
}

return style
}
11 changes: 6 additions & 5 deletions internal/kitsch/modules/project.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package modules

import (
"github.com/jwalton/kitsch/internal/kitsch/log"
"github.com/jwalton/kitsch/internal/kitsch/modules/schemas"
"github.com/jwalton/kitsch/internal/kitsch/projects"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -52,6 +51,8 @@ type ProjectModule struct {
Type string `yaml:"type" jsonschema:",required,enum=project"`
// Projects is project-specific configuration.
Projects map[string]ProjectConfig `yaml:"projects"`
// DefaultProjectStyle is the style to use if no project-specific style is specified.
DefaultProjectStyle string `yaml:"defaultProjectStyle"`
}

// Execute the module.
Expand All @@ -76,11 +77,11 @@ func (mod ProjectModule) Execute(context *Context) ModuleResult {
ProjectStyle: overrides.Style,
}

projectStyle, err := context.Styles.Get(data.ProjectStyle)
if err != nil {
log.Warn("Invalid style " + data.ProjectStyle + ": " + err.Error())
projectStyle, _ = context.Styles.Get("")
projectStyleString := data.ProjectStyle
if projectStyleString == "" {
projectStyleString = mod.DefaultProjectStyle
}
projectStyle := context.GetStyle(projectStyleString)

text := ""
if data.ToolVersion != "" {
Expand Down

0 comments on commit 0deca5d

Please sign in to comment.