mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: Match kubectl table style for simpler scripting (#1363)
Fixes #1322.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package cliui
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/jedib0t/go-pretty/v6/table"
|
||||
)
|
||||
|
||||
// Table creates a new table with standardized styles.
|
||||
func Table() table.Writer {
|
||||
tableWriter := table.NewWriter()
|
||||
tableWriter.Style().Box.PaddingLeft = ""
|
||||
tableWriter.Style().Box.PaddingRight = " "
|
||||
tableWriter.Style().Options.DrawBorder = false
|
||||
tableWriter.Style().Options.SeparateHeader = false
|
||||
tableWriter.Style().Options.SeparateColumns = false
|
||||
return tableWriter
|
||||
}
|
||||
|
||||
// FilterTableColumns returns configurations to hide columns
|
||||
// that are not provided in the array. If the array is empty,
|
||||
// no filtering will occur!
|
||||
func FilterTableColumns(header table.Row, columns []string) []table.ColumnConfig {
|
||||
if len(columns) == 0 {
|
||||
return nil
|
||||
}
|
||||
columnConfigs := make([]table.ColumnConfig, 0)
|
||||
for _, headerTextRaw := range header {
|
||||
headerText, _ := headerTextRaw.(string)
|
||||
hidden := true
|
||||
for _, column := range columns {
|
||||
if strings.EqualFold(strings.ReplaceAll(column, "_", " "), headerText) {
|
||||
hidden = false
|
||||
break
|
||||
}
|
||||
}
|
||||
columnConfigs = append(columnConfigs, table.ColumnConfig{
|
||||
Name: headerText,
|
||||
Hidden: hidden,
|
||||
})
|
||||
}
|
||||
return columnConfigs
|
||||
}
|
||||
Reference in New Issue
Block a user