-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make a thin wrapper around tabwriter (#62)
- Loading branch information
1 parent
7e11a1b
commit 79a8195
Showing
6 changed files
with
95 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,38 @@ | ||
// Package table implements a thin wrapper around [text/tabwriter] to keep | ||
// formatting consistent across cli. | ||
package table | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"text/tabwriter" | ||
) | ||
|
||
// TableWriter config, used for showing subcommands in help. | ||
const ( | ||
minWidth = 2 // Min cell width | ||
tabWidth = 8 // Tab width in spaces | ||
padding = 1 // Padding | ||
padChar = '\t' // Char to pad with | ||
) | ||
|
||
// Table is a text table. | ||
type Table struct { | ||
tw *tabwriter.Writer // The underlying writer | ||
} | ||
|
||
// New returns a new [Table], writing to w. | ||
func New(w io.Writer) Table { | ||
tw := tabwriter.NewWriter(w, minWidth, tabWidth, padding, padChar, tabwriter.TabIndent) | ||
return Table{tw: tw} | ||
} | ||
|
||
// Row adds a row to the [Table]. | ||
func (t Table) Row(format string, a ...any) { | ||
fmt.Fprintf(t.tw, format, a...) | ||
} | ||
|
||
// Flush flushes the written data to the writer. | ||
func (t Table) Flush() error { | ||
return t.tw.Flush() | ||
} |
This file contains 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,45 @@ | ||
package table_test | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/FollowTheProcess/cli/internal/table" | ||
"github.com/FollowTheProcess/test" | ||
) | ||
|
||
var ( | ||
debug = flag.Bool("debug", false, "Print debug output during tests") | ||
update = flag.Bool("update", false, "Update golden files") | ||
) | ||
|
||
func TestTable(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
|
||
tab := table.New(buf) | ||
|
||
tab.Row("Col1\tCol2\tCol3\n") | ||
tab.Row("val1\tval2\tval3\n") | ||
tab.Row("val4\tval5\tval6\n") | ||
|
||
err := tab.Flush() | ||
test.Ok(t, err) | ||
|
||
file := filepath.Join(test.Data(t), "table.txt") | ||
|
||
if *debug { | ||
fmt.Printf("DEBUG (%s)\n_____\n\n%s\n", "TestTable", buf.String()) | ||
} | ||
|
||
if *update { | ||
t.Logf("Updating %s\n", file) | ||
err := os.WriteFile(file, buf.Bytes(), os.ModePerm) | ||
test.Ok(t, err) | ||
} | ||
|
||
test.File(t, buf.String(), "table.txt") | ||
} |
This file contains 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,3 @@ | ||
Col1 Col2 Col3 | ||
val1 val2 val3 | ||
val4 val5 val6 |