Structured dialogs (fields, warnings, buttons with hotkeys, danger default on Cancel), multi-field input that types every key, and a scrollable list/text that fits small terminals (crutch #10). The stack owns all input while open; clicks outside the top modal are ignored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
144 lines
3.3 KiB
Go
144 lines
3.3 KiB
Go
package modal
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/x/ansi"
|
|
|
|
"vcom/internal/tui/keys"
|
|
"vcom/internal/tui/style"
|
|
)
|
|
|
|
// Field of an Input modal.
|
|
type Field struct {
|
|
Label string
|
|
Value string
|
|
Placeholder string
|
|
Secret bool
|
|
}
|
|
|
|
// Input is a form of one or more text fields (mkdir, rename, SSH host).
|
|
// Every typed key goes to the focused field untranslated ("q" is a letter
|
|
// here, U section 7). Enter submits ([]string of values), Esc cancels.
|
|
type Input struct {
|
|
Title string
|
|
Text string // optional line above the fields
|
|
// Validate is called on Enter; an error is shown and the modal stays.
|
|
Validate func(values []string) error
|
|
Width int
|
|
|
|
fields []textinput.Model
|
|
labels []string
|
|
focus int
|
|
err string
|
|
}
|
|
|
|
// NewInput builds an Input.
|
|
func NewInput(title string, fields ...Field) *Input {
|
|
in := &Input{Title: title}
|
|
for _, f := range fields {
|
|
ti := textinput.New()
|
|
ti.Prompt = ""
|
|
ti.SetValue(f.Value)
|
|
ti.Placeholder = f.Placeholder
|
|
ti.CursorEnd()
|
|
if f.Secret {
|
|
ti.EchoMode = textinput.EchoPassword
|
|
}
|
|
in.fields = append(in.fields, ti)
|
|
in.labels = append(in.labels, f.Label)
|
|
}
|
|
if len(in.fields) > 0 {
|
|
in.fields[0].Focus()
|
|
}
|
|
return in
|
|
}
|
|
|
|
func (in *Input) Context() keys.Context { return keys.CtxModalInput }
|
|
|
|
// Values returns the current field values.
|
|
func (in *Input) Values() []string {
|
|
out := make([]string, len(in.fields))
|
|
for i, f := range in.fields {
|
|
out[i] = f.Value()
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (in *Input) setFocus(i int) {
|
|
in.fields[in.focus].Blur()
|
|
in.focus = i
|
|
in.fields[in.focus].Focus()
|
|
}
|
|
|
|
func (in *Input) Key(a keys.ActionID, k tea.KeyMsg) (Outcome, tea.Cmd) {
|
|
n := len(in.fields)
|
|
switch a {
|
|
case keys.ModalConfirm:
|
|
vals := in.Values()
|
|
if in.Validate != nil {
|
|
if err := in.Validate(vals); err != nil {
|
|
in.err = err.Error()
|
|
return Keep, nil
|
|
}
|
|
}
|
|
return Done(vals), nil
|
|
case keys.ModalCancel:
|
|
return Cancel(), nil
|
|
case keys.ModalNext:
|
|
if n > 1 {
|
|
in.setFocus((in.focus + 1) % n)
|
|
}
|
|
return Keep, nil
|
|
case keys.ModalPrev:
|
|
if n > 1 {
|
|
in.setFocus((in.focus + n - 1) % n)
|
|
}
|
|
return Keep, nil
|
|
case keys.ModalClear:
|
|
if n > 0 {
|
|
in.fields[in.focus].SetValue("")
|
|
}
|
|
return Keep, nil
|
|
}
|
|
if n == 0 {
|
|
return Keep, nil
|
|
}
|
|
in.err = ""
|
|
var cmd tea.Cmd
|
|
in.fields[in.focus], cmd = in.fields[in.focus].Update(k)
|
|
return Keep, cmd
|
|
}
|
|
|
|
func (in *Input) View(maxW, maxH int, st *style.Styles) string {
|
|
want := in.Width
|
|
if want == 0 {
|
|
want = 56
|
|
}
|
|
cw := innerWidth(maxW, want)
|
|
var lines []string
|
|
if in.Text != "" {
|
|
lines = append(lines, strings.Split(ansi.Wrap(in.Text, cw, ""), "\n")...)
|
|
}
|
|
lw := 0
|
|
for _, l := range in.labels {
|
|
lw = max(lw, ansi.StringWidth(l))
|
|
}
|
|
for i := range in.fields {
|
|
f := &in.fields[i]
|
|
f.Width = max(4, cw-lw-3)
|
|
label := st.ModalLabel.Render(in.labels[i] + strings.Repeat(" ", lw-ansi.StringWidth(in.labels[i])) + " ")
|
|
lines = append(lines, ansi.Truncate(label+"▏"+f.View(), cw, ""))
|
|
}
|
|
if in.err != "" {
|
|
lines = append(lines, st.NoteError.Render(ansi.Truncate(in.err, cw, "…")))
|
|
}
|
|
lines, _, _ = clip(lines, in.focus, max(1, maxH-3))
|
|
return frame(st, in.Title, lines, maxW)
|
|
}
|
|
|
|
func (in *Input) Mouse(m tea.MouseMsg, x, y, w, h int, st *style.Styles) (Outcome, tea.Cmd) {
|
|
return Keep, nil
|
|
}
|