Skip to content
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

Add header support to tableprinter #139

Merged
merged 4 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 22 additions & 5 deletions pkg/tableprinter/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type fieldOption func(*tableField)

type TablePrinter interface {
AddHeaders([]string, ...fieldOption)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok kind of an odd question, but should this be AddHeader? I don't have an opinion, just linguistically wondering what is correct. Are we adding a table header, or are we adding table headers?

AddField(string, ...fieldOption)
EndRow()
Render() error
Expand Down Expand Up @@ -47,6 +48,7 @@ func New(w io.Writer, isTTY bool, maxWidth int) TablePrinter {
maxWidth: maxWidth,
}
}

return &tsvTablePrinter{
out: w,
}
Expand All @@ -59,9 +61,22 @@ type tableField struct {
}

type ttyTablePrinter struct {
out io.Writer
maxWidth int
rows [][]tableField
out io.Writer
maxWidth int
hasHeaders bool
rows [][]tableField
}

func (t *ttyTablePrinter) AddHeaders(headers []string, opts ...fieldOption) {
if t.hasHeaders {
return
}

t.hasHeaders = true
for _, header := range headers {
t.AddField(header, opts...)
}
t.EndRow()
}

func (t *ttyTablePrinter) AddField(s string, opts ...fieldOption) {
Expand Down Expand Up @@ -92,7 +107,7 @@ func (t *ttyTablePrinter) Render() error {
numCols := len(t.rows[0])
colWidths := t.calculateColumnWidths(len(delim))

for _, row := range t.rows {
for i, row := range t.rows {
for col, field := range row {
if col > 0 {
_, err := fmt.Fprint(t.out, delim)
Expand All @@ -104,7 +119,7 @@ func (t *ttyTablePrinter) Render() error {
if field.truncateFunc != nil {
truncVal = field.truncateFunc(colWidths[col], field.text)
}
if col < numCols-1 {
if col < numCols-1 || i == 0 && t.hasHeaders {
// pad value with spaces on the right
if padWidth := colWidths[col] - text.DisplayWidth(field.text); padWidth > 0 {
truncVal += strings.Repeat(" ", padWidth)
Expand Down Expand Up @@ -213,6 +228,8 @@ type tsvTablePrinter struct {
currentCol int
}

func (t *tsvTablePrinter) AddHeaders(_ []string, _ ...fieldOption) {}

func (t *tsvTablePrinter) AddField(text string, opts ...fieldOption) {
if t.currentCol > 0 {
fmt.Fprint(t.out, "\t")
Expand Down
32 changes: 32 additions & 0 deletions pkg/tableprinter/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package tableprinter

import (
"bytes"
"fmt"
"log"
"os"
"testing"

"github.com/MakeNowJust/heredoc"
)

func ExampleTablePrinter() {
Expand Down Expand Up @@ -74,6 +77,35 @@ func Test_ttyTablePrinter_WithTruncate(t *testing.T) {
}
}

func Test_ttyTablePrinter_AddHeaders(t *testing.T) {
buf := bytes.Buffer{}
tp := New(&buf, true, 80)

tp.AddHeaders([]string{"ONE", "TWO", "THREE"}, WithColor(func(s string) string {
return fmt.Sprintf("\x1b[4m%s\x1b[m", s)
}))
tp.AddHeaders([]string{"SHOULD", "NOT", "EXIST"})

tp.AddField("hello")
tp.AddField("beautiful")
tp.AddField("people")
tp.EndRow()

err := tp.Render()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

// Assert the last column header is padded to support underlines, background styles, etc.
expected := heredoc.Docf(`
%[1]s[4mONE %[1]s[m %[1]s[4mTWO %[1]s[m %[1]s[4mTHREE %[1]s[m
hello beautiful people
`, "\x1b")
if buf.String() != expected {
t.Errorf("expected: %q, got: %q", expected, buf.String())
}
}

func Test_tsvTablePrinter(t *testing.T) {
buf := bytes.Buffer{}
tp := New(&buf, false, 0)
Expand Down
Loading