Skip to content

Commit

Permalink
♻️ Split theme package
Browse files Browse the repository at this point in the history
Split theme package to move themes outside of the UI. This allows other
parts of the app which are not part of the UI to use these themes.

A colour package was added to act as a link between UI styles and the
theme package.
  • Loading branch information
mikelorant committed Feb 9, 2023
1 parent fcc9b23 commit 0d23778
Show file tree
Hide file tree
Showing 34 changed files with 390 additions and 396 deletions.
2 changes: 1 addition & 1 deletion internal/commit/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/mikelorant/committed/internal/emoji"
"github.com/mikelorant/committed/internal/repository"
"github.com/mikelorant/committed/internal/snapshot"
"github.com/mikelorant/committed/internal/ui/theme"
"github.com/mikelorant/committed/internal/theme"
)

type State struct {
Expand Down
96 changes: 96 additions & 0 deletions internal/theme/theme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package theme

import (
"os"

tint "github.com/lrstanley/bubbletint"
"github.com/mikelorant/committed/internal/config"
"github.com/muesli/termenv"
)

type Theme struct {
ID string
Registry *tint.Registry
}

func New(clr config.Colour) Theme {
ts := tints(clr)
reg := tint.NewRegistry(ts[0], ts[1:]...)

return Theme{
ID: reg.ID(),
Registry: reg,
}
}

func (t *Theme) Next() {
ids := t.List()
l := len(t.List())

switch {
case t.ID == ids[l-1]:
t.Set(ids[0])
default:
t.Registry.NextTint()
}

t.ID = t.Registry.ID()
}

func (t *Theme) Set(id string) bool {
if ok := t.Registry.SetTintID(id); !ok {
return false
}

t.ID = id

return true
}

func (t *Theme) List() []string {
var ts []string

for _, t := range t.Registry.Tints() {
ts = append(ts, t.ID())
}

return ts
}

func tints(clr config.Colour) []tint.Tint {
if clr == config.ColourDark {
return dark()
}

if clr == config.ColourLight {
return light()
}

if termenv.NewOutput(os.Stdout).HasDarkBackground() {
return dark()
}

return light()
}

func dark() []tint.Tint {
return []tint.Tint{
tint.TintBuiltinDark,
tint.TintGruvboxDark,
tint.TintSolarizedDarkHigherContrast,
tint.TintRetrowave,
tint.TintDracula,
tint.TintNord,
tint.TintTokyoNight,
}
}

func light() []tint.Tint {
return []tint.Tint{
tint.TintBuiltinLight,
tint.TintGruvboxLight,
tint.TintBuiltinSolarizedLight,
tint.TintBuiltinTangoLight,
tint.TintTokyoNightLight,
}
}
62 changes: 12 additions & 50 deletions internal/ui/theme/theme_test.go → internal/theme/theme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/ui/theme"
"github.com/mikelorant/committed/internal/theme"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -64,16 +64,16 @@ func TestNew(t *testing.T) {
th := theme.New(tt.colour)

var ids []string
for i := 0; i < len(th.ListTints()); i++ {
ids = append(ids, th.ListTints()[i])
for i := 0; i < len(th.List()); i++ {
ids = append(ids, th.List()[i])
}

assert.Equal(t, tt.ids, ids)
})
}
}

func TestNextTint(t *testing.T) {
func TestNext(t *testing.T) {
t.Parallel()

tests := []struct {
Expand Down Expand Up @@ -121,15 +121,15 @@ func TestNextTint(t *testing.T) {
th := theme.New(config.ColourAdaptive)

for i := 0; i < tt.count; i++ {
th.NextTint()
th.Next()
}

assert.Equal(t, tt.id, th.GetTint())
assert.Equal(t, tt.id, th.ID)
})
}
}

