GoSetup-bubble/setup/fields/select.go

127 lines
2.2 KiB
Go
Raw Normal View History

2025-12-26 17:45:37 +05:00
package fields
import (
"fmt"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
type SelectModel struct {
label string
description string
options []string
focus int
open bool
chosen string
2025-12-26 17:45:37 +05:00
}
func NewSelectModel(label, value, description string, options []string) *SelectModel {
2025-12-26 17:45:37 +05:00
found := false
for i := range options {
if options[i] == value {
found = true
}
}
if !found {
value = ""
}
return &SelectModel{
options: options,
label: label,
description: description,
focus: len(options),
chosen: value,
2025-12-26 17:45:37 +05:00
}
}
func (m *SelectModel) Init() tea.Cmd {
return textinput.Blink
}
func (m *SelectModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
if m.open {
switch msg.String() {
case "esc":
m.open = false
case "up":
m.focus--
if m.focus < 0 {
m.focus = len(m.options) - 1
}
case "down":
m.focus++
if m.focus == len(m.options) {
m.focus = 0
}
case "enter":
if len(m.options) == 0 || m.focus == len(m.options) {
return m, nil
}
m.chosen = m.options[m.focus]
m.open = false
m.focus = len(m.options)
return m, nil
2025-12-26 17:45:37 +05:00
}
} else {
switch msg.String() {
case "enter":
m.open = true
for optionIdx := range m.options {
if m.options[optionIdx] == m.chosen {
m.focus = optionIdx
}
}
if m.chosen == "" && len(m.options) > 0 {
m.focus = 0
}
}
}
}
return m, cmd
}
func (m *SelectModel) View() string {
res := fmt.Sprintf("%s: %s", m.label, m.chosen)
if m.description != "" {
res += fmt.Sprintf("\n%s", m.description)
}
2025-12-26 17:45:37 +05:00
if m.open && len(m.options) > 0 {
for i := range m.options {
if m.focus == i {
res += "\n\t-> " + m.options[i]
} else {
res += "\n\t " + m.options[i]
}
}
}
return res
}
func (m *SelectModel) Close() {
m.open = false
}
func (m *SelectModel) Focus() {
}
func (m *SelectModel) Blur() {
m.open = false
}
func (m *SelectModel) CaptureKeys() bool {
return m.open
}
func (m *SelectModel) GetLabel() string {
return m.label
}
func (m *SelectModel) GetValue() interface{} {
return m.chosen
}