Skip to content

Commit 5c488ec

Browse files
pieternIsaac
andcommitted
cmdio: stop paging after quit; probe the stream in use
The pager's quit waits for the terminal's answer, so a fetch in flight could still print its page and start another fetch; it now drops what lands after quit, as v1 did when Quit ended the program at once. The selftest huh and profile probes query the background on the stream they report on. The huh and paged template options use fromContext: SupportsColor already needs a cmdIO there. The reply guard tests that read the program's input back skip Windows, where bubbletea's reader can take those bytes first. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent de2a109 commit 5c488ec

8 files changed

Lines changed: 50 additions & 20 deletions

File tree

‎.nextchanges/cli/kitty-keyboard.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* Interactive prompts, selection lists and the pager enable the kitty keyboard protocol and modifyOtherKeys in terminals that support them, and turn them off again when they exit normally. A CLI killed by a signal such as SIGTERM can leave them enabled; run `reset` to restore the terminal. ([#7742](https://github.com/databricks/cli/pull/7742))
1+
* Interactive terminal UIs that read keys (prompts, selection lists, the pager, the table viewer and forms) enable the kitty keyboard protocol and modifyOtherKeys in terminals that support them, and turn them off again when they exit normally. A CLI killed by a signal such as SIGTERM can leave them enabled; run `reset` to restore the terminal. ([#7742](https://github.com/databricks/cli/pull/7742))

‎cmd/selftest/tui/huh.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ so this checks the theme, not the production prompt flows.`,
7676
cmdio.LogString(ctx, "answer: "+answer)
7777

7878
// The appkit theme is a huh.ThemeFunc, so its colours depend on the
79-
// background, queried once here for the probe. The blurred button is
80-
// the colour the theme picks for that background.
81-
dark := lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
79+
// background, queried once here for the probe on stderr, where the
80+
// form draws. The blurred button is the colour the theme picks for
81+
// that background.
82+
dark := lipgloss.HasDarkBackground(os.Stdin, os.Stderr)
8283
cmdio.LogString(ctx, fmt.Sprintf("dark_background=%t", dark))
8384
fg := theme.Theme(dark).Focused.BlurredButton.GetForeground()
8485
r, g, b, _ := fg.RGBA()

‎cmd/selftest/tui/profile.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ func newProfileCmd() *cobra.Command {
3030
// NO_COLOR only removes color; TERM=dumb also rules out prompts and
3131
// spinners. The env block below has the raw values, this line the decision.
3232
fmt.Fprintf(w, "interactive=%s prompt=%t\n", cmdio.GetInteractiveMode(ctx), cmdio.IsPromptSupported(ctx))
33+
// The background is queried on each stream's own terminal: one of
34+
// them can be redirected while the other is a terminal.
3335
for _, s := range []struct {
3436
name string
3537
out io.Writer
38+
file *os.File
3639
}{
37-
{"stdout", cmd.OutOrStdout()},
38-
{"stderr", cmd.ErrOrStderr()},
40+
{"stdout", cmd.OutOrStdout(), os.Stdout},
41+
{"stderr", cmd.ErrOrStderr(), os.Stderr},
3942
} {
4043
r, color := cmdio.NewRenderer(ctx, s.out)
4144
fmt.Fprintf(w, "%s: supports_color=%t profile=%s dark_background=%t\n",
42-
s.name, color, r.Profile(), lipgloss.HasDarkBackground(os.Stdin, os.Stdout))
45+
s.name, color, r.Profile(), lipgloss.HasDarkBackground(os.Stdin, s.file))
4346
}
4447
fmt.Fprintln(w, "env:")
4548
for _, name := range profileEnvVars {

‎libs/cmdio/huh.go‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package cmdio
22

33
import (
44
"context"
5-
"io"
6-
"os"
75

86
tea "charm.land/bubbletea/v2"
97
"charm.land/huh/v2"
@@ -27,12 +25,8 @@ func RunHuhForm(ctx context.Context, form *huh.Form) error {
2725
}
2826

2927
func huhProgramOptions(ctx context.Context) []tea.ProgramOption {
30-
c, _ := ctx.Value(cmdIOKey).(*cmdIO)
31-
var in io.Reader = os.Stdin
32-
var errW io.Writer = os.Stderr
33-
if c != nil {
34-
in, errW = c.in, c.err
35-
}
28+
c := fromContext(ctx)
29+
in, errW := c.in, c.err
3630
return []tea.ProgramOption{
3731
tea.WithInput(in),
3832
tea.WithOutput(errW),

‎libs/cmdio/paged_template.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func renderIteratorPagedTemplateCore[T any](
114114
}
115115
// NewTestIO's synthetic window size, at the height that keeps v1's fallback
116116
// page size of 50 rows; nil in production.
117-
if c, ok := ctx.Value(cmdIOKey).(*cmdIO); ok && c.teaWindowSize != nil {
117+
if c := fromContext(ctx); c.teaWindowSize != nil {
118118
teaOpts = append(teaOpts, tea.WithWindowSize(c.teaWindowSize.Width, pagerFallbackPageSize+pagerViewOverhead))
119119
}
120120
p := NewTeaProgram(m, in, teaOpts...)

‎libs/cmdio/pager.go‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,15 @@ type pagerModel[T any] struct {
6363
// the page's rows are lost or reordered. Keys wait for it.
6464
printing bool
6565
quitPending bool
66-
drainAll bool
67-
hasPrinted bool
68-
iterDone bool
69-
err error
66+
// quitting is set once Quit is returned. Quitting waits for the
67+
// terminal's answer (see NewTeaProgram), so a fetch still in flight can
68+
// land in the meantime; its page is dropped and no further fetch starts,
69+
// as it was when Quit ended the program at once.
70+
quitting bool
71+
drainAll bool
72+
hasPrinted bool
73+
iterDone bool
74+
err error
7075
}
7176

7277
// newPagerModel wires ctx into the fetch closure so nothing on the
@@ -171,6 +176,9 @@ func (m *pagerModel[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
171176

172177
case batchMsg:
173178
m.fetching = false
179+
if m.quitting {
180+
return m, nil
181+
}
174182
if msg.err != nil {
175183
m.err = msg.err
176184
return m, tea.Quit
@@ -195,6 +203,7 @@ func (m *pagerModel[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
195203
m.printing = false
196204
switch {
197205
case m.quitPending:
206+
m.quitting = true
198207
return m, tea.Quit
199208
case m.drainAll && !m.iterDone && m.err == nil:
200209
m.fetching = true
@@ -231,6 +240,7 @@ func (m *pagerModel[T]) quit() tea.Cmd {
231240
m.quitPending = true
232241
return nil
233242
}
243+
m.quitting = true
234244
return tea.Quit
235245
}
236246

‎libs/cmdio/pager_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,24 @@ func TestPagerModelQuitKeysInterruptDrain(t *testing.T) {
298298
}
299299
}
300300

301+
// The quit waits for the terminal's answer, so a fetch in flight can land
302+
// after it. That page is dropped and no further fetch starts.
303+
func TestPagerModelDropsBatchAfterQuit(t *testing.T) {
304+
m := newTestPager(t, &numberIterator{n: 100}, 5)
305+
m.hasPrinted = true
306+
m.fetching = true
307+
m.drainAll = true
308+
_, cmd := m.Update(teainput.Press("q"))
309+
_, ok := runCmd(t, cmd).(tea.QuitMsg)
310+
require.True(t, ok)
311+
312+
_, cmd = m.Update(batchMsg{lines: []string{"6", "7"}, count: 2})
313+
assert.Nil(t, cmd, "a page that lands after quit is neither printed nor followed by a fetch")
314+
_, cmd = m.Update(batchMsg{err: errors.New("boom")})
315+
assert.Nil(t, cmd)
316+
assert.NoError(t, m.err, "an error that lands after quit is dropped too")
317+
}
318+
301319
func TestPagerModelIgnoresKeysAfterDone(t *testing.T) {
302320
m := newTestPager(t, &numberIterator{n: 0}, 5)
303321
m.iterDone = true

‎libs/cmdio/teaprogram_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmdio
33
import (
44
"bytes"
55
"os"
6+
"runtime"
67
"strings"
78
"sync"
89
"testing"
@@ -130,6 +131,9 @@ func (f *fakeTerminal) queriesSeen() int {
130131
// away, against a terminal that answers queries late. build makes the program
131132
// from the options. It returns what was left unread on the program's input.
132133
func runQuitImmediately(t *testing.T, build func(in *os.File, opts ...tea.ProgramOption) *tea.Program) string {
134+
if runtime.GOOS == "windows" {
135+
t.Skip("on Windows bubbletea's input reader can outlive the program and take the bytes this reads")
136+
}
133137
inR, inW, err := os.Pipe()
134138
require.NoError(t, err)
135139
t.Cleanup(func() { inR.Close(); inW.Close() })

0 commit comments

Comments
 (0)