1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
package input
import (
"strings"
"unicode"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/cqroot/prompt/constants"
)
type Model struct {
df string
textInput textinput.Model
validateFunc ValidateFunc
inputMode InputMode
quitting bool
err error
keys KeyMap
showHelp bool
help help.Model
teaProgramOpts []tea.ProgramOption
}
func New(defaultValue string, opts ...Option) *Model {
ti := textinput.New()
ti.Placeholder = defaultValue
ti.Focus()
ti.CharLimit = 156
ti.Width = 40
ti.Prompt = ""
m := &Model{
textInput: ti,
df: defaultValue,
inputMode: InputAll,
quitting: false,
err: nil,
keys: DefaultKeyMap,
showHelp: false,
help: help.New(),
teaProgramOpts: make([]tea.ProgramOption, 0),
}
for _, opt := range opts {
opt(m)
}
return m
}
func (m Model) Data() string {
if m.textInput.Value() == "" {
return m.textInput.Placeholder
} else {
return m.textInput.Value()
}
}
func (m Model) DataString() string {
if m.textInput.EchoMode == EchoNormal {
return m.Data()
}
m.textInput.Blur()
str := m.textInput.View()
m.textInput.Focus()
return str
}
func (m Model) Quitting() bool {
return m.quitting
}
func (m Model) Error() error {
return m.err
}
func (m Model) TeaProgramOpts() []tea.ProgramOption {
return m.teaProgramOpts
}
func (m *Model) WithInputMode(mode InputMode) *Model {
m.inputMode = mode
return m
}
func (m *Model) WithEchoMode(mode EchoMode) *Model {
m.textInput.EchoMode = mode
return m
}
func (m *Model) WithValidateFunc(vf ValidateFunc) *Model {
m.validateFunc = vf
return m
}
func (m Model) Init() tea.Cmd {
return textinput.Blink
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.help.Width = msg.Width
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.Confirm):
if m.err == nil && m.validateFunc != nil {
currVal := m.textInput.Value()
if currVal == "" {
currVal = m.textInput.Placeholder
}
m.err = m.validateFunc(currVal)
}
m.quitting = true
return m, tea.Quit
case key.Matches(msg, m.keys.Quit):
m.quitting = true
m.err = constants.ErrUserQuit
return m, tea.Quit
}
if m.inputMode == InputNumber || m.inputMode == InputInteger {
keypress := msg.String()
if len(keypress) == 1 {
if keypress == "." {
if m.inputMode != InputNumber ||
strings.Contains(m.textInput.Value(), ".") {
return m, nil
}
} else {
if !unicode.IsNumber([]rune(keypress)[0]) {
return m, nil
}
}
}
}
}
var cmd tea.Cmd
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}
func (m Model) View() string {
view := m.textInput.View()
if m.textInput.Value() != "" && m.validateFunc != nil {
err := m.validateFunc(m.textInput.Value())
if err != nil {
view = view + constants.DefaultErrorPromptPrefixStyle.Render("\n✖ ") +
constants.DefaultNoteStyle.Render(err.Error())
}
}
if m.showHelp {
view += "\n\n"
view += m.help.View(m.keys)
}
return view
}
|