1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
package tableprinter
import (
"bytes"
"fmt"
"log"
"os"
"strings"
"testing"
"github.com/MakeNowJust/heredoc"
)
func ExampleTablePrinter() {
// information about the terminal can be obtained using the [pkg/term] package
isTTY := true
termWidth := 14
red := func(s string) string {
return "\x1b[31m" + s + "\x1b[m"
}
t := New(os.Stdout, isTTY, termWidth)
t.AddField("9", WithTruncate(nil))
t.AddField("hello")
t.EndRow()
t.AddField("10", WithTruncate(nil))
t.AddField("long description", WithColor(red))
t.EndRow()
if err := t.Render(); err != nil {
log.Fatal(err)
}
// stdout now contains:
// 9 hello
// 10 long de...
}
func Test_ttyTablePrinter_autoTruncate(t *testing.T) {
buf := bytes.Buffer{}
tp := New(&buf, true, 5)
tp.AddField("1")
tp.AddField("hello")
tp.EndRow()
tp.AddField("2")
tp.AddField("world")
tp.EndRow()
err := tp.Render()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "1 he\n2 wo\n"
if buf.String() != expected {
t.Errorf("expected: %q, got: %q", expected, buf.String())
}
}
func Test_ttyTablePrinter_WithTruncate(t *testing.T) {
buf := bytes.Buffer{}
tp := New(&buf, true, 15)
tp.AddField("long SHA", WithTruncate(nil))
tp.AddField("hello")
tp.EndRow()
tp.AddField("another SHA", WithTruncate(nil))
tp.AddField("world")
tp.EndRow()
err := tp.Render()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "long SHA he\nanother SHA wo\n"
if buf.String() != expected {
t.Errorf("expected: %q, got: %q", expected, buf.String())
}
}
func Test_ttyTablePrinter_AddHeader(t *testing.T) {
buf := bytes.Buffer{}
tp := New(&buf, true, 80)
tp.AddHeader([]string{"ONE", "TWO", "THREE"}, WithColor(func(s string) string {
return fmt.Sprintf("\x1b[4m%s\x1b[m", s)
}))
// Subsequent calls to AddHeader are ignored.
tp.AddHeader([]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)
}
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_ttyTablePrinter_WithPadding(t *testing.T) {
buf := bytes.Buffer{}
tp := New(&buf, true, 80)
// Center the headers.
tp.AddHeader([]string{"A", "B", "C"}, WithPadding(func(width int, s string) string {
left := (width - len(s)) / 2
return strings.Repeat(" ", left) + s + strings.Repeat(" ", width-left-len(s))
}))
tp.AddField("hello")
tp.AddField("beautiful")
tp.AddField("people")
tp.EndRow()
err := tp.Render()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := heredoc.Doc(`
A B C
hello beautiful people
`)
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)
tp.AddField("1")
tp.AddField("hello")
tp.EndRow()
tp.AddField("2")
tp.AddField("world")
tp.EndRow()
err := tp.Render()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "1\thello\n2\tworld\n"
if buf.String() != expected {
t.Errorf("expected: %q, got: %q", expected, buf.String())
}
}
func Test_tsvTablePrinter_AddHeader(t *testing.T) {
buf := bytes.Buffer{}
tp := New(&buf, false, 0)
// Headers are not output in TSV output.
tp.AddHeader([]string{"ONE", "TWO", "THREE"})
tp.AddField("hello")
tp.AddField("beautiful")
tp.AddField("people")
tp.EndRow()
tp.AddField("1")
tp.AddField("2")
tp.AddField("3")
tp.EndRow()
err := tp.Render()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := "hello\tbeautiful\tpeople\n1\t2\t3\n"
if buf.String() != expected {
t.Errorf("expected: %q, got: %q", expected, buf.String())
}
}
|