Skip to content

Commit

Permalink
🚧 Add radio pane
Browse files Browse the repository at this point in the history
A radio pane shows a list of values with only a single value selected.
A title headlines the list of values.
  • Loading branch information
mikelorant committed Mar 1, 2023
1 parent ef361a3 commit 74a1c75
Show file tree
Hide file tree
Showing 16 changed files with 421 additions and 9 deletions.
20 changes: 17 additions & 3 deletions internal/ui/colour/colour.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,14 @@ type optionSection struct {
SettingJoiner lipgloss.TerminalColor
}

type optionSetting struct{}
type optionSetting struct {
Setting lipgloss.TerminalColor
SettingTitle lipgloss.TerminalColor
SettingSelected lipgloss.TerminalColor
SettingTitleSelected lipgloss.TerminalColor
SettingDotEmpty lipgloss.TerminalColor
SettingDotFilled lipgloss.TerminalColor
}

type shortcut struct {
Key lipgloss.TerminalColor
Expand Down Expand Up @@ -268,9 +275,16 @@ func (c *Colour) OptionSection() optionSection {

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

return optionSetting{}
return optionSetting{
Setting: clr.Fg(),
SettingSelected: ToAdaptive(clr.BrightWhite()),
SettingTitle: clr.Fg(),
SettingTitleSelected: ToAdaptive(clr.BrightWhite()),
SettingDotEmpty: clr.Fg(),
SettingDotFilled: ToAdaptive(clr.Cyan()),
}
}

//nolint:revive
Expand Down
29 changes: 25 additions & 4 deletions internal/ui/colour/colour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ type optionSection struct {
SettingJoiner Colour
}

type optionSetting struct{}
type optionSetting struct {
Setting Colour
SettingSelected Colour
SettingTitle Colour
SettingTitleSelected Colour
SettingDotEmpty Colour
SettingDotFilled Colour
}

type shortcut struct {
Key Colour
Expand Down Expand Up @@ -491,8 +498,15 @@ func TestOptionSetting(t *testing.T) {
optionSetting optionSetting
}{
{
name: "OptionSetting",
optionSetting: optionSetting{},
name: "OptionSetting",
optionSetting: optionSetting{
Setting: Colour{Dark: "#bbbbbb"},
SettingSelected: Colour{Dark: "#ffffff", Light: "#ffffff"},
SettingTitle: Colour{Dark: "#bbbbbb"},
SettingTitleSelected: Colour{Dark: "#ffffff", Light: "#ffffff"},
SettingDotEmpty: Colour{Dark: "#bbbbbb"},
SettingDotFilled: Colour{Dark: "#00bbbb", Light: "#bb0000"},
},
},
}

Expand All @@ -502,7 +516,14 @@ func TestOptionSetting(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

_ = colour.New(theme.New(config.ColourAdaptive)).OptionSetting()
clr := colour.New(theme.New(config.ColourAdaptive)).OptionSetting()

assert.Equal(t, tt.optionSetting.Setting, toColour(clr.Setting), "Setting")
assert.Equal(t, tt.optionSetting.SettingSelected, toColour(clr.SettingSelected), "SettingSelected")
assert.Equal(t, tt.optionSetting.SettingTitle, toColour(clr.SettingTitle), "SettingTitle")
assert.Equal(t, tt.optionSetting.SettingTitleSelected, toColour(clr.SettingTitleSelected), "SettingTitleSelected")
assert.Equal(t, tt.optionSetting.SettingDotEmpty, toColour(clr.SettingDotEmpty), "SettingDotEmpty")
assert.Equal(t, tt.optionSetting.SettingDotFilled, toColour(clr.SettingDotFilled), "SettingDotFilled")
})
}
}
Expand Down
83 changes: 83 additions & 0 deletions internal/ui/option/setting/radio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package setting

import (
"fmt"
"strings"
)

type Radio struct {
Title string
Values []string
Index int

focus bool
}

func (r *Radio) Render(styles Styles) string {
var str []string

switch r.focus {
case true:
str = append(str, styles.settingTitleSelected.Render(r.Title))
default:
str = append(str, styles.settingTitle.Render(r.Title))
}

for idx, val := range r.Values {
if idx == r.Index {
v := styles.settingSelected.Render(val)
str = append(str, fmt.Sprintf("%v %v", styles.settingDotFilled, v))

continue
}

v := styles.setting.Render(val)
str = append(str, fmt.Sprintf("%v %v", styles.settingDotEmpty, v))
}

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

func (r *Radio) Focus() {
r.focus = true
}

func (r *Radio) Blur() {
r.focus = false
}

func (r *Radio) Value() string {
return r.Values[r.Index]
}

func (r *Radio) Next() {
if r.Index >= len(r.Values)-1 {
r.Index = 0
return
}

r.Index++
}

func (r *Radio) Previous() {
if r.Index <= 0 {
r.Index = len(r.Values) - 1
return
}

r.Index--
}

func (r *Radio) Select(i int) {
r.Index = i
}

func (r *Radio) Type() Type {
return TypeRadio
}

func ToRadio(p Paner) *Radio {
r, _ := p.(*Radio)

return r
}
8 changes: 8 additions & 0 deletions internal/ui/option/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Type int
const (
TypeUnset = iota
TypeNoop
TypeRadio
)

const (
Expand Down Expand Up @@ -86,6 +87,13 @@ func (m *Model) AddPaneSet(name string, ps []Paner) {

func (m *Model) SelectPane(title string) {
for _, p := range m.panes {
switch pane := p.(type) {
case *Radio:
if pane.Title != title {
continue
}
}

m.Selected.Blur()
m.Selected = p
m.Selected.Focus()
Expand Down
Loading

0 comments on commit 74a1c75

Please sign in to comment.