-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The themes and emoji profiles are configurable. To discover the values to set them to a list command was added. Themes required some extra methods to provide the necessary fields. A minor refactor was applied to the name of the previous list command.
- Loading branch information
1 parent
0d23778
commit 1e6c132
Showing
15 changed files
with
351 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/charmbracelet/lipgloss" | ||
tint "github.com/lrstanley/bubbletint" | ||
"github.com/mikelorant/committed/internal/config" | ||
"github.com/mikelorant/committed/internal/emoji" | ||
"github.com/mikelorant/committed/internal/theme" | ||
"github.com/rodaine/table" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewListCmd(w io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "List settings with profiles or IDs", | ||
} | ||
|
||
cmd.AddCommand(NewListThemesCmd(w)) | ||
cmd.AddCommand(NewListEmojiProfilesCmd(w)) | ||
|
||
return cmd | ||
} | ||
|
||
func NewListThemesCmd(w io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "themes", | ||
Short: "List theme IDs", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
listThemes(w) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func NewListEmojiProfilesCmd(w io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "emojis", | ||
Short: "List emoji profiles", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
listEmojiProfiles(w) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func listThemes(w io.Writer) { | ||
th := theme.New(config.ColourAdaptive) | ||
|
||
tbl := table.New("Name", "ID", "Palette") | ||
tbl.WithHeaderFormatter(header(th.Registry)) | ||
tbl.WithWidthFunc(lipgloss.Width) | ||
tbl.WithWriter(w) | ||
|
||
for _, v := range th.List() { | ||
th.Set(v.ID()) | ||
tbl.AddRow(v.DisplayName(), v.ID(), palette(th.Registry)) | ||
} | ||
|
||
tbl.Print() | ||
} | ||
|
||
func listEmojiProfiles(w io.Writer) { | ||
em := emoji.New() | ||
th := theme.New(config.ColourAdaptive) | ||
|
||
tbl := table.New("Profile", "URL") | ||
tbl.WithHeaderFormatter(header(th.Registry)) | ||
tbl.WithWidthFunc(lipgloss.Width) | ||
tbl.WithWriter(w) | ||
|
||
for _, v := range em.ListProfiles()[1:] { | ||
tbl.AddRow(emoji.ToString(v), emoji.ToURL(v)) | ||
} | ||
|
||
tbl.Print() | ||
} | ||
|
||
func header(reg *tint.Registry) func(format string, vals ...interface{}) string { | ||
fg := reg.BrightBlack() | ||
if lipgloss.HasDarkBackground() { | ||
fg = reg.BrightWhite() | ||
} | ||
|
||
return func(format string, vals ...interface{}) string { | ||
var ss []any | ||
|
||
for _, v := range vals { | ||
style := lipgloss.NewStyle().Foreground(fg) | ||
ss = append(ss, style.Render(v.(string))) | ||
} | ||
|
||
return fmt.Sprintf(format, ss...) | ||
} | ||
} | ||
|
||
func palette(reg *tint.Registry) string { | ||
var colours []string | ||
|
||
pal := []lipgloss.TerminalColor{ | ||
reg.Black(), reg.Red(), reg.Green(), reg.Yellow(), | ||
reg.Blue(), reg.Purple(), reg.Cyan(), reg.White(), | ||
reg.BrightBlack(), reg.BrightRed(), reg.BrightGreen(), reg.BrightYellow(), | ||
reg.BrightBlue(), reg.BrightPurple(), reg.BrightCyan(), reg.BrightWhite(), | ||
} | ||
|
||
for _, v := range pal { | ||
colours = append(colours, lipgloss.NewStyle().Background(v).Render(" ")) | ||
} | ||
|
||
return strings.Join(colours, "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package cmd_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/hexops/autogold/v2" | ||
"github.com/mikelorant/committed/cmd" | ||
) | ||
|
||
func TestListCmd(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
}{ | ||
{ | ||
name: "list_arg", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var buf bytes.Buffer | ||
|
||
list := cmd.NewListCmd(&buf) | ||
list.SetOut(&buf) | ||
list.SetErr(&buf) | ||
list.SetArgs([]string{}) | ||
|
||
list.Execute() | ||
autogold.ExpectFile(t, autogold.Raw(buf.String()), autogold.Name(tt.name)) | ||
}) | ||
} | ||
} | ||
|
||
func TestListEmojiCmd(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
}{ | ||
{ | ||
name: "list_emoji_arg", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var buf bytes.Buffer | ||
|
||
list := cmd.NewListEmojiProfilesCmd(&buf) | ||
list.SetOut(&buf) | ||
list.SetErr(&buf) | ||
list.SetArgs([]string{}) | ||
|
||
list.Execute() | ||
autogold.ExpectFile(t, autogold.Raw(buf.String()), autogold.Name(tt.name)) | ||
}) | ||
} | ||
} | ||
|
||
func TestListThemeCmd(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
}{ | ||
{ | ||
name: "list_theme_arg", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var buf bytes.Buffer | ||
|
||
list := cmd.NewListThemesCmd(&buf) | ||
list.SetOut(&buf) | ||
list.SetErr(&buf) | ||
list.SetArgs([]string{}) | ||
|
||
list.Execute() | ||
autogold.ExpectFile(t, autogold.Raw(buf.String()), autogold.Name(tt.name)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.