diff --git a/go.mod b/go.mod index b28dcdc31..de6ef5ce1 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/go-logr/logr v1.4.2 github.com/golang-jwt/jwt/v4 v4.5.1 github.com/google/go-containerregistry v0.20.2 - github.com/jedib0t/go-pretty/v6 v6.6.3 + github.com/jedib0t/go-pretty/v6 v6.6.4 github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 github.com/onsi/ginkgo/v2 v2.22.0 github.com/onsi/gomega v1.36.0 diff --git a/go.sum b/go.sum index 69ceeeadc..5d848c09a 100644 --- a/go.sum +++ b/go.sum @@ -245,8 +245,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= -github.com/jedib0t/go-pretty/v6 v6.6.3 h1:nGqgS0tgIO1Hto47HSaaK4ac/I/Bu7usmdD3qvs0WvM= -github.com/jedib0t/go-pretty/v6 v6.6.3/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E= +github.com/jedib0t/go-pretty/v6 v6.6.4 h1:B51RjA+Sytv0C0Je7PHGDXZBF2JpS5dZEWWRueBLP6U= +github.com/jedib0t/go-pretty/v6 v6.6.4/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= diff --git a/vendor/github.com/jedib0t/go-pretty/v6/table/render_init.go b/vendor/github.com/jedib0t/go-pretty/v6/table/render_init.go index 2627e012b..7ad2b6124 100644 --- a/vendor/github.com/jedib0t/go-pretty/v6/table/render_init.go +++ b/vendor/github.com/jedib0t/go-pretty/v6/table/render_init.go @@ -223,9 +223,6 @@ func (t *Table) initForRenderRows() { t.autoIndexVIndexMaxLength = len(fmt.Sprint(len(t.rowsRaw))) // stringify all the rows to make it easy to render - if t.rowPainter != nil { - t.rowsColors = make([]text.Colors, len(t.rowsRaw)) - } t.rows = t.initForRenderRowsStringify(t.rowsRaw, renderHint{}) t.rowsFooter = t.initForRenderRowsStringify(t.rowsFooterRaw, renderHint{isFooterRow: true}) t.rowsHeader = t.initForRenderRowsStringify(t.rowsHeaderRaw, renderHint{isHeaderRow: true}) @@ -233,6 +230,9 @@ func (t *Table) initForRenderRows() { // sort the rows as requested t.initForRenderSortRows() + // find the row colors (if any) + t.initForRenderRowPainterColors() + // suppress columns without any content t.initForRenderSuppressColumns() @@ -243,14 +243,42 @@ func (t *Table) initForRenderRows() { func (t *Table) initForRenderRowsStringify(rows []Row, hint renderHint) []rowStr { rowsStr := make([]rowStr, len(rows)) for idx, row := range rows { - if t.rowPainter != nil && hint.isRegularRow() { - t.rowsColors[idx] = t.rowPainter(row) - } + hint.rowNumber = idx + 1 rowsStr[idx] = t.analyzeAndStringify(row, hint) } return rowsStr } +func (t *Table) initForRenderRowPainterColors() { + if !t.hasRowPainter() { + return + } + + // generate the colors + t.rowsColors = make([]text.Colors, len(t.rowsRaw)) + for idx, row := range t.rowsRaw { + idxColors := idx + if len(t.sortedRowIndices) > 0 { + // override with the sorted row index + for j := 0; j < len(t.sortedRowIndices); j++ { + if t.sortedRowIndices[j] == idx { + idxColors = j + break + } + } + } + + if t.rowPainter != nil { + t.rowsColors[idxColors] = t.rowPainter(row) + } else if t.rowPainterWithAttributes != nil { + t.rowsColors[idxColors] = t.rowPainterWithAttributes(row, RowAttributes{ + Number: idx + 1, + NumberSorted: idxColors + 1, + }) + } + } +} + func (t *Table) initForRenderRowSeparator() { t.rowSeparator = make(rowStr, t.numColumns) for colIdx, maxColumnLength := range t.maxColumnLengths { @@ -265,21 +293,12 @@ func (t *Table) initForRenderSortRows() { } // sort the rows - sortedRowIndices := t.getSortedRowIndices() + t.sortedRowIndices = t.getSortedRowIndices() sortedRows := make([]rowStr, len(t.rows)) for idx := range t.rows { - sortedRows[idx] = t.rows[sortedRowIndices[idx]] + sortedRows[idx] = t.rows[t.sortedRowIndices[idx]] } t.rows = sortedRows - - // sort the rowsColors - if len(t.rowsColors) > 0 { - sortedRowsColors := make([]text.Colors, len(t.rows)) - for idx := range t.rows { - sortedRowsColors[idx] = t.rowsColors[sortedRowIndices[idx]] - } - t.rowsColors = sortedRowsColors - } } func (t *Table) initForRenderSuppressColumns() { diff --git a/vendor/github.com/jedib0t/go-pretty/v6/table/row.go b/vendor/github.com/jedib0t/go-pretty/v6/table/row.go new file mode 100644 index 000000000..3c165fcab --- /dev/null +++ b/vendor/github.com/jedib0t/go-pretty/v6/table/row.go @@ -0,0 +1,43 @@ +package table + +import ( + "fmt" + + "github.com/jedib0t/go-pretty/v6/text" +) + +// Row defines a single row in the Table. +type Row []interface{} + +func (r Row) findColumnNumber(colName string) int { + for colIdx, col := range r { + if fmt.Sprint(col) == colName { + return colIdx + 1 + } + } + return 0 +} + +// RowAttributes contains properties about the Row during the render. +type RowAttributes struct { + Number int // Row Number (1-indexed) as appended + NumberSorted int // Row number (1-indexed) after sorting +} + +// RowPainter is a custom function that takes a Row as input and returns the +// text.Colors{} to use on the entire row +type RowPainter func(row Row) text.Colors + +// RowPainterWithAttributes is the same as RowPainter but passes in additional +// attributes from render time +type RowPainterWithAttributes func(row Row, attr RowAttributes) text.Colors + +// rowStr defines a single row in the Table comprised of just string objects. +type rowStr []string + +// areEqual returns true if the contents of the 2 given columns are the same +func (row rowStr) areEqual(colIdx1 int, colIdx2 int) bool { + return colIdx1 >= 0 && colIdx1 < len(row) && + colIdx2 >= 0 && colIdx2 < len(row) && + row[colIdx1] == row[colIdx2] +} diff --git a/vendor/github.com/jedib0t/go-pretty/v6/table/table.go b/vendor/github.com/jedib0t/go-pretty/v6/table/table.go index 6cf043308..479d4b35a 100644 --- a/vendor/github.com/jedib0t/go-pretty/v6/table/table.go +++ b/vendor/github.com/jedib0t/go-pretty/v6/table/table.go @@ -10,30 +10,6 @@ import ( "github.com/jedib0t/go-pretty/v6/text" ) -// Row defines a single row in the Table. -type Row []interface{} - -func (r Row) findColumnNumber(colName string) int { - for colIdx, col := range r { - if fmt.Sprint(col) == colName { - return colIdx + 1 - } - } - return 0 -} - -// RowPainter is a custom function that takes a Row as input and returns the -// text.Colors{} to use on the entire row -type RowPainter func(row Row) text.Colors - -// rowStr defines a single row in the Table comprised of just string objects. -type rowStr []string - -// areEqual returns true if the contents of the 2 given columns are the same -func (row rowStr) areEqual(colIdx1 int, colIdx2 int) bool { - return colIdx1 >= 0 && colIdx2 < len(row) && row[colIdx1] == row[colIdx2] -} - // Table helps print a 2-dimensional array in a human-readable pretty-table. type Table struct { // allowedRowLength is the max allowed length for a row (or line of output) @@ -74,7 +50,7 @@ type Table struct { // rows stores the rows that make up the body (in string form) rows []rowStr // rowsColors stores the text.Colors over-rides for each row as defined by - // rowPainter + // rowPainter or rowPainterWithAttributes rowsColors []text.Colors // rowsConfigs stores RowConfig for each row rowsConfigMap map[int]RowConfig @@ -95,6 +71,8 @@ type Table struct { // rowPainter is a custom function that given a Row, returns the colors to // use on the entire row rowPainter RowPainter + // rowPainterWithAttributes is same as rowPainter, but with attributes + rowPainterWithAttributes RowPainterWithAttributes // rowSeparator is a dummy row that contains the separator columns (dashes // that make up the separator between header/body/footer rowSeparator rowStr @@ -103,6 +81,8 @@ type Table struct { separators map[int]bool // sortBy stores a map of Column sortBy []SortBy + // sortedRowIndices is the output of sorting + sortedRowIndices []int // style contains all the strings used to draw the table, and more style *Style // suppressEmptyColumns hides columns which have no content on all regular @@ -309,12 +289,37 @@ func (t *Table) SetPageSize(numLines int) { t.pager.size = numLines } -// SetRowPainter sets the RowPainter function which determines the colors to use -// on a row. Before rendering, this function is invoked on all rows and the -// color of each row is determined. This color takes precedence over other ways -// to set color (ColumnConfig.Color*, SetColor*()). -func (t *Table) SetRowPainter(painter RowPainter) { - t.rowPainter = painter +// SetRowPainter sets up the function which determines the colors to use on a +// row. Before rendering, this function is invoked on all rows and the color +// of each row is determined. This color takes precedence over other ways to +// set color (ColumnConfig.Color*, SetColor*()). +func (t *Table) SetRowPainter(painter interface{}) { + // TODO: fix interface on major version bump to accept only + // one type of RowPainter: RowPainterWithAttributes renamed to RowPainter + + // reset both so only one is set at any given time + t.rowPainter = nil + t.rowPainterWithAttributes = nil + + // if called as SetRowPainter(RowPainter(func...)) + switch painter.(type) { + case RowPainter: + t.rowPainter = painter.(RowPainter) + return + case RowPainterWithAttributes: + t.rowPainterWithAttributes = painter.(RowPainterWithAttributes) + return + } + + // if called as SetRowPainter(func...) + switch fmt.Sprintf("%T", painter) { + case "func(table.Row) text.Colors": + t.rowPainter = painter.(func(row Row) text.Colors) + return + case "func(table.Row, table.RowAttributes) text.Colors": + t.rowPainterWithAttributes = painter.(func(row Row, attr RowAttributes) text.Colors) + return + } } // SetStyle overrides the DefaultStyle with the provided one. @@ -461,7 +466,7 @@ func (t *Table) getColumnColors(colIdx int, hint renderHint) text.Colors { return colors } } - if t.rowPainter != nil && hint.isRegularNonSeparatorRow() && !t.isIndexColumn(colIdx, hint) { + if t.hasRowPainter() && hint.isRegularNonSeparatorRow() && !t.isIndexColumn(colIdx, hint) { if colors := t.rowsColors[hint.rowNumber-1]; colors != nil { return colors } @@ -717,6 +722,10 @@ func (t *Table) hasHiddenColumns() bool { return false } +func (t *Table) hasRowPainter() bool { + return t.rowPainter != nil || t.rowPainterWithAttributes != nil +} + func (t *Table) hideColumns() map[int]int { colIdxMap := make(map[int]int) numColumns := 0 diff --git a/vendor/github.com/jedib0t/go-pretty/v6/table/writer.go b/vendor/github.com/jedib0t/go-pretty/v6/table/writer.go index 406aaab43..51eee8c72 100644 --- a/vendor/github.com/jedib0t/go-pretty/v6/table/writer.go +++ b/vendor/github.com/jedib0t/go-pretty/v6/table/writer.go @@ -27,7 +27,7 @@ type Writer interface { SetColumnConfigs(configs []ColumnConfig) SetIndexColumn(colNum int) SetOutputMirror(mirror io.Writer) - SetRowPainter(painter RowPainter) + SetRowPainter(painter interface{}) SetStyle(style Style) SetTitle(format string, a ...interface{}) SortBy(sortBy []SortBy) diff --git a/vendor/modules.txt b/vendor/modules.txt index f58687475..6b7f6574d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -313,7 +313,7 @@ github.com/inconshreveable/mousetrap # github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 ## explicit github.com/jbenet/go-context/io -# github.com/jedib0t/go-pretty/v6 v6.6.3 +# github.com/jedib0t/go-pretty/v6 v6.6.4 ## explicit; go 1.17 github.com/jedib0t/go-pretty/v6/table github.com/jedib0t/go-pretty/v6/text