diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-14 09:21:05 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-14 09:21:05 +0200 |
| commit | 4137c95d0b6882fbdc40d73b1f3528c28ceb653a (patch) | |
| tree | b8c1f53c2d7e657e8bca59b6154fefd128b1fb30 /internal/termprint/columns.go | |
| parent | 8ae4be9684a58d44985e5b5ee5e90f74555b2dde (diff) | |
Release v0.22.1v0.22.1
Diffstat (limited to 'internal/termprint/columns.go')
| -rw-r--r-- | internal/termprint/columns.go | 63 |
1 files changed, 40 insertions, 23 deletions
diff --git a/internal/termprint/columns.go b/internal/termprint/columns.go index b4d30bc..25cc07e 100644 --- a/internal/termprint/columns.go +++ b/internal/termprint/columns.go @@ -83,30 +83,15 @@ func (cp *ColumnPrinter) Writer(idx int) io.Writer { // PrintHeader writes provider/model headers and a divider row. func (cp *ColumnPrinter) PrintHeader() { + cp.PrintHeaderTo(cp.stdout) +} + +// PrintHeaderTo writes provider/model headers and a divider row to w. +func (cp *ColumnPrinter) PrintHeaderTo(w io.Writer) { cp.mu.Lock() defer cp.mu.Unlock() - combo := make([]string, cp.columns) - for i := 0; i < cp.columns; i++ { - provider := strings.TrimSpace(cp.providers[i]) - model := strings.TrimSpace(cp.models[i]) - switch { - case provider != "" && model != "": - combo[i] = provider + ":" + model - case provider != "": - combo[i] = provider - case model != "": - combo[i] = model - default: - combo[i] = "" - } - } - cp.writeLine(combo) - divider := make([]string, cp.columns) - line := strings.Repeat("─", cp.colWidth) - for i := range divider { - divider[i] = line - } - cp.writeLine(divider) + cp.writeLineTo(w, cp.headerCells()) + cp.writeLineTo(w, cp.dividerCells()) } // Flush emits any buffered partial line for a column. @@ -182,6 +167,38 @@ func (cp *ColumnPrinter) wrap(text string) []string { } func (cp *ColumnPrinter) writeLine(cells []string) { + cp.writeLineTo(cp.stdout, cells) +} + +func (cp *ColumnPrinter) headerCells() []string { + cells := make([]string, cp.columns) + for i := 0; i < cp.columns; i++ { + provider := strings.TrimSpace(cp.providers[i]) + model := strings.TrimSpace(cp.models[i]) + switch { + case provider != "" && model != "": + cells[i] = provider + ":" + model + case provider != "": + cells[i] = provider + case model != "": + cells[i] = model + default: + cells[i] = "" + } + } + return cells +} + +func (cp *ColumnPrinter) dividerCells() []string { + cells := make([]string, cp.columns) + line := strings.Repeat("─", cp.colWidth) + for i := range cells { + cells[i] = line + } + return cells +} + +func (cp *ColumnPrinter) writeLineTo(w io.Writer, cells []string) { if len(cells) < cp.columns { extra := make([]string, cp.columns-len(cells)) cells = append(cells, extra...) @@ -203,5 +220,5 @@ func (cp *ColumnPrinter) writeLine(cells []string) { } } builder.WriteByte('\n') - _, _ = cp.stdout.Write([]byte(builder.String())) + _, _ = w.Write([]byte(builder.String())) } |
