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
1 change: 1 addition & 0 deletions .nextchanges/cli/alt-keys-inert.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Alt with a key that isn't a letter (Alt+Enter, Alt+arrows, Alt+Space) no longer acts as that key in prompts, selection lists and the pager. Alt+Enter used to submit and Alt+Left used to move the cursor. ([#7741](https://github.com/databricks/cli/pull/7741))
1 change: 1 addition & 0 deletions .nextchanges/cli/paste-as-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Selection lists treat pasted text as typed search text, and ignore it outside search. Pasting `j` or `/` no longer moves the cursor or opens search. The table view's search no longer inserts `alt+f` for Alt+F or `[text]` for a paste. ([#7741](https://github.com/databricks/cli/pull/7741))
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Value: ab
✔ Value: abc█
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
value: "ab"
value: "abc"
err: <nil>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
✔ Value: abX█
✔ Value: abcX█
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
value: "abXc"
value: "abcX"
err: <nil>
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@

Search: █
? Pick one:
alpha
beta (id-02)
gamma
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ item-20 ready


20 rows
/ italt+f[em-1]█
/ item-1█
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ item-20 ready



20 rows | no matches | ←→↑↓ scroll | g/G top/bottom | / search | q quit | 100%
20 rows | match 1/10 | n/N next/prev | ←→↑↓ scroll | g/G top/bottom | / search | q quit | 100%
2 changes: 1 addition & 1 deletion libs/cmdio/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (m *pagerModel[T]) handleKey(key string) (tea.Model, tea.Cmd) {
return m, m.startDrain()
case "esc", "ctrl+c", "q", "Q":
return m, tea.Quit
case "space", "alt+space": // Alt+Space has always advanced the pager.
case "space":
return m, m.startAdvance()
}
return m, nil
Expand Down
9 changes: 9 additions & 0 deletions libs/cmdio/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ func (m *selectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if !ok {
return m, nil
}
if in.Paste {
// A paste is literal filter text: it doesn't navigate or toggle
// search the way typed "j", "k" or "/" do.
if m.searchActive && in.Text != "" {
m.appendFilter(in.Text)
}
return m, nil
}

switch in.Key {
case "ctrl+c":
m.cancelled = true
Expand Down
31 changes: 31 additions & 0 deletions libs/cmdio/select_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmdio

import (
"strings"
"testing"

"github.com/databricks/cli/libs/cmdio/teainput"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -39,3 +41,32 @@ func TestRunSelectNoColor(t *testing.T) {
assert.Contains(t, out, ansiBold)
assert.Contains(t, out, ansiUnderline)
}

// A paste is text, not keys: outside search it neither moves the cursor nor
// opens search, and inside search it is appended literally, "/" included.
func TestSelectPaste(t *testing.T) {
opts := SelectOptions{
Label: "Pick",
Items: []Tuple{{"alpha", "1"}, {"beta", "2"}, {"gamma", "3"}},
Searcher: func(input string, idx int) bool {
return strings.Contains(strings.ToLower(input), "a")
},
}
m, err := newSelectModel(opts, false)
require.NoError(t, err)

next, _ := m.Update(teainput.Paste("j/"))
m = next.(*selectModel)
assert.Equal(t, 0, m.cursor, "a pasted j must not move the cursor")
assert.False(t, m.searchActive, "a pasted / must not open search")
assert.Empty(t, m.filter)

next, _ = m.Update(teainput.Press("/"))
m = next.(*selectModel)
require.True(t, m.searchActive)

next, _ = m.Update(teainput.Paste("b/x"))
m = next.(*selectModel)
assert.True(t, m.searchActive, "a pasted / inside search is literal")
assert.Equal(t, "b/x", m.filter)
}
2 changes: 1 addition & 1 deletion libs/cmdio/teainput/press_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestPressRoundTrips(t *testing.T) {
for _, key := range []string{
"enter", "ctrl+c", "ctrl+j", "ctrl+h", "backspace", "alt+backspace",
"enter", "alt+enter", "ctrl+c", "alt+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",
} {
Expand Down
11 changes: 4 additions & 7 deletions libs/cmdio/teainput/teainput.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
package teainput

import (
"strings"

tea "github.com/charmbracelet/bubbletea"
)

Expand All @@ -32,9 +30,8 @@ type Event struct {
// 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.
// Alt stays in the name, so Alt+Enter is "alt+enter" and no model matches it
// unless it names it. Alt+rune and Alt+space carry no text.
func Decode(msg tea.Msg) (Event, bool) {
k, ok := msg.(tea.KeyMsg)
if !ok {
Expand Down Expand Up @@ -66,10 +63,10 @@ func Decode(msg tea.Msg) (Event, bool) {
return printable(" "), true
case tea.KeyBackspace, tea.KeyTab:
if k.Alt {
return Event{Key: k.String()}, true // "alt+backspace", "alt+tab"
return Event{Key: k.String()}, true
}
}
return Event{Key: strings.TrimPrefix(k.String(), "alt+")}, true
return Event{Key: k.String()}, true
}

// printable is the event for one typed rune: its key name is the rune, or
Expand Down
7 changes: 3 additions & 4 deletions libs/cmdio/teainput/teainput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ func TestDecode(t *testing.T) {
{"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"}},
{"alt enter", tea.KeyMsg{Type: tea.KeyEnter, Alt: true}, teainput.Event{Key: "alt+enter"}},
{"alt ctrl+c", tea.KeyMsg{Type: tea.KeyCtrlC, Alt: true}, teainput.Event{Key: "alt+ctrl+c"}},
{"alt left", tea.KeyMsg{Type: tea.KeyLeft, Alt: true}, teainput.Event{Key: "alt+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"}},
Expand Down
31 changes: 17 additions & 14 deletions libs/tableview/tableview.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"io"
"strings"
"text/tabwriter"
"unicode/utf8"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/databricks/cli/libs/cmdio/teainput"

"github.com/databricks/cli/libs/cmdio"
)
Expand Down Expand Up @@ -219,21 +221,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.viewport.Height = height
}
return m, nil
}

case tea.KeyMsg:
if in, ok := teainput.Decode(msg); ok {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The select model (line 256) and pager (line 186) both guard against paste before dispatching, but the table view doesn't. In normal mode a paste event has in.Key == "", so the updateNormal switch falls through and m.viewport.Update(msg) receives the raw tea.KeyMsg. If the viewport has a key binding that matches the pasted text (e.g. a single pasted "d" hitting HalfPageDown), it would scroll. Can we add a paste guard here like the other two models?

if m.searching {
return m.updateSearch(msg)
return m.updateSearch(in)
}
return m.updateNormal(msg)
return m.updateNormal(msg, in.Key)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nice to have] A paste in normal mode has in.Key == "", so updateNormal matches no case and the raw tea.KeyMsg (with Paste: true) is forwarded to m.viewport.Update(msg) at line 290. The select model intercepts paste before the key switch (select.go:256–262); the same guard here would keep the two models consistent and avoid leaking a decoded event back into an undecoded path. Something like:

if in, ok := teainput.Decode(msg); ok {
    if in.Paste {
        if m.searching && in.Text != "" {
            m.searchInput += in.Text
        }
        return m, nil
    }
    ...
}

}

var cmd tea.Cmd
m.viewport, cmd = m.viewport.Update(msg)
return m, cmd
}

func (m model) updateNormal(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
// updateNormal handles a key outside search mode. msg is the raw message,
// passed on to the viewport for keys the table doesn't handle itself.
func (m model) updateNormal(msg tea.Msg, key string) (tea.Model, tea.Cmd) {
switch key {
case "q", "esc", "ctrl+c":
return m, tea.Quit
case "/":
Expand Down Expand Up @@ -266,7 +271,7 @@ func (m model) updateNormal(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
case "pgup", "b":
m.moveCursor(-m.viewport.Height)
return m, nil
case "pgdown", "f", " ":
case "pgdown", "f", "space":
m.moveCursor(m.viewport.Height)
return m, nil
case "g":
Expand Down Expand Up @@ -307,8 +312,8 @@ func (m *model) scrollToCursor() {
}
}

func (m model) updateSearch(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
func (m model) updateSearch(in teainput.Event) (tea.Model, tea.Cmd) {
switch in.Key {
case "enter":
m.searching = false
m.searchQuery = m.searchInput
Expand All @@ -330,15 +335,13 @@ func (m model) updateSearch(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.viewport.Height++
return m, nil
case "backspace":
if len(m.searchInput) > 0 {
m.searchInput = m.searchInput[:len(m.searchInput)-1]
if _, size := utf8.DecodeLastRuneInString(m.searchInput); size > 0 {
m.searchInput = m.searchInput[:len(m.searchInput)-size]
}
return m, nil
default:
// Only accept printable characters.
if len(msg.String()) == 1 || msg.Type == tea.KeyRunes {
m.searchInput += msg.String()
}
// Only accept printable characters (typed or pasted).
m.searchInput += in.Text
return m, nil
}
}
Expand Down
10 changes: 10 additions & 0 deletions libs/tableview/tableview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/charmbracelet/bubbles/viewport"
"github.com/databricks/cli/libs/cmdio/teainput"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -117,3 +118,12 @@ func TestMoveCursorClampsAtZeroAndLast(t *testing.T) {
m.moveCursor(100)
assert.Equal(t, 2, m.cursor, "cursor should clamp to last data row")
}

func TestSearchBackspaceRemovesWholeRune(t *testing.T) {
m := readyModel([]string{"name"}, [][]string{{"café"}}, 5)
m.searching = true
m.searchInput = "café"

next, _ := m.Update(teainput.Press("backspace"))
assert.Equal(t, "caf", next.(model).searchInput)
}