GoSetup-bubble/setup/fields/checkbox.go

70 lines
1.1 KiB
Go

package fields
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
)
type CheckboxModel struct {
Label string
description string
Checked bool
}
func NewCheckboxModel(label, description string, checked bool) *CheckboxModel {
return &CheckboxModel{
Label: label,
description: description,
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 += "[ ]"
}
if m.description != "" {
res += fmt.Sprintf("\n%s", m.description)
}
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
}