-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtables.go
62 lines (50 loc) · 1.41 KB
/
tables.go
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
package tables
import (
"fmt"
"github.com/stackitcloud/stackit-cli/internal/pkg/pager"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
)
type Table struct {
table table.Writer
}
// Creates a new table
func NewTable() Table {
t := table.NewWriter()
return Table{
table: t,
}
}
// Sets the header of the table
func (t *Table) SetHeader(header ...interface{}) {
t.table.AppendHeader(table.Row(header))
}
// Adds a row to the table
func (t *Table) AddRow(row ...interface{}) {
t.table.AppendRow(table.Row(row))
}
// Adds a separator between rows
func (t *Table) AddSeparator() {
t.table.AppendSeparator()
}
// Enables auto-merging of cells with similar values in the given columns
func (t *Table) EnableAutoMergeOnColumns(columns ...int) {
var colConfigs []table.ColumnConfig
for _, c := range columns {
colConfigs = append(colConfigs, table.ColumnConfig{Number: c, AutoMerge: true})
}
t.table.SetColumnConfigs(colConfigs)
}
// Returns the table rendered
func (t *Table) Render() string {
t.table.SetStyle(table.StyleLight)
t.table.Style().Options.DrawBorder = false
t.table.Style().Options.SeparateRows = false
t.table.Style().Options.SeparateColumns = true
t.table.Style().Options.SeparateHeader = true
return fmt.Sprintf("\n%s\n\n", t.table.Render())
}
// Displays the table in the command's stdout
func (t *Table) Display(cmd *cobra.Command) error {
return pager.Display(cmd, t.Render())
}