diff --git a/libs/cmdio/pager.go b/libs/cmdio/pager.go index 7aff1608486..7462c12adc6 100644 --- a/libs/cmdio/pager.go +++ b/libs/cmdio/pager.go @@ -9,6 +9,7 @@ import ( bubblespinner "github.com/charmbracelet/bubbles/spinner" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "github.com/databricks/cli/libs/cmdio/teainput" "github.com/databricks/databricks-sdk-go/listing" ) @@ -180,30 +181,25 @@ func (m *pagerModel[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, printCmd } - case tea.KeyMsg: + } + + if in, ok := teainput.Decode(msg); ok && !in.Paste { if m.iterDone { return m, nil } - return m.handleKey(msg) + return m.handleKey(in.Key) } return m, nil } -func (m *pagerModel[T]) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) { - switch msg.Type { //nolint:exhaustive // the pager only cares about a few keys - case tea.KeyEnter: +func (m *pagerModel[T]) handleKey(key string) (tea.Model, tea.Cmd) { + switch key { + case "enter": return m, m.startDrain() - case tea.KeyEsc, tea.KeyCtrlC: + case "esc", "ctrl+c", "q", "Q": return m, tea.Quit - case tea.KeySpace: + case "space", "alt+space": // Alt+Space has always advanced the pager. return m, m.startAdvance() - case tea.KeyRunes: - switch msg.String() { - case " ": - return m, m.startAdvance() - case "q", "Q": - return m, tea.Quit - } } return m, nil } diff --git a/libs/cmdio/prompt.go b/libs/cmdio/prompt.go index ce0fc674872..6f309db2601 100644 --- a/libs/cmdio/prompt.go +++ b/libs/cmdio/prompt.go @@ -7,6 +7,7 @@ import ( "strings" tea "github.com/charmbracelet/bubbletea" + "github.com/databricks/cli/libs/cmdio/teainput" ) // Glyphs drawn into the prompt's rendered output. cursorBlock stands in for @@ -117,17 +118,21 @@ func (m *promptModel) glyph() string { func (m *promptModel) Init() tea.Cmd { return nil } func (m *promptModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - key, ok := msg.(tea.KeyMsg) + in, ok := teainput.Decode(msg) if !ok { return m, nil } + if in.Paste { + m.insert([]rune(in.Text)) + return m, nil + } - switch key.Type { - case tea.KeyCtrlC: + switch in.Key { + case "ctrl+c": m.cancelled = true return m, tea.Quit - case tea.KeyEnter, tea.KeyCtrlJ: + case "enter", "ctrl+j": // Enter sends CR, Ctrl+J sends LF; treat both as submit. m.touched = true if m.validate != nil { @@ -139,7 +144,7 @@ func (m *promptModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.submitted = true return m, tea.Quit - case tea.KeyDelete, tea.KeyCtrlD: + case "delete", "ctrl+d": // Delete and Ctrl+D both exit the prompt with EOF, even on a // non-empty buffer. Surprising at first glance, but it matches the // previous prompt library and is pinned by a baseline test — @@ -147,23 +152,20 @@ func (m *promptModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.deleted = true return m, tea.Quit - case tea.KeyLeft, tea.KeyCtrlB: + case "left", "ctrl+b": if m.cursor > 0 { m.cursor-- } - case tea.KeyRight, tea.KeyCtrlF: + case "right", "ctrl+f": if m.cursor < len(m.runes) { m.cursor++ } - case tea.KeyBackspace, tea.KeyCtrlH: + case "backspace", "ctrl+h": // Backspace sends DEL, Ctrl+H sends BS; treat both as backspace. - // Alt+Backspace (the word-delete combo) is dropped rather than - // falling through as a plain backspace. - if key.Alt { - return m, nil - } + // Alt+Backspace (the word-delete combo) is named "alt+backspace" and + // falls to the default branch rather than acting as a plain backspace. if m.cursor == 0 { return m, nil } @@ -171,39 +173,33 @@ func (m *promptModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.cursor-- m.submitErr = nil - case tea.KeyRunes, tea.KeySpace: - // Alt+ (e.g. Alt+f, Alt+b) are word-nav combos we do not - // support. Drop them rather than inserting the rune literally — - // the baseline tests pin this as a no-op, and silently typing - // "f" when the user pressed Alt+f would be a regression. - if key.Alt { - return m, nil - } - typed := key.Runes - if key.Type == tea.KeySpace { - typed = []rune{' '} - } - // Some terminals send an empty bracketed paste when the clipboard is - // empty. Nothing changes, so it must not count as an edit or clear the - // error line. - if len(typed) == 0 { - return m, nil - } - tail := append([]rune{}, m.runes[m.cursor:]...) - m.runes = append(m.runes[:m.cursor], typed...) - m.runes = append(m.runes, tail...) - m.cursor += len(typed) - m.touched = true - m.submitErr = nil - default: - // All other key types are intentionally inert (Home/End, - // Ctrl+W/Ctrl+U, Ctrl+P/N, function keys, etc.). + // Printable input carries Text. Alt+ (e.g. Alt+f, Alt+b) are + // word-nav combos we do not support and carry no Text, so they are + // dropped rather than inserting the rune literally — the baseline + // tests pin this as a no-op. All other keys (Home/End, Ctrl+W/Ctrl+U, + // Ctrl+P/N, function keys, etc.) are intentionally inert too. + m.insert([]rune(in.Text)) } return m, nil } +// insert types runes at the cursor. Some terminals send an empty bracketed +// paste when the clipboard is empty; nothing changes, so it must not count as +// an edit or clear the error line. +func (m *promptModel) insert(typed []rune) { + if len(typed) == 0 { + return + } + tail := append([]rune{}, m.runes[m.cursor:]...) + m.runes = append(m.runes[:m.cursor], typed...) + m.runes = append(m.runes, tail...) + m.cursor += len(typed) + m.touched = true + m.submitErr = nil +} + func (m *promptModel) View() string { return m.render() } func (m *promptModel) render() string { diff --git a/libs/cmdio/select.go b/libs/cmdio/select.go index 7d888de7b27..a575a7eeb04 100644 --- a/libs/cmdio/select.go +++ b/libs/cmdio/select.go @@ -10,6 +10,7 @@ import ( "unicode/utf8" tea "github.com/charmbracelet/bubbletea" + "github.com/databricks/cli/libs/cmdio/teainput" ) const ( @@ -248,16 +249,16 @@ func parsePromptTemplate(funcs template.FuncMap, name, src string) (*template.Te func (m *selectModel) Init() tea.Cmd { return tea.HideCursor } func (m *selectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - key, ok := msg.(tea.KeyMsg) + in, ok := teainput.Decode(msg) if !ok { return m, nil } - switch key.Type { - case tea.KeyCtrlC: + switch in.Key { + case "ctrl+c": m.cancelled = true return m, tea.Quit - case tea.KeyEnter, tea.KeyCtrlJ: + case "enter", "ctrl+j": // Enter on an empty filtered list is intentionally inert; only // Ctrl+C escapes from a "No results" panel. if len(m.matches) == 0 { @@ -266,25 +267,22 @@ func (m *selectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.submitted = true return m, tea.Quit - case tea.KeyUp, tea.KeyCtrlP: + case "up", "ctrl+p": m.cursorUp() - case tea.KeyDown, tea.KeyCtrlN: + case "down", "ctrl+n": m.cursorDown() - case tea.KeyLeft, tea.KeyCtrlB: + case "left", "ctrl+b": m.pageUp() - case tea.KeyRight, tea.KeyCtrlF: + case "right", "ctrl+f": m.pageDown() - case tea.KeyBackspace, tea.KeyCtrlH: + case "backspace", "ctrl+h": // Backspace sends DEL, Ctrl+H sends BS; treat both as filter - // backspace. Alt+Backspace (word-delete) is dropped rather than - // deleting a single rune. - if key.Alt { - return m, nil - } + // backspace. Alt+Backspace (word-delete) is named "alt+backspace" + // and falls to the default branch rather than deleting a rune. if !m.searchActive || m.filter == "" { return m, nil } @@ -293,67 +291,65 @@ func (m *selectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } m.recomputeMatches() - case tea.KeyTab, tea.KeyRunes, tea.KeySpace: - // Alt+ are word-nav combos; drop them rather than letting - // the rune sneak into the filter buffer. The baseline tests pin - // this as a no-op for parity with the previous prompt library. - if key.Alt { + case "tab": + if m.searchActive { + m.appendFilter("\t") + } + + case "esc": + // Esc is intentionally inert. + + default: + // Alt+ are word-nav combos and carry no Text, so they can't + // sneak into the filter buffer. The baseline tests pin this as a + // no-op for parity with the previous prompt library. Other keys + // (Home/End, Ctrl+U/W, function keys, …) carry no Text either. + if in.Text == "" { return m, nil } if !m.searchActive { // Outside search mode, vim-style shortcuts navigate the list and // "/" toggles search when a Searcher is configured. Anything - // else is dropped. Multiple runes can arrive in a single KeyMsg + // else is dropped. Multiple runes can arrive in a single event // when the user types quickly, so dispatch per-rune. - if key.Type == tea.KeyRunes { - for _, r := range key.Runes { - switch r { - case 'j': - m.cursorDown() - case 'k': - m.cursorUp() - case 'l': - m.pageDown() - case 'h': - m.pageUp() - case '/': - if m.searcher != nil { - m.searchActive = true - } + for _, r := range in.Text { + switch r { + case 'j': + m.cursorDown() + case 'k': + m.cursorUp() + case 'l': + m.pageDown() + case 'h': + m.pageUp() + case '/': + if m.searcher != nil { + m.searchActive = true } } } return m, nil } - switch key.Type { - case tea.KeyTab: - m.filter += "\t" - case tea.KeySpace: - m.filter += " " - default: - // "/" toggles search off: clear the filter and exit search - // mode. Any other runes batched in the same KeyMsg are dropped - // — when the user types "/" the surrounding characters almost - // certainly belong to a toggle, not the filter. - if slices.Contains(key.Runes, '/') { - m.filter = "" - m.searchActive = false - m.recomputeMatches() - return m, nil - } - m.filter += string(key.Runes) + // "/" toggles search off: clear the filter and exit search mode. + // Any other runes batched in the same event are dropped — when the + // user types "/" the surrounding characters almost certainly belong + // to a toggle, not the filter. + if strings.ContainsRune(in.Text, '/') { + m.filter = "" + m.searchActive = false + m.recomputeMatches() + return m, nil } - m.recomputeMatches() - - case tea.KeyEsc: - // Esc is intentionally inert. - - default: - // Other key types (Home/End, Ctrl+U/W, function keys, …) are no-ops. + m.appendFilter(in.Text) } return m, nil } +func (m *selectModel) appendFilter(s string) { + m.filter += s + m.recomputeMatches() +} + func (m *selectModel) View() string { return m.render() } func (m *selectModel) render() string { diff --git a/libs/cmdio/teainput/press.go b/libs/cmdio/teainput/press.go new file mode 100644 index 00000000000..aedd2e2c4e3 --- /dev/null +++ b/libs/cmdio/teainput/press.go @@ -0,0 +1,49 @@ +package teainput + +import ( + "strings" + + tea "github.com/charmbracelet/bubbletea" +) + +// v1KeyTypes maps the keystroke names used by [Event.Key] to v1 key types. +var v1KeyTypes = func() map[string]tea.KeyType { + m := map[string]tea.KeyType{} + // v1 key types are control codes (0..127) and negative special keys. + for k := tea.KeyType(-128); k < 128; k++ { + if name := k.String(); name != "" && name != " " { + m[name] = k + } + } + m["space"] = tea.KeySpace + return m +}() + +// Press builds the message for a key press, named like [Event.Key] +// (e.g. "enter", "ctrl+c", "alt+backspace", "space", "q"). It exists so model +// tests don't construct version-specific key messages. +func Press(key string) tea.Msg { + alt := false + if rest, ok := strings.CutPrefix(key, "alt+"); ok && rest != "" { + alt, key = true, rest + } + if t, ok := v1KeyTypes[key]; ok { + msg := tea.KeyMsg{Type: t, Alt: alt} + if t == tea.KeySpace { + msg.Runes = []rune{' '} + } + return msg + } + return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(key), Alt: alt} +} + +// Type builds the message for typing text as a single event, like a burst of +// fast typing. +func Type(text string) tea.Msg { + return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(text)} +} + +// Paste builds the message for a bracketed paste of text. +func Paste(text string) tea.Msg { + return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(text), Paste: true} +} diff --git a/libs/cmdio/teainput/press_test.go b/libs/cmdio/teainput/press_test.go new file mode 100644 index 00000000000..b8811931243 --- /dev/null +++ b/libs/cmdio/teainput/press_test.go @@ -0,0 +1,25 @@ +package teainput_test + +import ( + "testing" + + "github.com/databricks/cli/libs/cmdio/teainput" + "github.com/stretchr/testify/assert" +) + +func TestPressRoundTrips(t *testing.T) { + for _, key := range []string{ + "enter", "ctrl+c", "ctrl+j", "ctrl+h", "backspace", "alt+backspace", + "esc", "tab", "alt+tab", "shift+tab", "delete", "up", "down", "left", "right", + "space", "alt+space", "q", "Q", "alt+f", "ctrl+n", "ctrl+p", + } { + ev, ok := teainput.Decode(teainput.Press(key)) + assert.True(t, ok) + assert.Equal(t, key, ev.Key) + } + + ev, _ := teainput.Decode(teainput.Type("abc")) + assert.Equal(t, teainput.Event{Text: "abc"}, ev) + ev, _ = teainput.Decode(teainput.Paste("x/y")) + assert.Equal(t, teainput.Event{Text: "x/y", Paste: true}, ev) +} diff --git a/libs/cmdio/teainput/teainput.go b/libs/cmdio/teainput/teainput.go new file mode 100644 index 00000000000..ea2dcebeac0 --- /dev/null +++ b/libs/cmdio/teainput/teainput.go @@ -0,0 +1,82 @@ +// Package teainput decodes Bubble Tea input messages into one shape for all +// TUI models in the CLI. +// +// The shape follows Bubble Tea v2: keys are matched by their keystroke name +// ("enter", "ctrl+c", "space", "alt+backspace", "a") and printable input is +// carried as text, with bracketed paste as a separate event. Models written +// against [Event] don't depend on the version-specific key API, so moving to +// v2 only changes this package. +package teainput + +import ( + "strings" + + tea "github.com/charmbracelet/bubbletea" +) + +// Event is a decoded key press or paste. +type Event struct { + // Key is the keystroke name, e.g. "enter", "ctrl+c", "space", + // "alt+backspace" or "a". It is empty when the message carries only text, + // which is a paste, or several runes that arrived in one read. + Key string + + // Text is the printable text the event inserts. It is empty for + // non-printable keys and for Alt-modified runes. + Text string + + // Paste is set when Text came from a bracketed paste. + Paste bool +} + +// Decode returns the [Event] for a key press or paste message, and false for +// every other message. +// +// Alt is kept in the name only where the models have always treated it as a +// different key (Alt+rune, Alt+space, Alt+backspace, Alt+tab). Other Alt +// combinations act as the plain key, as they did before the decoder existed. +func Decode(msg tea.Msg) (Event, bool) { + k, ok := msg.(tea.KeyMsg) + if !ok { + return Event{}, false + } + if k.Paste { + return Event{Text: string(k.Runes), Paste: true}, true + } + + switch k.Type { //nolint:exhaustive // only runes, space, backspace and tab carry Alt or text + case tea.KeyRunes: + if k.Alt { + if len(k.Runes) == 1 { + return Event{Key: "alt+" + string(k.Runes)}, true + } + return Event{}, true + } + // One rune is a key press. Several runes in one read are typed text + // (or a paste without brackets) and get no key name, so a burst that + // spells a key name, like "enter", is text and not that key. + if len(k.Runes) == 1 { + return printable(string(k.Runes)), true + } + return Event{Text: string(k.Runes)}, true + case tea.KeySpace: + if k.Alt { + return Event{Key: "alt+space"}, true + } + return printable(" "), true + case tea.KeyBackspace, tea.KeyTab: + if k.Alt { + return Event{Key: k.String()}, true // "alt+backspace", "alt+tab" + } + } + return Event{Key: strings.TrimPrefix(k.String(), "alt+")}, true +} + +// printable is the event for one typed rune: its key name is the rune, or +// "space" for a space. +func printable(s string) Event { + if s == " " { + return Event{Key: "space", Text: s} + } + return Event{Key: s, Text: s} +} diff --git a/libs/cmdio/teainput/teainput_test.go b/libs/cmdio/teainput/teainput_test.go new file mode 100644 index 00000000000..79514761591 --- /dev/null +++ b/libs/cmdio/teainput/teainput_test.go @@ -0,0 +1,59 @@ +package teainput_test + +import ( + "testing" + + tea "github.com/charmbracelet/bubbletea" + "github.com/databricks/cli/libs/cmdio/teainput" + "github.com/stretchr/testify/assert" +) + +func TestDecode(t *testing.T) { + cases := []struct { + name string + msg tea.Msg + want teainput.Event + }{ + {"rune", tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("a")}, teainput.Event{Key: "a", Text: "a"}}, + {"alt rune", tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("f"), Alt: true}, teainput.Event{Key: "alt+f"}}, + {"alt runes", tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("fb"), Alt: true}, teainput.Event{}}, + {"space", tea.KeyMsg{Type: tea.KeySpace, Runes: []rune(" ")}, teainput.Event{Key: "space", Text: " "}}, + {"space as rune", tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(" ")}, teainput.Event{Key: "space", Text: " "}}, + {"alt space", tea.KeyMsg{Type: tea.KeySpace, Runes: []rune(" "), Alt: true}, teainput.Event{Key: "alt+space"}}, + {"enter", tea.KeyMsg{Type: tea.KeyEnter}, teainput.Event{Key: "enter"}}, + {"ctrl+j", tea.KeyMsg{Type: tea.KeyCtrlJ}, teainput.Event{Key: "ctrl+j"}}, + {"ctrl+c", tea.KeyMsg{Type: tea.KeyCtrlC}, teainput.Event{Key: "ctrl+c"}}, + {"ctrl+h", tea.KeyMsg{Type: tea.KeyCtrlH}, teainput.Event{Key: "ctrl+h"}}, + {"backspace", tea.KeyMsg{Type: tea.KeyBackspace}, teainput.Event{Key: "backspace"}}, + {"alt backspace", tea.KeyMsg{Type: tea.KeyBackspace, Alt: true}, teainput.Event{Key: "alt+backspace"}}, + {"esc", tea.KeyMsg{Type: tea.KeyEsc}, teainput.Event{Key: "esc"}}, + {"tab", tea.KeyMsg{Type: tea.KeyTab}, teainput.Event{Key: "tab"}}, + {"alt tab", tea.KeyMsg{Type: tea.KeyTab, Alt: true}, teainput.Event{Key: "alt+tab"}}, + {"shift+tab", tea.KeyMsg{Type: tea.KeyShiftTab}, teainput.Event{Key: "shift+tab"}}, + {"delete", tea.KeyMsg{Type: tea.KeyDelete}, teainput.Event{Key: "delete"}}, + {"up", tea.KeyMsg{Type: tea.KeyUp}, teainput.Event{Key: "up"}}, + // Alt on keys the models always treated as plain keys is dropped. + {"alt enter", tea.KeyMsg{Type: tea.KeyEnter, Alt: true}, teainput.Event{Key: "enter"}}, + {"alt ctrl+c", tea.KeyMsg{Type: tea.KeyCtrlC, Alt: true}, teainput.Event{Key: "ctrl+c"}}, + {"alt left", tea.KeyMsg{Type: tea.KeyLeft, Alt: true}, teainput.Event{Key: "left"}}, + // Several runes in one read are text. They carry no key name, so a burst + // that spells a key name is typed, not pressed. + {"burst", tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("jk")}, teainput.Event{Text: "jk"}}, + {"burst enter", tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("enter")}, teainput.Event{Text: "enter"}}, + {"burst down", tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("down")}, teainput.Event{Text: "down"}}, + {"burst ctrl+c", tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("ctrl+c")}, teainput.Event{Text: "ctrl+c"}}, + {"paste", tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("a b"), Paste: true}, teainput.Event{Text: "a b", Paste: true}}, + {"paste enter", tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("enter"), Paste: true}, teainput.Event{Text: "enter", Paste: true}}, + {"empty paste", tea.KeyMsg{Type: tea.KeyRunes, Paste: true}, teainput.Event{Paste: true}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got, ok := teainput.Decode(tc.msg) + assert.True(t, ok) + assert.Equal(t, tc.want, got) + }) + } + + _, ok := teainput.Decode(tea.WindowSizeMsg{}) + assert.False(t, ok) +}