Revert from previous mistaken merge

This commit is contained in:
2025-12-11 14:56:45 +01:00
parent c2e15496da
commit 12fbb7a3a9
+65 -135
View File
@@ -6,17 +6,18 @@ import (
//"sort" //"sort"
//"strings" //"strings"
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
type item struct {
title string
description string
}
type item struct {
title string
description string
}
func (i item) Title() string { return i.title } func (i item) Title() string { return i.title }
func (i item) Description() string { return i.description } func (i item) Description() string { return i.description }
func (i item) FilterValue() string { return i.title } func (i item) FilterValue() string { return i.title }
@@ -24,59 +25,52 @@ func (i item) FilterValue() string { return i.title }
type view int type view int
type model struct { type model struct {
list list.Model list list.Model
commands CmdList commands CmdList
currentView view currentView view
currentKey string currentKey string
selectedCmd string selectedCmd string
categoryIndex int
} }
func (m model) Init() tea.Cmd { return nil } func (m model) Init() tea.Cmd { return nil }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "ctrl+c", "q": case "ctrl+c", "q":
return m, tea.Quit
case "enter":
selected, ok := m.list.SelectedItem().(item)
if ok {
if m.currentView == viewCategories {
m.categoryIndex = m.list.Index()
m.currentKey = selected.title
m.currentView = viewCommands
m.list.Title = m.currentKey
m.list.SetItems(cmdItemsToList(m.commands.Get(m.currentKey)))
m.list.Select(0)
} else if m.currentView == viewCommands {
m.selectedCmd = selected.title
return m, tea.Quit return m, tea.Quit
}
case "enter":
selected, ok := m.list.SelectedItem().(item)
if ok {
if m.currentView == viewCategories {
m.currentKey = selected.title
m.currentView = viewCommands
m.list.Title = m.currentKey
m.list.SetItems(cmdItemsToList(m.commands.Get(m.currentKey)))
} else if m.currentView == viewCommands {
m.selectedCmd = selected.title
return m, tea.Quit
}
}
case "b":
if m.currentView == viewCommands {
m.currentView = viewCategories
m.list.Title = "Choose a list of commands"
m.list.SetItems(cmdListKeysToList(m.commands))
}
} }
case "b": case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
if m.currentView == viewCommands { m.list.SetSize(msg.Width-h, msg.Height-v)
m.currentView = viewCategories
m.list.Title = "Choose a list of commands"
m.list.SetItems(cmdListKeysToList(m.commands))
m.list.Select(m.categoryIndex)
} else if m.currentView == viewCategories {
return m, tea.Quit
}
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
} }
@@ -90,9 +84,7 @@ func (m model) View() string {
return docStyle.Render(m.list.View()) return docStyle.Render(m.list.View())
} }
var docStyle = lipgloss.NewStyle(). var docStyle = lipgloss.NewStyle().Margin(1, 2)
Margin(1, 2).
Background(lipgloss.Color("235"))
const ( const (
viewCategories view = iota viewCategories view = iota
@@ -101,12 +93,25 @@ const (
func StartTui(commands CmdList) { func StartTui(commands CmdList) {
delegate := newStyledDelegate() delegate := list.NewDefaultDelegate()
backKey := key.NewBinding(
key.WithKeys("b"),
key.WithHelp("b", "back"),
)
delegate.ShortHelpFunc = func() []key.Binding{
return []key.Binding{
backKey,
}
}
delegate.FullHelpFunc = func() [][]key.Binding{
return [][]key.Binding{
{backKey},
}
}
items := cmdListKeysToList(commands) items := cmdListKeysToList(commands)
l := list.New(items, delegate, 0, 0) l := list.New(items, delegate, 0, 0)
l.Title = "Choose a list of commands" l.Title = "Choose a list of commands"
l.Styles = newListStyles()
m := model{ m := model{
list: l, list: l,
@@ -127,15 +132,15 @@ func StartTui(commands CmdList) {
} }
func cmdListKeysToList(cmds CmdList) []list.Item { func cmdListKeysToList(cmds CmdList) []list.Item {
items := []list.Item{} items := []list.Item{}
for _, group := range cmds { for _, group := range cmds {
listItem := item{ listItem := item{
title: group.Category, title: group.Category,
description: "", description: "",
} }
items = append(items, listItem) items = append(items, listItem)
} }
return items return items
} }
func cmdItemsToList(cmds []CmdItem) []list.Item { func cmdItemsToList(cmds []CmdItem) []list.Item {
@@ -144,85 +149,10 @@ func cmdItemsToList(cmds []CmdItem) []list.Item {
for _, command := range cmds { for _, command := range cmds {
listItem := item{ listItem := item{
title: command.CommandName, title: command.CommandName,
description: command.CommandDescription, description: command.CommandDescription,
} }
items = append(items, listItem) items = append(items, listItem)
} }
return items return items
} }
func newStyledDelegate() list.DefaultDelegate {
delegate := list.NewDefaultDelegate()
backKey := key.NewBinding(
key.WithKeys("b"),
key.WithHelp("b", "back/exit"),
)
delegate.ShortHelpFunc = func() []key.Binding {
return []key.Binding{backKey}
}
delegate.FullHelpFunc = func() [][]key.Binding {
return [][]key.Binding{
{backKey},
}
}
delegate.SetSpacing(0)
styles := list.NewDefaultItemStyles()
styles.NormalTitle = styles.NormalTitle.
Foreground(lipgloss.Color("110")).
PaddingLeft(1)
styles.NormalDesc = styles.NormalDesc.
Foreground(lipgloss.Color("247")).
PaddingLeft(2)
styles.SelectedTitle = lipgloss.NewStyle().
Foreground(lipgloss.Color("0")).
Background(lipgloss.Color("187")).
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("186")).
Padding(0, 1)
styles.SelectedDesc = styles.SelectedTitle.
Foreground(lipgloss.Color("232")).
Background(lipgloss.Color("230")).
Padding(0, 1)
styles.FilterMatch = styles.FilterMatch.
Foreground(lipgloss.Color("226"))
delegate.Styles = styles
return delegate
}
func newListStyles() list.Styles {
styles := list.DefaultStyles()
styles.Title = lipgloss.NewStyle().
BorderStyle(lipgloss.ThickBorder()).
BorderForeground(lipgloss.Color("180")).
Background(lipgloss.Color("23")).
Foreground(lipgloss.Color("230")).
Padding(0, 2)
styles.TitleBar = styles.TitleBar.MarginBottom(1)
styles.StatusBar = styles.StatusBar.
Background(lipgloss.Color("236")).
Foreground(lipgloss.Color("252")).
Padding(0, 1)
styles.StatusEmpty = styles.StatusEmpty.Foreground(lipgloss.Color("60"))
styles.PaginationStyle = styles.PaginationStyle.
Foreground(lipgloss.Color("244")).
PaddingLeft(4)
styles.HelpStyle = styles.HelpStyle.
MarginLeft(1).
Foreground(lipgloss.Color("244"))
styles.ActivePaginationDot = styles.ActivePaginationDot.
Foreground(lipgloss.Color("220"))
styles.InactivePaginationDot = styles.InactivePaginationDot.
Foreground(lipgloss.Color("238"))
return styles
}