Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/djthorpe/go-tablewriter
Writer for table data
https://github.com/djthorpe/go-tablewriter
ascii csv golang tablewriter tsv
Last synced: about 19 hours ago
JSON representation
Writer for table data
- Host: GitHub
- URL: https://github.com/djthorpe/go-tablewriter
- Owner: djthorpe
- License: apache-2.0
- Created: 2024-05-07T07:00:42.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-05-23T07:02:01.000Z (9 months ago)
- Last Synced: 2024-05-23T07:29:27.789Z (9 months ago)
- Topics: ascii, csv, golang, tablewriter, tsv
- Language: Go
- Homepage: https://pkg.go.dev/github.com/djthorpe/go-tablewriter
- Size: 68.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-tablewriter
This module implements a writer for table data, which can be output as CSV or Text.
Example:
```go
package mainimport (
tablewriter "github.com/djthorpe/go-tablewriter")
func main() {
table := []TableData{
{A: "hello", B: "world"},
}
writer := tablewriter.New(os.Stdout)
writer.Write(table, tablewriter.OptHeader())
}
```The `Write` function expects a single struct or a slice of structs as the first argument. Each struct
represents a row in the table. The struct fields (including any which are embedded) are used as
columns in the table.## Table Options
The following options can be used to customize the output:
- `tablewriter.OptHeader()`: Output the header row.
- `tablewriter.OptFieldDelim('|')`: Set the field delimiter, default is ',' for CSV and '|' for Text.
- `tablewriter.OptOutputCSV()`: Output as CSV.
- `tablewriter.OptOutputText()`: Output as Text.
- `tablewriter.OptNull("")`: Set how the nil value is represented in the output, defaults to ``.## Struct Tags
Tags on struct fields can determine how the field is output. The `json` and `writer` tags can be used,
with the `writer` tag options taking precedence.- `writer:"-"`: Skip the field.
- `writer:"Name"`: Set the column header to "Name".
- `writer:",omitdefault"`: If all values in the table are zero-valued, skip output of the column (TODO)
- `writer:",wrap"`: Field is wrapped to the width of the column.
- `writer:",alignright"`: Field is right-aligned in the column.
- `writer:",width:20"`: Suggested column width is 20 characters## Customize Field Output
You can implement the following interface on any field to customize how it is output:
```go
type Marshaller interface {
Marshal() ([]byte, error)
}
```By default, strings and time.Time types are output as-is and other values are marshalled
using the `encoding/json` package.## Contribution and License
See the [LICENSE](LICENSE) file for license rights and limitations, currently Apache.
Pull requests and [issues](https://github.com/djthorpe/go-tablewriter/issues) are welcome.## Changelog
- v0.0.1 (May 2024) Initial version
- v0.0.2 (May 2024) Documentation updates
- v0.0.4 (May 2024) Added text wrapping for text output
- v0.0.5 (May 2024) Exposing options for customizing the output in the struct tags
- v0.0.9 (Aug 2024) Added a `Writeln` method for output of text
- v0.0.10 (Dec 2024) Upgraded dependenciesFuture versions will include more options for customizing the output:
- Estimating sizing the width of fields for the text package
- Setting the width of the table based on terminal width
- Adding JSON and SQL output
- Outputing fields with ANSI color codes