Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type host struct {
}
```

These tags affect text table output only. JSON and YAML output encode the original structs.

### Annotations

Insert comments or context between rows:
Expand Down
49 changes: 49 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,55 @@ func TestOmitEmptyColumns(t *testing.T) {
}
}

func TestSkipField(t *testing.T) {
type record struct {
Name string
Secret string `table:"-"`
}

var buf bytes.Buffer

tbl := New(WithWriter(&buf))
tbl.Write(record{Name: "text-visible", Secret: "hidden"})
_ = tbl.Flush()

output := buf.String()
if strings.Contains(output, "SECRET") || strings.Contains(output, "hidden") {
t.Errorf("expected skipped field to be omitted, got: %q", output)
}
}

func TestSkipFieldDoesNotAffectStructuredOutput(t *testing.T) {
type record struct {
Name string
Secret string `table:"-"`
}

t.Run("JSON", func(t *testing.T) {
var buf bytes.Buffer

tbl := NewJSON(WithWriter(&buf))
tbl.Write(record{Name: "json-visible", Secret: "included"})
_ = tbl.Flush()

if output := buf.String(); !strings.Contains(output, `"Secret": "included"`) {
t.Errorf("expected skipped field in JSON output, got: %q", output)
}
})

t.Run("YAML", func(t *testing.T) {
var buf bytes.Buffer

tbl := NewYAML(WithWriter(&buf))
tbl.Write(record{Name: "yaml-visible", Secret: "included"})
_ = tbl.Flush()

if output := buf.String(); !strings.Contains(output, "secret: included") {
t.Errorf("expected skipped field in YAML output, got: %q", output)
}
})
}

func TestWrapperInterface(t *testing.T) {
type status string

Expand Down
39 changes: 33 additions & 6 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type columnInfo struct {
Width int
OmitEmpty bool
IsZero bool
Skip bool
}

type wrapper interface {
Expand Down Expand Up @@ -112,7 +113,7 @@ func (t *Table) flush(info []columnInfo, rows [][]cell, annotations []annotation
}

for j := range rows[i] {
if info[j].IsZero { // skip empty columns TODO: make this configurable
if !info[j].visible() { // skip empty columns TODO: make this configurable
continue
}

Expand Down Expand Up @@ -141,7 +142,7 @@ func (t *Table) flush(info []columnInfo, rows [][]cell, annotations []annotation
}

// Skip padding for the last column
if j == len(rows[i])-1 {
if j == lastVisibleColumn(info) {
fmt.Fprint(t.writer, sgr.Wrap(rowColor, text))
} else {
fmt.Fprint(t.writer, sgr.Wrap(rowColor, text, padding), " ")
Expand Down Expand Up @@ -174,6 +175,14 @@ func (t *Table) flushHeader(info []columnInfo, rows [][]cell) {
}
}

for j := range info {
if info[j].OmitEmpty && isColumnZero(j, rows) {
info[j].IsZero = true
}
}

lastColumn := lastVisibleColumn(info)

for i := range numLines {
if i > 0 {
fmt.Fprintln(t.writer)
Expand All @@ -182,9 +191,7 @@ func (t *Table) flushHeader(info []columnInfo, rows [][]cell) {
for j := range info { // header
var label string

if info[j].OmitEmpty && isColumnZero(j, rows) {
info[j].IsZero = true

if !info[j].visible() {
continue
}

Expand All @@ -194,7 +201,7 @@ func (t *Table) flushHeader(info []columnInfo, rows [][]cell) {

fmt.Fprint(t.writer, sgr.Wrapf(t.colors.Header, "%-*s", info[j].Width, label))

if j != len(info)-1 {
if j != lastColumn {
fmt.Fprint(t.writer, " ")
}
}
Expand All @@ -203,6 +210,20 @@ func (t *Table) flushHeader(info []columnInfo, rows [][]cell) {
fmt.Fprintln(t.writer)
}

func lastVisibleColumn(info []columnInfo) int {
for i := len(info) - 1; i >= 0; i-- {
if info[i].visible() {
return i
}
}

return -1
}

func (c columnInfo) visible() bool {
return !c.IsZero && !c.Skip
}

func (t *Table) annotationsFor(start, end int, includeStart bool) []annotation {
var annotations []annotation

Expand Down Expand Up @@ -236,6 +257,12 @@ func (t *Table) processHeader(header reflect.Type) []columnInfo {
// Parse tag: "LABEL,omitempty" -> label="LABEL", omitEmpty=true
label, options, _ := strings.Cut(tag, ",")

if label == "-" {
columns[i].Skip = true

continue
}

if label != "" {
labels := strings.Split(label, "\n")
if labels[0] == "" { // use the default label if empty
Expand Down