Skip to content

Commit

Permalink
✨ Add configuration for colour profile
Browse files Browse the repository at this point in the history
Allow setting the colour profile for the interface. Adaptive is the
default and will detect the background. This can be overridden by
setting the value in the config file.
  • Loading branch information
mikelorant committed Jan 27, 2023
1 parent ac48f50 commit 41a17be
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 32 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ view:
# Light default: builtin_light
theme: builtin_dark

# Colour profile for displaying themes.
# Values: adaptive, dark, light
# Default: adaptive
colour: adaptive

commit:
# Emoji format in commit.
# Values: shortcode, character
Expand Down
3 changes: 2 additions & 1 deletion internal/ui/body/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/hexops/autogold/v2"
"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/repository"
"github.com/mikelorant/committed/internal/ui/body"
"github.com/mikelorant/committed/internal/ui/theme"
Expand Down Expand Up @@ -165,7 +166,7 @@ func TestModel(t *testing.T) {
Message: tt.args.message,
},
},
Theme: theme.New(),
Theme: theme.New(config.ColourAdaptive),
Options: commit.Options{
Amend: tt.args.amend,
},
Expand Down
10 changes: 5 additions & 5 deletions internal/ui/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func (m *Model) defaults(cfg config.Config) {
m.defaultEmojiType(cfg.Commit.EmojiType)
m.defaultFocus(cfg.View.Focus)
m.defaultSignoff(cfg.Commit.Signoff)
m.defaultTheme(cfg.View.Theme)
m.defaultTheme(cfg.View.Theme, cfg.View.Colour)
}

