-
Notifications
You must be signed in to change notification settings - Fork 160
Add interactive pager for list commands with a row template #5015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cbd549c
Add interactive pager for list commands with a row template
simonfaltum 7518921
Simplify pager: trim docs, extract flushPage, collapse tests
simonfaltum 81cb4dc
Apply go-code-structure patterns to tests
simonfaltum 0155767
Merge remote-tracking branch 'origin/main' into simonfaltum/list-simp…
simonfaltum 094a6f4
Merge remote-tracking branch 'origin/main' into simonfaltum/list-simp…
simonfaltum 8bf0d4a
Merge remote-tracking branch 'origin/main' into simonfaltum/list-simp…
simonfaltum 0a17994
Replace x/term raw stdin with a bubbletea tea.Model pager
simonfaltum eb4058e
Trim narrative comments in the paged renderer
simonfaltum 00e671c
Align pager stream doc and rename firstBatch
simonfaltum 030e79c
Show a loading spinner while the pager fetches
simonfaltum ea9e103
Address review: ctx, pageSize, plumbing, and loading ellipsis
simonfaltum 3fcaa42
Merge branch 'main' into simonfaltum/list-simple-paginated
simonfaltum bf33cee
Reserve 1 line for the prompt, not 2
simonfaltum 6b5e033
Merge remote-tracking branch 'origin/simonfaltum/list-simple-paginate…
simonfaltum db943a7
Merge branch 'main' into simonfaltum/list-simple-paginated
simonfaltum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| package cmdio | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "io" | ||
| "regexp" | ||
| "strings" | ||
| "text/template" | ||
| "unicode/utf8" | ||
|
|
||
| tea "github.com/charmbracelet/bubbletea" | ||
| "github.com/databricks/databricks-sdk-go/listing" | ||
| ) | ||
|
|
||
| // ansiCSIPattern matches ANSI SGR escape sequences so colored cells | ||
| // aren't counted toward column widths. github.com/fatih/color emits CSI | ||
| // ... m, which is all our templates use. | ||
| var ansiCSIPattern = regexp.MustCompile("\x1b\\[[0-9;]*m") | ||
|
|
||
| // renderIteratorPagedTemplate pages an iterator through the template | ||
| // renderer, prompting between batches. SPACE advances one page, ENTER | ||
| // drains the rest, q/esc/Ctrl+C quit. | ||
| func renderIteratorPagedTemplate[T any]( | ||
| ctx context.Context, | ||
| iter listing.Iterator[T], | ||
| in io.Reader, | ||
| out io.Writer, | ||
| headerTemplate, tmpl string, | ||
| ) error { | ||
| return renderIteratorPagedTemplateCore(ctx, iter, in, out, headerTemplate, tmpl, pagerFallbackPageSize) | ||
| } | ||
|
|
||
| // templatePager renders accumulated rows, locking column widths from the | ||
| // first page so layout stays stable across batches. We do not use | ||
| // text/tabwriter because it recomputes widths on every Flush. | ||
| type templatePager struct { | ||
| headerT *template.Template | ||
| rowT *template.Template | ||
| headerStr string | ||
| widths []int | ||
| headerDone bool | ||
| } | ||
|
|
||
| // flushLines renders the header (on the first call) plus any buffered | ||
| // rows, then pads each cell to the widths recorded on the first page so | ||
| // columns line up across batches. | ||
| func (p *templatePager) flushLines(buf []any) ([]string, error) { | ||
| if p.headerDone && len(buf) == 0 { | ||
| return nil, nil | ||
| } | ||
| var rendered bytes.Buffer | ||
| if !p.headerDone && p.headerStr != "" { | ||
| if err := p.headerT.Execute(&rendered, nil); err != nil { | ||
| return nil, err | ||
| } | ||
| rendered.WriteByte('\n') | ||
| } | ||
| if len(buf) > 0 { | ||
| if err := p.rowT.Execute(&rendered, buf); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| p.headerDone = true | ||
|
|
||
| text := strings.TrimRight(rendered.String(), "\n") | ||
| if text == "" { | ||
| return nil, nil | ||
| } | ||
| rows := strings.Split(text, "\n") | ||
| if p.widths == nil { | ||
| p.widths = computeWidths(rows) | ||
| } | ||
| lines := make([]string, len(rows)) | ||
| for i, row := range rows { | ||
| lines[i] = padRow(strings.Split(row, "\t"), p.widths) | ||
| } | ||
| return lines, nil | ||
| } | ||
|
|
||
| func renderIteratorPagedTemplateCore[T any]( | ||
| ctx context.Context, | ||
| iter listing.Iterator[T], | ||
| in io.Reader, | ||
| out io.Writer, | ||
| headerTemplate, tmpl string, | ||
| pageSize int, | ||
| ) error { | ||
| // Header and row templates must be separate *template.Template | ||
| // instances: Parse replaces the receiver's body in place, so sharing | ||
| // one makes the second Parse stomp the first. | ||
| headerT, err := template.New("header").Funcs(renderFuncMap).Parse(headerTemplate) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| rowT, err := template.New("row").Funcs(renderFuncMap).Parse(tmpl) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| pager := &templatePager{ | ||
| headerT: headerT, | ||
| rowT: rowT, | ||
| headerStr: headerTemplate, | ||
| } | ||
| m := newPagerModel(ctx, iter, pager, pageSize, limitFromContext(ctx)) | ||
| p := tea.NewProgram( | ||
| m, | ||
| tea.WithInput(in), | ||
| tea.WithOutput(out), | ||
| // Match spinner: let SIGINT reach the process rather than the TUI | ||
| // so Ctrl+C also interrupts a stalled iterator fetch. | ||
| tea.WithoutSignalHandler(), | ||
| ) | ||
| // Unlike cmdio.NewSpinner, the pager doesn't need to acquire/release | ||
| // through cmdIO: p.Run is blocking and tea restores the terminal on | ||
| // its own before returning, so there's no other tea.Program that could | ||
| // race with ours. | ||
| if _, err := p.Run(); err != nil { | ||
| return err | ||
| } | ||
| return m.err | ||
| } | ||
|
|
||
| // visualWidth counts runes ignoring ANSI SGR escape sequences. | ||
| func visualWidth(s string) int { | ||
| return utf8.RuneCountInString(ansiCSIPattern.ReplaceAllString(s, "")) | ||
| } | ||
|
|
||
| func computeWidths(rows []string) []int { | ||
| var widths []int | ||
| for _, row := range rows { | ||
| for i, cell := range strings.Split(row, "\t") { | ||
| if i >= len(widths) { | ||
| widths = append(widths, 0) | ||
| } | ||
| if w := visualWidth(cell); w > widths[i] { | ||
| widths[i] = w | ||
| } | ||
| } | ||
| } | ||
| return widths | ||
| } | ||
|
|
||
| // padRow joins cells with two-space separators matching tabwriter's | ||
| // minpad, padding every cell except the last to widths[i] visual runes. | ||
| func padRow(cells []string, widths []int) string { | ||
| var b strings.Builder | ||
| for i, cell := range cells { | ||
| if i > 0 { | ||
| b.WriteString(" ") | ||
| } | ||
| b.WriteString(cell) | ||
| if i < len(cells)-1 && i < len(widths) { | ||
| if pad := widths[i] - visualWidth(cell); pad > 0 { | ||
| b.WriteString(strings.Repeat(" ", pad)) | ||
| } | ||
| } | ||
| } | ||
| return b.String() | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spinner does acquire/release because it returns the control flow to the caller and cleanup needs to happen when the CLI exits (restore terminal control characters).
This is not needed here because it is blocking.
Could be helpful context in a comment for future refactors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a comment near
p.Runexplaining why we dont need the dance here (blocking, tea restores the terminal on its own). thanks for the context