Go module for outputting data in various table formats.
go get github.com/cuberat-go/fmttable
There is also a sample command (dsv2table) to convert delimited files to table output under the cmd directory that you can install:
go install github.com/cuberat-go/fmttable/cmd/dsv2table@latest
csvr := csv.NewReader(r)
csvr.Comma = ','
csvr.TrimLeadingSpace = true
var (
headers []string
rows [][]string
)
// Read the CSV data.
for {
record, err := csvr.Read()
if err == io.EOF {
break
}
if err != nil {
slog.Error("Error reading CSV data", "error", err)
return err
}
if headers == nil {
headers = record
} else {
rows = append(rows, record)
}
}
table := fmttable.NewTableFormat(headers, rows).WithStyle(conf.TableStyle)
if table == nil {
return fmt.Errorf("invalid table style: %s", conf.TableStyle)
}
tableStr := table.String()
+---+---+
|foo|bar|
+---+---+
|1 |2 |
|3 |4 |
|5 |6 |
|7 |8 |
+---+---+
Atlassian Confluence style table.
|| foo || bar ||
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 7 | 8 |
GitHub style Markdown table.
| foo | bar |
| --- | --- |
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 7 | 8 |