GoSetup-bubble/setup/fields/checkbox.go

65 lines
950 B
Go
Raw Normal View History

2025-12-26 17:45:37 +05:00
package fields
import (
tea "github.com/charmbracelet/bubbletea"
)
type CheckboxModel struct {
Label string
Checked bool
}
func NewCheckboxModel(label string, checked bool) *CheckboxModel {
return &CheckboxModel{
Label: label,
Checked: checked,
}
}
func (m *CheckboxModel) Init() tea.Cmd {
return nil
}
func (m *CheckboxModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case " ", "enter":
m.Checked = !m.Checked
}
}
return m, nil
}
func (m *CheckboxModel) View() string {
res := ""
res += m.Label + ": "
if m.Checked {
res += "[x]"
} else {
res += "[ ]"
}
return res
}
func (m *CheckboxModel) Focus() {
}
func (m *CheckboxModel) Blur() {
}
func (m *CheckboxModel) CaptureKeys() bool {
return false
}
func (m *CheckboxModel) GetLabel() string {
return m.Label
}
func (m *CheckboxModel) GetValue() interface{} {
return m.Checked
}