Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions libs/cmdio/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
76 changes: 36 additions & 40 deletions libs/cmdio/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -139,71 +144,62 @@ 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 —
// changing it would silently shift caller behavior.
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
}
m.runes = append(m.runes[:m.cursor-1], m.runes[m.cursor:]...)
m.cursor--
m.submitErr = nil

case tea.KeyRunes, tea.KeySpace:
// Alt+<rune> (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+<rune> (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 {
Expand Down
114 changes: 55 additions & 59 deletions libs/cmdio/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"unicode/utf8"

tea "github.com/charmbracelet/bubbletea"
"github.com/databricks/cli/libs/cmdio/teainput"
)

const (
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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+<rune> 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+<rune> 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 {
Expand Down
Loading