Skip to content

Commit

Permalink
✨ Add listing of themes and emojis
Browse files Browse the repository at this point in the history
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
mikelorant committed Feb 9, 2023
1 parent 0d23778 commit 1e6c132
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 20 deletions.
61 changes: 51 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ commits by showing you the layout in the same format as `git log`.
brew install mikelorant/committed/committed
```

2. Before creating and applying a commit you will need to stage the files you wish
to add with the `git add` command.
2. Before creating and applying a commit you will need to stage the files you
wish to add with the `git add` command.

3. Committed replaces the `git commit` command and all you need to do to commit
your change is to run:
Expand Down Expand Up @@ -88,9 +88,9 @@ useful.

### Option Key

The option key needs to be set to send the `meta` or `esc+` keycode. Terminals such
as macOS Terminal or iTerm2 may not have this as default. If not set correctly it will
not be possible to apply a commit.
The option key needs to be set to send the `meta` or `esc+` keycode. Terminals
such as macOS Terminal or iTerm2 may not have this as default. If not set
correctly it will not be possible to apply a commit.

To make these changes following the instructions below.

Expand Down Expand Up @@ -140,14 +140,18 @@ Usage:
Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
list List settings with profiles or IDs
version Print the version information
Flags:
--config string Config file location (default "$HOME/.config/committed/config.yaml")
--dry-run Simulate applying a commit (default true)
-a, --amend Replace the tip of the current branch by creating a new commit
-h, --help help for committed
-v, --version version for committed
--config string Config file location (default
"$HOME/.config/committed/config.yaml")
--snapshot string Snapshot file location (default
"$HOME/.local/state/committed/snapshot.yaml")
--dry-run Simulate applying a commit (default true)
-a, --amend Replace the tip of the current branch by creating a new commit
-h, --help help for committed
-v, --version version for committed
Use "committed [command] --help" for more information about a command.
```
Expand Down Expand Up @@ -214,6 +218,43 @@ authors:
email: john.doe@example.com
```
### Themes
There are a number of themes available that modify the colours. By default, the
background colour is detected which changes the choices of themes. This
detection can be disabled by setting the colour profile in the configuration.
The first theme of each set is the default theme applied.
#### Dark Themes
| Name | ID |
| :----------------------------------------------------------- | :----------------------------- |
| Builtin Dark | builtin_dark |
| [Dracula](https://draculatheme.com/) | dracula |
| [Gruvbox Dark](https://github.com/morhetz/gruvbox) | gruvbox_dark |
| [Nord](https://www.nordtheme.com/) | nord |
| Retrowave | retrowave |
| [Solarized Dark Higher Contrast](https://ethanschoonover.com/solarized/) | solarized_dark_higher_contrast |
| [Tokyo Night](https://github.com/enkia/tokyo-night-vscode-theme) | tokyo_night |
#### Light Theme
| Name | ID |
| :------------------------------------------------------------------------ | :---------------------- |
| Builtin Light | builtin_light |
| [Builtin Solarized Light](https://ethanschoonover.com/solarized/) | builtin_solarized_light |
| [Builtin Tango Light](http://tango.freedesktop.org/Tango_Desktop_Project) | builtin_tango_light |
| [Gruvbox Light](https://github.com/morhetz/gruvbox) | gruvbox_light |
| [Tokyo Night Light](https://github.com/enkia/tokyo-night-vscode-theme) | tokyo_night_light |
### Emoji Profiles
Popular emoji sets can be set as the default profile:
- [Gitmoji](https://gitmoji.dev/)
- [Devmoji](https://github.com/folke/devmoji)
- [Emoji-Log](https://github.com/ahmadawais/emoji-log)
## 🏆 Best Practises [⭡](#committed)
To create a well formed commit, these are some of the best practises that are
Expand Down
120 changes: 120 additions & 0 deletions cmd/list.go
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, "")
}
99 changes: 99 additions & 0 deletions cmd/list_test.go
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))
})
}
}
10 changes: 7 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package cmd
import (
"errors"
"fmt"
"io"
"log"
"os"

"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/ui"

"github.com/go-git/go-git/v5"
cc "github.com/ivanpirog/coloredcobra"
"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/ui"
"github.com/spf13/cobra"
)

Expand All @@ -32,6 +32,7 @@ type App struct {
Commiter Commiter
UIer UIer
Logger Logger
Writer io.Writer

req *commit.Request
opts commit.Options
Expand Down Expand Up @@ -61,6 +62,7 @@ func NewRootCmd(a App) *cobra.Command {
)

cmd.AddCommand(NewVersionCmd())
cmd.AddCommand(NewListCmd(a.Writer))
cmd.SetVersionTemplate(verTmpl)
cmd.Flags().SortFlags = false
cmd.Flags().StringVarP(&a.opts.ConfigFile, "config", "", defaultConfigFile, "Config file location")
Expand Down Expand Up @@ -93,11 +95,13 @@ func NewApp() App {
c := commit.New()
u := ui.New()
l := log.Default()
w := os.Stdout

return App{
Commiter: &c,
UIer: &u,
Logger: l,
Writer: w,
}
}

Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/help_arg.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Usage:
Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
list List settings with profiles or IDs
version Print the version information

Flags:
Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/help_flag.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Usage:
Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
list List settings with profiles or IDs
version Print the version information

Flags:
Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/invalid.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Usage:
Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
list List settings with profiles or IDs
version Print the version information

Flags:
Expand Down
Loading

0 comments on commit 1e6c132

Please sign in to comment.