Skip to content

Commit

Permalink
menu: changed my mind on exposing underlying list
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Miller committed Mar 15, 2024
1 parent 8d9d915 commit de89207
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions menu/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ func (m Model) handleKeyMsg(keyMsg tea.KeyMsg, msg tea.Msg) (tea.Model, tea.Cmd)
case key.Matches(keyMsg, DefaultKeyMap.Back):
return m, navstack.PopNavigationCmd()
case key.Matches(keyMsg, DefaultKeyMap.Select):
choice, ok := m.List.SelectedItem().(choiceItem)
choice, ok := m.list.SelectedItem().(choiceItem)
if ok {
return m.SelectChoice(choice.key)
}
default:
l, cmd := m.List.Update(msg)
m.List = l
l, cmd := m.list.Update(msg)
m.list = l
return m, cmd
}

Expand Down
40 changes: 20 additions & 20 deletions menu/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ type MenuStyles struct {

type Model struct {
Choices []Choice
List list.Model

delegate list.DefaultDelegate
list list.Model
width int
height int
help.KeyMap
Expand All @@ -64,31 +64,31 @@ func New(title string, choices []Choice, selected *Choice) Model {
defaultHeight := 20

model := Model{
List: list.New([]list.Item{}, delegation, defaultWidth, defaultHeight),
list: list.New([]list.Item{}, delegation, defaultWidth, defaultHeight),
delegate: delegation,
keys: DefaultKeyMap,
help: help.New(),
width: defaultWidth,
height: defaultHeight,
}

model.List.Styles.Title = styles.ListTitleStyle
model.List.Title = title
model.List.SetShowPagination(true)
model.List.SetShowTitle(true)
model.List.SetFilteringEnabled(false)
model.List.SetShowFilter(false)
model.List.SetShowStatusBar(false)
model.List.SetShowHelp(false)
model.list.Styles.Title = styles.ListTitleStyle
model.list.Title = title
model.list.SetShowPagination(true)
model.list.SetShowTitle(true)
model.list.SetFilteringEnabled(false)
model.list.SetShowFilter(false)
model.list.SetShowStatusBar(false)
model.list.SetShowHelp(false)

chooseKeyBinding := key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "choose"),
)
model.List.AdditionalFullHelpKeys = func() []key.Binding {
model.list.AdditionalFullHelpKeys = func() []key.Binding {
return []key.Binding{chooseKeyBinding}
}
model.List.AdditionalShortHelpKeys = func() []key.Binding {
model.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{chooseKeyBinding}
}

Expand All @@ -113,16 +113,16 @@ func (m *Model) SetChoices(choices []Choice, selected *Choice) {
items[i] = choiceItem{title: choice.Title, desc: choice.Description, key: choice}
}

m.List.SetItems(items)
m.list.SetItems(items)
if selected != nil {
m.List.Select(selectedIndex)
m.list.Select(selectedIndex)
}
}

// SetStyles allows you to customize the styles used by the menu. This is mostly a passthrough
// to the bubble/list component used by the menu.
func (m Model) SetStyles(s MenuStyles) {
m.List.Styles.Title = s.ListTitleStyle
m.list.Styles.Title = s.ListTitleStyle
m.delegate.Styles = s.ListItemStyles
}

Expand All @@ -137,7 +137,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

// No selection made yet so update the list
var cmd tea.Cmd
m.List, cmd = m.List.Update(msg)
m.list, cmd = m.list.Update(msg)
return m, cmd
}

Expand All @@ -153,26 +153,26 @@ func (m Model) SelectChoice(choice Choice) (Model, tea.Cmd) {
func (m *Model) SetSize(w tea.WindowSizeMsg) {
m.width = w.Width
m.height = w.Height
m.List.SetSize(w.Width, w.Height)
m.list.SetSize(w.Width, w.Height)
m.help.Width = w.Width
}

func (m *Model) SetShowTitle(display bool) {
m.List.SetShowTitle(display)
m.list.SetShowTitle(display)
}

// View renders the menu. When no choices are present, nothing is rendered.
func (m Model) View() string {
var help string
if m.help.ShowAll {
height := m.height - 5
m.List.SetSize(m.width, height)
m.list.SetSize(m.width, height)
help = styles.HelpStyle.Render(m.help.View(m.keys))
}

// display menu if choices are present.
if len(m.Choices) > 0 {
return "\n" + m.List.View() + help
return "\n" + m.list.View() + help
}

return ""
Expand Down

0 comments on commit de89207

Please sign in to comment.