func (m *Model) defaultEmojiType(et config.EmojiType) {
Expand All @@ -33,9 +33,9 @@ func (m *Model) defaultSignoff(signoff bool) {
m.signoff = signoff
}

func (m *Model) defaultTheme(t string) {
th := theme.New()
th.SetTint(t)
func (m *Model) defaultTheme(th string, clr config.Colour) {
t := theme.New(clr)
t.SetTint(th)

m.state.Theme = th
m.state.Theme = t
}
3 changes: 2 additions & 1 deletion internal/ui/filterlist/filterlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/hexops/autogold/v2"
"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/ui/filterlist"
"github.com/mikelorant/committed/internal/ui/theme"
"github.com/mikelorant/committed/internal/ui/uitest"
Expand Down Expand Up @@ -331,7 +332,7 @@ func TestModel(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
state := &commit.State{
Theme: theme.New(),
Theme: theme.New(config.ColourAdaptive),
}

m := filterlist.New(castToListItems(tt.args.items), tt.args.title, tt.args.height, state)
Expand Down
3 changes: 2 additions & 1 deletion internal/ui/footer/footer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hexops/autogold/v2"
"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/repository"
"github.com/mikelorant/committed/internal/ui/footer"
"github.com/mikelorant/committed/internal/ui/theme"
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestModel(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
state := &commit.State{
Theme: theme.New(),
Theme: theme.New(config.ColourAdaptive),
}
if tt.args.author.Name != "" && tt.args.author.Email != "" {
state.Repository.Users = []repository.User{tt.args.author}
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/header/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,6 @@ func testState() commit.State {
},
},
},
Theme: theme.New(),
Theme: theme.New(config.ColourAdaptive),
}
}
3 changes: 2 additions & 1 deletion internal/ui/help/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hexops/autogold/v2"
"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/ui/help"
"github.com/mikelorant/committed/internal/ui/theme"
"github.com/mikelorant/committed/internal/ui/uitest"
Expand Down Expand Up @@ -95,7 +96,7 @@ func TestModel(t *testing.T) {
help.Content = tt.args.content

state := &commit.State{
Theme: theme.New(),
Theme: theme.New(config.ColourAdaptive),
}

m := help.New(state)
Expand Down
3 changes: 2 additions & 1 deletion internal/ui/info/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/hexops/autogold/v2"
"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/repository"
"github.com/mikelorant/committed/internal/ui/info"
"github.com/mikelorant/committed/internal/ui/theme"
Expand Down Expand Up @@ -306,7 +307,7 @@ func testState() commit.State {
When: time.Date(2022, time.January, 1, 1, 0, 0, 0, time.UTC),
},
},
Theme: theme.New(),
Theme: theme.New(config.ColourAdaptive),
Options: commit.Options{
Amend: true,
},
Expand Down
3 changes: 2 additions & 1 deletion internal/ui/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/hexops/autogold/v2"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/ui/message"
"github.com/mikelorant/committed/internal/ui/theme"
"github.com/mikelorant/committed/internal/ui/uitest"
Expand Down Expand Up @@ -72,7 +73,7 @@ func TestModel(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := message.State{
Theme: theme.New(),
Theme: theme.New(config.ColourAdaptive),
Emoji: tt.args.emoji,
Summary: tt.args.summary,
Body: tt.args.body,
Expand Down
3 changes: 2 additions & 1 deletion internal/ui/shortcut/shortcut_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hexops/autogold/v2"
"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/ui/shortcut"
"github.com/mikelorant/committed/internal/ui/theme"
"github.com/mikelorant/committed/internal/ui/uitest"
Expand Down Expand Up @@ -177,7 +178,7 @@ func TestModel(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
state := &commit.State{
Theme: theme.New(),
Theme: theme.New(config.ColourAdaptive),
}

m := shortcut.New(shortcut.Shortcuts{
Expand Down
3 changes: 2 additions & 1 deletion internal/ui/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hexops/autogold/v2"
"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/ui/status"
"github.com/mikelorant/committed/internal/ui/theme"
"github.com/mikelorant/committed/internal/ui/uitest"
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestModel(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
state := &commit.State{
Theme: theme.New(),
Theme: theme.New(config.ColourAdaptive),
}

m := status.New(state)
Expand Down
17 changes: 9 additions & 8 deletions internal/ui/theme/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/charmbracelet/lipgloss"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/ui/theme"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -111,7 +112,7 @@ func TestBody(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)
body := th.Body()

assert.Equal(t, tt.body.Boundary, toColour(body.Boundary), "Boundary")
Expand Down Expand Up @@ -149,7 +150,7 @@ func TestFilterList(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)
filterlist := th.FilterList()

assert.Equal(t, tt.filterlist.Boundary, toColour(filterlist.Boundary), "Boundary")
Expand Down Expand Up @@ -182,7 +183,7 @@ func TestFooter(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)
footer := th.Footer()

assert.Equal(t, tt.footer.View, toColour(footer.View), "Boundary")
Expand Down Expand Up @@ -217,7 +218,7 @@ func TestHeader(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)
header := th.Header()

assert.Equal(t, tt.header.EmojiBoundary, toColour(header.EmojiBoundary), "EmojiBoundary")
Expand Down Expand Up @@ -252,7 +253,7 @@ func TestHelp(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)
help := th.Help()

assert.Equal(t, tt.help.Boundary, toColour(help.Boundary), "Boundary")
Expand Down Expand Up @@ -286,7 +287,7 @@ func TestInfo(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)
info := th.Info()

assert.Equal(t, tt.info.HashText, toColour(info.HashText), "HashText")
Expand Down Expand Up @@ -322,7 +323,7 @@ func TestMessage(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)
message := th.Message()

assert.Equal(t, tt.message.Summary, toColour(message.Summary), "Summary")
Expand Down Expand Up @@ -350,7 +351,7 @@ func TestShortcut(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)
shortcut := th.Shortcut()

assert.Equal(t, tt.shortcut.Key, toColour(shortcut.Key), "Key")
Expand Down
18 changes: 14 additions & 4 deletions internal/ui/theme/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
tint "github.com/lrstanley/bubbletint"
"github.com/mikelorant/committed/internal/config"
)

type Theme struct {
Expand All @@ -12,15 +13,24 @@ type Theme struct {

type Msg int

func New() Theme {
func New(clr config.Colour) Theme {
var reg *tint.Registry
var t []tint.Tint

switch lipgloss.HasDarkBackground() {
case true:
switch clr {
case config.ColourDark:
lipgloss.SetHasDarkBackground(true)
t = darkTints()
case false:
case config.ColourLight:
lipgloss.SetHasDarkBackground(false)
t = lightTints()
default:
switch lipgloss.HasDarkBackground() {
case true:
t = darkTints()
case false:
t = lightTints()
}
}

reg = tint.NewRegistry(t[0], t[1:]...)
Expand Down
11 changes: 6 additions & 5 deletions internal/ui/theme/theme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package theme_test
import (
"testing"

"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/ui/theme"
"github.com/stretchr/testify/assert"
)
Expand All @@ -28,7 +29,7 @@ func TestNew(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)

var ids []string
for i := 0; i < len(th.ListTints()); i++ {
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestNextTint(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)

for i := 0; i < tt.count; i++ {
th.NextTint()
Expand Down Expand Up @@ -111,7 +112,7 @@ func TestListTints(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)

got := th.ListTints()
assert.Equal(t, tt.want, got)
Expand Down Expand Up @@ -139,7 +140,7 @@ func TestGetTint(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)

_ = th.SetTint(tt.id)
got := th.GetTint()
Expand Down Expand Up @@ -176,7 +177,7 @@ func TestSetTint(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := theme.New()
th := theme.New(config.ColourAdaptive)

ok := th.SetTint(tt.id)
if !tt.want.ok {
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ func testState() commit.State {
},
},
},
Theme: theme.New(),
Theme: theme.New(config.ColourAdaptive),
Options: commit.Options{
Amend: true,
},
Expand Down

0 comments on commit 41a17be

Please sign in to comment.