-
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.
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
1 parent
0a9671b
commit ef361a3
Showing
6 changed files
with
246 additions
and
0 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
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,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 | ||
} |
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,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 | ||
} |
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,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)) | ||
}) | ||
} | ||
} |
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,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 | ||
} |