2022-04-14 08:49:56 +00:00
|
|
|
package report
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Table returns a text-formatted table, optionally as valid Markdown,
|
|
|
|
// so the output can be pasted into the docs.
|
|
|
|
func Table(rows [][]string, cols []string, markDown bool) string {
|
2022-04-14 10:18:05 +00:00
|
|
|
return TableWithCaption(rows, cols, "", markDown)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableWithCaption returns a text-formatted table with caption, optionally as valid Markdown,
|
|
|
|
// so the output can be pasted into the docs.
|
|
|
|
func TableWithCaption(rows [][]string, cols []string, caption string, markDown bool) string {
|
2022-04-14 08:49:56 +00:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
|
2022-04-14 10:18:05 +00:00
|
|
|
// Set Borders.
|
2022-04-14 08:49:56 +00:00
|
|
|
borders := tablewriter.Border{
|
|
|
|
Left: true,
|
|
|
|
Right: true,
|
|
|
|
Top: !markDown,
|
|
|
|
Bottom: !markDown,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render.
|
|
|
|
table := tablewriter.NewWriter(buf)
|
2022-04-14 10:18:05 +00:00
|
|
|
|
|
|
|
// Set Caption.
|
|
|
|
if caption != "" {
|
|
|
|
table.SetCaption(true, caption)
|
|
|
|
}
|
|
|
|
|
2022-04-14 08:49:56 +00:00
|
|
|
table.SetAutoWrapText(!markDown)
|
|
|
|
table.SetAutoFormatHeaders(false)
|
|
|
|
table.SetHeader(cols)
|
|
|
|
table.SetBorders(borders)
|
|
|
|
table.SetCenterSeparator("|")
|
|
|
|
table.AppendBulk(rows)
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|