diff --git a/.nextchanges/air/7583.md b/.nextchanges/air/7583.md new file mode 100644 index 00000000000..2cf218fa8fc --- /dev/null +++ b/.nextchanges/air/7583.md @@ -0,0 +1 @@ +* Shrink the Experiment and User columns of the interactive `air list` table to fit the terminal width, keeping the end of a shortened experiment name. ([#7583](https://github.com/databricks/cli/pull/7583)) diff --git a/cmd/air/list_tui.go b/cmd/air/list_tui.go index af5951bca44..53ae3b3980f 100644 --- a/cmd/air/list_tui.go +++ b/cmd/air/list_tui.go @@ -115,6 +115,7 @@ type listModel struct { cursor int offset int // index of the first visible row height int // terminal height, for windowing + width int // terminal width, for fitting the flexible columns; 0 until known mode int // modeList or modeDetail viewport viewport.Model // for detail pane @@ -188,6 +189,8 @@ func (m listModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.WindowSizeMsg: m.height = msg.Height + m.width = msg.Width + m.cols = computeListCols(m.rows).fitWidth(m.width) m.viewport.Width = msg.Width m.viewport.Height = max(msg.Height-3, 1) m.viewport.SetContent(m.wrappedDetail()) @@ -201,7 +204,7 @@ func (m listModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil } m.rows = append(m.rows, msg.rows...) - m.cols = computeListCols(m.rows) + m.cols = computeListCols(m.rows).fitWidth(m.width) m.offset = m.clampedOffset() // A page with no matches but more to scan: keep paging so the cursor isn't // stuck at the end of the loaded rows. @@ -347,7 +350,12 @@ func (m listModel) View() string { visible := m.visibleCount() lines := []string{m.styles.renderHeader(m.cols)} for i := m.offset; i < m.offset+visible && i < len(m.rows); i++ { - lines = append(lines, m.styles.renderRow(m.cols, m.rows[i], i == m.cursor, m.links)) + row := m.rows[i] + // Experiment names often share a prefix and differ at the end, so a + // shrunk cell keeps the end. The one-shot table keeps the start, so its + // output stays as scripts already see it. + row.Experiment = truncateStart(row.Experiment, m.cols.experiment) + lines = append(lines, m.styles.renderRow(m.cols, row, i == m.cursor, m.links)) } lines = append(lines, m.renderHint()) return strings.Join(lines, "\n") + "\n" diff --git a/cmd/air/list_tui_render.go b/cmd/air/list_tui_render.go index 7b9add62f47..2152663430d 100644 --- a/cmd/air/list_tui_render.go +++ b/cmd/air/list_tui_render.go @@ -69,6 +69,47 @@ func computeListCols(rows []listRow) listCols { return c } +// Floors for the flexible columns: narrow enough to give room back to the fixed +// columns, wide enough that a truncated value is still recognizable. +const ( + experimentMinWidth = 12 + userMinWidth = 8 +) + +// tableWidth is the full width of a rendered row: every column plus the gutter +// and the two-cell separators. +func (c listCols) tableWidth() int { + const separators = 9 * 2 + return 1 + c.runID + c.experiment + c.status + c.started + c.duration + + c.progress + mlflowColWidth + c.user + c.accel + separators +} + +// fitWidth shrinks the flexible columns (Experiment, User) until the table fits +// in width cells, so a long value truncates instead of pushing the columns to +// its right off screen. It takes from the wider column first and stops at the +// floors: a terminal too narrow for the fixed columns still overflows. A width +// of 0 means the terminal size is unknown and leaves the columns alone. +func (c listCols) fitWidth(width int) listCols { + if width <= 0 { + return c + } + experimentFloor := min(c.experiment, experimentMinWidth) + userFloor := min(c.user, userMinWidth) + for overflow := c.tableWidth() - width; overflow > 0; overflow-- { + canShrinkExperiment := c.experiment > experimentFloor + canShrinkUser := c.user > userFloor + switch { + case canShrinkExperiment && (!canShrinkUser || c.experiment >= c.user): + c.experiment-- + case canShrinkUser: + c.user-- + default: + return c + } + } + return c +} + // renderHeader renders the muted column-title row. func (s listStyles) renderHeader(cols listCols) string { h := func(text string, width int, right bool) string { diff --git a/cmd/air/list_tui_test.go b/cmd/air/list_tui_test.go index 89964d14619..d8c129c2d5b 100644 --- a/cmd/air/list_tui_test.go +++ b/cmd/air/list_tui_test.go @@ -375,3 +375,100 @@ func TestPadAndTruncate(t *testing.T) { assert.Equal(t, "abcd…", truncate("abcdefgh", 5)) assert.Equal(t, "abc", truncate("abc", 5)) } + +// wideListRows is a fixture whose Experiment and User values both exceed +// columnCap, with realistic widths in the fixed columns. +func wideListRows() []listRow { + return []listRow{ + {RunID: "684127395104822", Experiment: "/Users/someone.longname@example.com/quickstart/qwen-finetune-lora", User: "someone.with.a.long.name.for.testing@example.com", Status: "SUCCESS", StartedAt: new("2026-10-23T17:32:39.000000+00:00"), Duration: "1h 14m", MLflowURL: "https://h.test/ml/experiments/E/runs/04c41514fbb0", MLflowLabel: "qwen-run-001", Accelerators: "8x H100"}, + {RunID: "684127395104823", Experiment: "mixtral", User: "me@example.com", Status: "RUNNING", StartedAt: new("2026-10-23T18:43:24.000000+00:00"), Duration: "3m 32s", Progress: "81% · ~48m", MLflowURL: "-", MLflowLabel: "-", Accelerators: "1x A10"}, + } +} + +func TestListColsFitWidth(t *testing.T) { + natural := computeListCols(wideListRows()) + require.Equal(t, columnCap, natural.experiment) + require.Equal(t, columnCap, natural.user) + require.Equal(t, 186, natural.tableWidth()) + + tests := []struct { + name string + width int + wantExperiment int + wantUser int + wantFits bool + }{ + {name: "unknown width leaves columns alone", width: 0, wantExperiment: columnCap, wantUser: columnCap}, + // The fixed columns alone need 114 cells, so both flexible columns sit at + // their floors and the table still overflows. + {name: "80 columns", width: 80, wantExperiment: experimentMinWidth, wantUser: userMinWidth}, + {name: "120 columns", width: 120, wantExperiment: experimentMinWidth, wantUser: userMinWidth}, + {name: "narrowest width that fits", width: 134, wantExperiment: experimentMinWidth, wantUser: userMinWidth, wantFits: true}, + {name: "160 columns splits the shortfall", width: 160, wantExperiment: 23, wantUser: 23, wantFits: true}, + {name: "200 columns keeps natural widths", width: 200, wantExperiment: columnCap, wantUser: columnCap, wantFits: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := natural.fitWidth(tt.width) + assert.Equal(t, tt.wantExperiment, got.experiment) + assert.Equal(t, tt.wantUser, got.user) + if tt.wantFits { + assert.LessOrEqual(t, got.tableWidth(), tt.width) + } + + // Only the flexible columns change. + got.experiment, got.user = natural.experiment, natural.user + assert.Equal(t, natural, got) + }) + } +} + +func TestListColsFitWidthFloorsNeverWiden(t *testing.T) { + // Columns already narrower than the floors stay as they are. + natural := computeListCols(testListRows()) + got := natural.fitWidth(40) + assert.Equal(t, natural.experiment, got.experiment) + assert.Equal(t, userMinWidth, got.user) +} + +func TestListModelFitsTerminalWidth(t *testing.T) { + r, _ := cmdio.NewRenderer(cmdio.MockDiscard(t.Context()), io.Discard) + rows := wideListRows() + f := &runFetcher{ctx: t.Context(), exhausted: true} + m := newListModel(r, f, rows[:1], false) + + // Before the terminal reports its size the columns keep their natural widths. + assert.Equal(t, columnCap, m.cols.experiment) + + next, _ := m.Update(tea.WindowSizeMsg{Width: 160, Height: 30}) + m = next.(listModel) + lines := strings.Split(strings.TrimRight(m.View(), "\n"), "\n") + require.Len(t, lines, 3) // header, one row, hint + for _, line := range lines[:2] { + assert.LessOrEqual(t, ansi.StringWidth(line), 160) + } + assert.Contains(t, lines[0], "Accelerators") + assert.Contains(t, lines[1], "8x H100") + // Experiment keeps its end, User its start. + assert.Contains(t, lines[1], " …tart/qwen-finetune-lora ") + assert.Contains(t, lines[1], " someone.with.a.long.nam… ") + + // A lazily loaded page is laid out against the same width. + next, _ = m.Update(moreRowsMsg{rows: rows[1:]}) + m = next.(listModel) + assert.LessOrEqual(t, m.cols.tableWidth(), 160) + + // Widening the terminal restores the natural widths. + next, _ = m.Update(tea.WindowSizeMsg{Width: 200, Height: 30}) + m = next.(listModel) + assert.Equal(t, columnCap, m.cols.experiment) + assert.Equal(t, columnCap, m.cols.user) +} + +func TestStaticListTableIgnoresTerminalWidth(t *testing.T) { + r, _ := cmdio.NewRenderer(cmdio.MockDiscard(t.Context()), io.Discard) + lines := strings.Split(staticListTable(r, wideListRows(), false), "\n") + assert.Equal(t, computeListCols(wideListRows()).tableWidth(), ansi.StringWidth(lines[0])) + // The one-shot table still cuts a long Experiment at its end. + assert.Contains(t, lines[1], " /Users/someone.longname@example.com… ") +}