GoSetup-bubble/setup/list.go

70 lines
1.3 KiB
Go
Raw Normal View History

2025-12-26 17:45:37 +05:00
package setup
import (
"bubble/setup/ui"
"bubble/types"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type ListModel struct {
options []string
focus int
}
func NewListModel(applications []types.PackageApplication) *ListModel {
options := make([]string, len(applications))
for i, app := range applications {
options[i] = app.Name
}
return &ListModel{
options: options,
}
}
func (m *ListModel) Init() tea.Cmd {
return nil
}
func (m *ListModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "up", "w", "shift+tab":
m.focus--
if m.focus < 0 {
m.focus = len(m.options) - 1
}
case "down", "s", "tab":
m.focus++
if m.focus == len(m.options) {
m.focus = 0
}
case "enter":
return m, func() tea.Msg {
return types.SelectedAppMsg{Index: m.focus}
}
}
}
return m, cmd
}
func (m *ListModel) View() string {
res := ""
listStyle := lipgloss.NewStyle().Padding(1, 1).Foreground(ui.ColorPurple)
for i := range m.options {
if i == m.focus {
res += "[x] " + m.options[i]
} else {
res += "[ ] " + m.options[i]
}
if i != len(m.options)-1 {
res += "\n"
}
}
return listStyle.Render(res)
}