Skip to content

Commit

Permalink
🚧 Add setting option foundation
Browse files Browse the repository at this point in the history
Add interface to display setting options. A pane displays the values of
a setting as well as the current value.

Multiple pane types will be added in future commits. A noop pane has
been added for testing purposes.
  • Loading branch information
mikelorant committed Mar 1, 2023
1 parent 0a9671b commit ef361a3
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/ui/colour/colour.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ type optionSection struct {
SettingJoiner lipgloss.TerminalColor
}

type optionSetting struct{}

type shortcut struct {
Key lipgloss.TerminalColor
Label lipgloss.TerminalColor
Expand Down Expand Up @@ -264,6 +266,13 @@ func (c *Colour) OptionSection() optionSection {
}
}

//nolint:revive
func (c *Colour) OptionSetting() optionSetting {
_ = c.registry

return optionSetting{}
}

//nolint:revive
func (c *Colour) Shortcut() shortcut {
clr := c.registry
Expand Down
26 changes: 26 additions & 0 deletions internal/ui/colour/colour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ type optionSection struct {
SettingJoiner Colour
}

type optionSetting struct{}

type shortcut struct {
Key Colour
Label Colour
Expand Down Expand Up @@ -481,6 +483,30 @@ func TestOptionSection(t *testing.T) {
}
}

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

tests := []struct {
name string
optionSetting optionSetting
}{
{
name: "OptionSetting",
optionSetting: optionSetting{},
},
}

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

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

_ = colour.New(theme.New(config.ColourAdaptive)).OptionSetting()
})
}
}

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

Expand Down
15 changes: 15 additions & 0 deletions internal/ui/option/setting/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package setting

type Noop struct{}

func (n *Noop) Render(styles Styles) string {
return ""
}

func (n *Noop) Focus() {}

func (n *Noop) Blur() {}

func (n *Noop) Type() Type {
return TypeNoop
}
113 changes: 113 additions & 0 deletions internal/ui/option/setting/setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package setting

import (
"strings"

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

tea "github.com/charmbracelet/bubbletea"
)

type Paner interface {
Type() Type
Focus()
Blur()
Render(Styles) string
}

type Model struct {
Width int
Height int
Selected Paner

paneSet map[string][]Paner
panes []Paner
styles Styles
state *commit.State
}

type Type int

const (
TypeUnset = iota
TypeNoop
)

const (
defaultWidth = 20
defaultHeight = 20
)

func New(state *commit.State) Model {
return Model{
Width: defaultWidth,
Height: defaultHeight,
paneSet: make(map[string][]Paner),
styles: defaultStyles(state.Theme),
state: state,
}
}

func (m Model) Init() tea.Cmd {
return nil
}

//nolint:ireturn
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
//nolint:gocritic
switch msg.(type) {
case colour.Msg:
m.styles = defaultStyles(m.state.Theme)
}

return m, nil
}

func (m Model) View() string {
return m.RenderPanes()
}

func (m Model) RenderPanes() string {
var str []string

for _, p := range m.panes {
str = append(str, p.Render(m.styles))
}

return strings.Join(str, "\n\n")
}

func (m *Model) AddPaneSet(name string, ps []Paner) {
m.paneSet[name] = ps
m.panes = m.paneSet[name]
m.Selected = m.panes[0]
}

func (m *Model) SelectPane(title string) {
for _, p := range m.panes {
m.Selected.Blur()
m.Selected = p
m.Selected.Focus()

return
}
}

//nolint:ireturn
func (m *Model) ActivePane() Paner {
return m.Selected
}

func (m *Model) SwapPaneSet(name string) {
//nolint:gosimple
m.panes, _ = m.paneSet[name]
}

func (m Model) GetPaneSets() map[string][]Paner {
return m.paneSet
}

func ToModel(m tea.Model, c tea.Cmd) (Model, tea.Cmd) {
return m.(Model), c
}
67 changes: 67 additions & 0 deletions internal/ui/option/setting/setting_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package setting_test

import (
"testing"

"github.com/mikelorant/committed/internal/commit"
"github.com/mikelorant/committed/internal/config"
"github.com/mikelorant/committed/internal/theme"
"github.com/mikelorant/committed/internal/ui/option/setting"
"github.com/mikelorant/committed/internal/ui/uitest"

"github.com/hexops/autogold/v2"
)

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

type args struct {
panes []setting.Paner
model func(setting.Model) setting.Model
}

type want struct {
model func(setting.Model)
}

tests := []struct {
name string
args args
want want
}{
{
name: "noop",
args: args{
panes: []setting.Paner{
&setting.Noop{},
},
},
},
}

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

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

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

m := setting.New(state)
m.AddPaneSet("Set", tt.args.panes)

if tt.args.model != nil {
m = tt.args.model(m)
}

if tt.want.model != nil {
tt.want.model(m)
}

v := uitest.StripString(m.View())
autogold.ExpectFile(t, autogold.Raw(v), autogold.Name(tt.name))
})
}
}
16 changes: 16 additions & 0 deletions internal/ui/option/setting/style.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package setting

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

type Styles struct{}

func defaultStyles(th theme.Theme) Styles {
var s Styles

_ = colour.New(th).OptionSetting()

return s
}

0 comments on commit ef361a3

Please sign in to comment.