get.porter.sh/porter@v1.3.0/pkg/printer/table.go (about) 1 package printer 2 3 import ( 4 "fmt" 5 "io" 6 "reflect" 7 8 "github.com/olekukonko/tablewriter" 9 ) 10 11 func NewTableSection(out io.Writer) *tablewriter.Table { 12 table := tablewriter.NewWriter(out) 13 table.SetCenterSeparator("") 14 table.SetColumnSeparator("") 15 table.SetAlignment(tablewriter.ALIGN_LEFT) 16 table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) 17 table.SetBorders(tablewriter.Border{Left: false, Right: false, Bottom: false, Top: true}) 18 table.SetAutoFormatHeaders(false) 19 table.SetAutoWrapText(true) 20 table.SetReflowDuringAutoWrap(true) 21 return table 22 } 23 24 func PrintTableParameterSet(out io.Writer, params [][]string, headers ...string) error { 25 table := NewTableSection(out) 26 27 // Print the outputs table 28 table.SetHeader(headers) 29 for _, v := range params { 30 table.Append(v) 31 } 32 table.Render() 33 return nil 34 } 35 36 // PrintTable outputs a dataset in tabular format 37 func PrintTable(out io.Writer, v interface{}, getRow func(row interface{}) []string, headers ...string) error { 38 if reflect.TypeOf(v).Kind() != reflect.Slice { 39 return fmt.Errorf("invalid data passed to PrintTable, must be a slice but got %T", v) 40 } 41 42 rows := reflect.ValueOf(v) 43 44 table := NewTableSection(out) 45 46 // Print the outputs table 47 table.SetHeader(headers) 48 for i := 0; i < rows.Len(); i++ { 49 table.Append(getRow(rows.Index(i).Interface())) 50 } 51 52 table.Render() 53 return nil 54 }