Skip to content
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
55 changes: 55 additions & 0 deletions cmd/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func newThemeCmd() *cobra.Command {
Short: "Inspect grut themes",
}
themeCmd.AddCommand(newThemeListCmd(theme.ListThemes))
themeCmd.AddCommand(newThemeShowCmd(theme.Load))
return themeCmd
}

Expand All @@ -41,3 +42,57 @@ func newThemeListCmd(list themeListFunc) *cobra.Command {
listCmd.Flags().BoolVar(&asJSON, "json", false, "Print theme names as JSON")
return listCmd
}

type themeLoadFunc func(string) (*theme.Theme, error)

type themeShowReport struct {
Name string `json:"name"`
Variant string `json:"variant"`
Mode string `json:"mode"`
Colors theme.Colors `json:"colors"`
}

func newThemeShowCmd(load themeLoadFunc) *cobra.Command {
var asJSON bool
showCmd := &cobra.Command{
Use: "show <name>",
Short: "Show resolved theme details",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
resolved, err := load(args[0])
if err != nil {
return err
}
report := themeShowReport{
Name: resolved.Name,
Variant: resolved.Variant,
Mode: string(resolved.Mode),
Colors: resolved.Colors,
}
if asJSON {
enc := json.NewEncoder(cmd.OutOrStdout())
enc.SetIndent("", " ")
return enc.Encode(report)
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Name: %s\n", report.Name)
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Variant: %s\n", report.Variant)
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Mode: %s\n", report.Mode)
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Colors: %d slots\n", themeColorSlotCount(report.Colors))
return nil
},
}
showCmd.Flags().BoolVar(&asJSON, "json", false, "Print resolved theme details as JSON")
return showCmd
}

func themeColorSlotCount(colors theme.Colors) int {
data, err := json.Marshal(colors)
if err != nil {
return 0
}
var slots map[string]string
if err := json.Unmarshal(data, &slots); err != nil {
return 0
}
return len(slots)
}
37 changes: 37 additions & 0 deletions cmd/theme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"testing"

"github.com/jongio/grut/internal/theme"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -34,6 +36,41 @@ func TestThemeListCommandPrintsJSON(t *testing.T) {
assert.Equal(t, []string{"default", "gruvbox"}, names)
}

func TestThemeShowCommandPrintsText(t *testing.T) {
cmd := newThemeShowCmd(func(string) (*theme.Theme, error) {
return &theme.Theme{Name: "default", Variant: "dark", Mode: theme.ModeColor, Colors: theme.Colors{Background: "#000000"}}, nil
})
cmd.SetArgs([]string{"default"})
var out bytes.Buffer
cmd.SetOut(&out)

err := cmd.Execute()

require.NoError(t, err)
assert.Contains(t, out.String(), "Name: default")
assert.Contains(t, out.String(), "Variant: dark")
assert.Contains(t, out.String(), "Mode: color")
}

func TestThemeShowCommandPrintsJSON(t *testing.T) {
cmd := newThemeShowCmd(func(string) (*theme.Theme, error) {
return &theme.Theme{Name: "gruvbox", Variant: "dark", Mode: theme.ModeColor, Colors: theme.Colors{Background: "#282828"}}, nil
})
cmd.SetArgs([]string{"gruvbox", "--json"})
var out bytes.Buffer
cmd.SetOut(&out)

err := cmd.Execute()

require.NoError(t, err)
var report themeShowReport
require.NoError(t, json.Unmarshal(out.Bytes(), &report))
assert.Equal(t, "gruvbox", report.Name)
assert.Equal(t, "dark", report.Variant)
assert.Equal(t, "color", report.Mode)
assert.Equal(t, "#282828", report.Colors.Background)
}

func TestRootRegistersThemeListCommand(t *testing.T) {
root, cleanup := newRootCommand()
defer cleanup()
Expand Down
Loading