func TestListTints(t *testing.T) {
func TestList(t *testing.T) {
t.Parallel()

tests := []struct {
Expand Down Expand Up @@ -158,49 +158,13 @@ func TestListTints(t *testing.T) {

th := theme.New(config.ColourAdaptive)

got := th.ListTints()
got := th.List()
assert.Equal(t, tt.want, got)
})
}
}

func TestGetTint(t *testing.T) {
t.Parallel()

tests := []struct {
name string
id string
want string
}{
{
name: "valid",
id: "builtin_dark",
want: "builtin_dark",
},
{
name: "invalid",
id: "invalid",
want: "builtin_dark",
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

th := theme.New(config.ColourAdaptive)

_ = th.SetTint(tt.id)
got := th.GetTint()

assert.Equal(t, tt.want, got)
})
}
}

func TestSetTint(t *testing.T) {
func TestSet(t *testing.T) {
t.Parallel()

type want struct {
Expand Down Expand Up @@ -238,17 +202,15 @@ func TestSetTint(t *testing.T) {

th := theme.New(config.ColourAdaptive)

ok := th.SetTint(tt.id)
ok := th.Set(tt.id)
if !tt.want.ok {
assert.False(t, ok)
got := th.GetTint()
assert.Equal(t, tt.want.id, got)
assert.Equal(t, tt.want.id, th.ID)
return
}
assert.True(t, ok)

got := th.GetTint()
assert.Equal(t, tt.want.id, got)
assert.Equal(t, tt.want.id, th.ID)
})
}
}
4 changes: 2 additions & 2 deletions internal/ui/body/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/ui/theme"
"github.com/mikelorant/committed/internal/ui/colour"
)

type Model struct {
Expand Down Expand Up @@ -63,7 +63,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

//nolint:gocritic
switch msg.(type) {
case theme.Msg:
case colour.Msg:
m.styles = defaultStyles(m.state.Theme)
styleTextArea(&m.textArea, m.state)
switch m.textArea.Focused() {
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/body/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/hexops/autogold/v2"
"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/theme"
"github.com/mikelorant/committed/internal/ui/body"
"github.com/mikelorant/committed/internal/ui/theme"
"github.com/mikelorant/committed/internal/ui/uitest"
"github.com/stretchr/testify/assert"
)
Expand Down
17 changes: 9 additions & 8 deletions internal/ui/body/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package body

import (
"github.com/charmbracelet/lipgloss"
"github.com/mikelorant/committed/internal/ui/theme"
"github.com/mikelorant/committed/internal/theme"
"github.com/mikelorant/committed/internal/ui/colour"
)

type Styles struct {
Expand All @@ -17,7 +18,7 @@ type Styles struct {
func defaultStyles(th theme.Theme) Styles {
var s Styles

colour := th.Body()
clr := colour.New(th).Body()

s.boundary = lipgloss.NewStyle().
Width(74).
Expand All @@ -26,23 +27,23 @@ func defaultStyles(th theme.Theme) Styles {
MarginLeft(4).
Align(lipgloss.Left, lipgloss.Top).
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(colour.Boundary).
BorderForeground(clr.Boundary).
Padding(0, 1, 0, 1)

s.textAreaPlaceholder = lipgloss.NewStyle().
Foreground(colour.TextAreaPlaceholder)
Foreground(clr.TextAreaPlaceholder)

s.textAreaPrompt = lipgloss.NewStyle().
Foreground(colour.TextAreaPrompt)
Foreground(clr.TextAreaPrompt)

s.textAreaFocusedText = lipgloss.NewStyle().
Foreground(colour.TextAreaFocusedText)
Foreground(clr.TextAreaFocusedText)

s.textAreaBlurredText = lipgloss.NewStyle().
Foreground(colour.TextAreaBlurredText)
Foreground(clr.TextAreaBlurredText)

s.textAreaCursorStyle = lipgloss.NewStyle().
Foreground(colour.TextAreaCursorStyle)
Foreground(clr.TextAreaCursorStyle)

return s
}
Loading

0 comments on commit 0d23778

Please sign in to comment.