60 lines
1 KiB
Go
60 lines
1 KiB
Go
|
|
package fields
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"github.com/charmbracelet/bubbles/cursor"
|
||
|
|
"github.com/charmbracelet/bubbles/textinput"
|
||
|
|
tea "github.com/charmbracelet/bubbletea"
|
||
|
|
)
|
||
|
|
|
||
|
|
type InputModel struct {
|
||
|
|
label string
|
||
|
|
input textinput.Model
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewInputModel(label, value string) *InputModel {
|
||
|
|
t := textinput.New()
|
||
|
|
t.SetValue(value)
|
||
|
|
t.Cursor.SetMode(cursor.CursorBlink)
|
||
|
|
return &InputModel{
|
||
|
|
label: label,
|
||
|
|
input: t,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *InputModel) Init() tea.Cmd {
|
||
|
|
return textinput.Blink
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *InputModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
|
||
|
|
var cmd tea.Cmd
|
||
|
|
m.input, cmd = m.input.Update(msg)
|
||
|
|
return m, cmd
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *InputModel) View() string {
|
||
|
|
res := fmt.Sprintf("%s: %v", m.label, m.input.View())
|
||
|
|
return res
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *InputModel) Focus() {
|
||
|
|
m.input.Focus()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *InputModel) Blur() {
|
||
|
|
m.input.Blur()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *InputModel) CaptureKeys() bool {
|
||
|
|
//return m.input.Focused()
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *InputModel) GetLabel() string {
|
||
|
|
return m.label
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *InputModel) GetValue() interface{} {
|
||
|
|
return m.input.Value()
|
||
|
|
}
|