Merge pull request #349 from cloudbase/dependabot/go_modules/github.com/jedib0t/go-pretty/v6-6.6.6

Bump github.com/jedib0t/go-pretty/v6 from 6.6.5 to 6.6.6
This commit is contained in:
Gabriel 2025-02-11 10:49:19 +02:00 committed by GitHub
commit 09e3df25ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 116 additions and 61 deletions

2
go.mod
View file

@ -19,7 +19,7 @@ require (
github.com/gorilla/handlers v1.5.2
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.4-0.20240702125206-a62d9d2a8413
github.com/jedib0t/go-pretty/v6 v6.6.5
github.com/jedib0t/go-pretty/v6 v6.6.6
github.com/juju/clock v1.1.1
github.com/juju/retry v1.0.1
github.com/manifoldco/promptui v0.9.0

4
go.sum
View file

@ -90,8 +90,8 @@ github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jedib0t/go-pretty/v6 v6.6.5 h1:9PgMJOVBedpgYLI56jQRJYqngxYAAzfEUua+3NgSqAo=
github.com/jedib0t/go-pretty/v6 v6.6.5/go.mod h1:Uq/HrbhuFty5WSVNfjpQQe47x16RwVGXIveNGEyGtHs=
github.com/jedib0t/go-pretty/v6 v6.6.6 h1:LyezkL+1SuqH2z47e5IMQkYUIcs2BD+MnpdPRiRcN0c=
github.com/jedib0t/go-pretty/v6 v6.6.6/go.mod h1:YwC5CE4fJ1HFUDeivSV1r//AmANFHyqczZk+U6BDALU=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=

View file

@ -2,6 +2,7 @@ package table
import (
"fmt"
"sort"
"strings"
"unicode"
@ -57,35 +58,93 @@ func (t *Table) extractMaxColumnLengths(rows []rowStr, hint renderHint) {
}
func (t *Table) extractMaxColumnLengthsFromRow(row rowStr, mci mergedColumnIndices) {
for colIdx, colStr := range row {
for colIdx := 0; colIdx < len(row); colIdx++ {
colStr := row[colIdx]
longestLineLen := text.LongestLineLen(colStr)
maxColWidth := t.getColumnWidthMax(colIdx)
if maxColWidth > 0 && maxColWidth < longestLineLen {
longestLineLen = maxColWidth
}
mergedColumnsLength := mci.mergedLength(colIdx, t.maxColumnLengths)
if longestLineLen > mergedColumnsLength {
if mergedColumnsLength > 0 {
t.extractMaxColumnLengthsFromRowForMergedColumns(colIdx, longestLineLen, mci)
} else {
t.maxColumnLengths[colIdx] = longestLineLen
if mergeEndIndex, ok := mci[colIdx]; ok {
startIndexMap := t.maxMergedColumnLengths[mergeEndIndex]
if startIndexMap == nil {
startIndexMap = make(map[int]int)
t.maxMergedColumnLengths[mergeEndIndex] = startIndexMap
}
} else if maxColWidth == 0 && longestLineLen > t.maxColumnLengths[colIdx] {
if longestLineLen > startIndexMap[colIdx] {
startIndexMap[colIdx] = longestLineLen
}
colIdx = mergeEndIndex
} else if longestLineLen > t.maxColumnLengths[colIdx] {
t.maxColumnLengths[colIdx] = longestLineLen
}
}
}
func (t *Table) extractMaxColumnLengthsFromRowForMergedColumns(colIdx int, mergedColumnLength int, mci mergedColumnIndices) {
numMergedColumns := mci.len(colIdx)
mergedColumnLength -= (numMergedColumns - 1) * text.StringWidthWithoutEscSequences(t.style.Box.MiddleSeparator)
maxLengthSplitAcrossColumns := mergedColumnLength / numMergedColumns
if maxLengthSplitAcrossColumns > t.maxColumnLengths[colIdx] {
t.maxColumnLengths[colIdx] = maxLengthSplitAcrossColumns
}
for otherColIdx := range mci[colIdx] {
if maxLengthSplitAcrossColumns > t.maxColumnLengths[otherColIdx] {
t.maxColumnLengths[otherColIdx] = maxLengthSplitAcrossColumns
// reBalanceMaxMergedColumnLengths tries to re-balance the merged column lengths
// across all columns. It does this from the lowest end index to the highest,
// and within that set from the highest start index to the lowest. It
// distributes the length across the columns not already exceeding the average.
func (t *Table) reBalanceMaxMergedColumnLengths() {
endIndexKeys, startIndexKeysMap := getSortedKeys(t.maxMergedColumnLengths)
middleSepLen := text.StringWidthWithoutEscSequences(t.style.Box.MiddleSeparator)
for _, endIndexKey := range endIndexKeys {
startIndexKeys := startIndexKeysMap[endIndexKey]
for idx := len(startIndexKeys) - 1; idx >= 0; idx-- {
startIndexKey := startIndexKeys[idx]
columnBalanceMap := map[int]struct{}{}
for index := startIndexKey; index <= endIndexKey; index++ {
columnBalanceMap[index] = struct{}{}
}
mergedColumnLength := t.maxMergedColumnLengths[endIndexKey][startIndexKey] -
((len(columnBalanceMap) - 1) * middleSepLen)
// keep reducing the set of columns until the remainder are the ones less than
// the average of the remaining length (total merged length - all lengths > average)
for {
if mergedColumnLength <= 0 { // already exceeded the merged length
columnBalanceMap = map[int]struct{}{}
break
}
numMergedColumns := len(columnBalanceMap)
maxLengthSplitAcrossColumns := mergedColumnLength / numMergedColumns
mapReduced := false
for mergedColumn := range columnBalanceMap {
maxColumnLength := t.maxColumnLengths[mergedColumn]
if maxColumnLength >= maxLengthSplitAcrossColumns {
mapReduced = true
mergedColumnLength -= maxColumnLength
delete(columnBalanceMap, mergedColumn)
}
}
if !mapReduced {
break
}
}
// act on any remaining columns that need balancing
if len(columnBalanceMap) > 0 {
// remove the max column sizes from the remaining amount to balance, then
// share out the remainder amongst the columns.
numRebalancedColumns := len(columnBalanceMap)
balanceColumns := make([]int, 0, numRebalancedColumns)
for balanceColumn := range columnBalanceMap {
mergedColumnLength -= t.maxColumnLengths[balanceColumn]
balanceColumns = append(balanceColumns, balanceColumn)
}
// pad out the columns one by one
sort.Ints(balanceColumns)
columnLengthRemaining := mergedColumnLength
columnsRemaining := numRebalancedColumns
for index := 0; index < numRebalancedColumns; index++ {
balancedSpace := columnLengthRemaining / columnsRemaining
balanceColumn := balanceColumns[index]
t.maxColumnLengths[balanceColumn] += balancedSpace
columnLengthRemaining -= balancedSpace
columnsRemaining--
}
}
}
}
}
@ -136,6 +195,7 @@ func (t *Table) initForRenderColumnConfigs() {
func (t *Table) initForRenderColumnLengths() {
t.maxColumnLengths = make([]int, t.numColumns)
t.maxMergedColumnLengths = make(map[int]map[int]int)
t.extractMaxColumnLengths(t.rowsHeader, renderHint{isHeaderRow: true})
t.extractMaxColumnLengths(t.rows, renderHint{})
t.extractMaxColumnLengths(t.rowsFooter, renderHint{isFooterRow: true})
@ -147,6 +207,7 @@ func (t *Table) initForRenderColumnLengths() {
t.maxColumnLengths[colIdx] = minWidth
}
}
t.reBalanceMaxMergedColumnLengths()
}
func (t *Table) initForRenderHideColumns() {

View file

@ -36,6 +36,9 @@ type Table struct {
indexColumn int
// maxColumnLengths stores the length of the longest line in each column
maxColumnLengths []int
// maxMergedColumnLengths stores the longest lengths for merged columns
// endIndex -> startIndex -> maxMergedLength
maxMergedColumnLengths map[int]map[int]int
// maxRowLength stores the length of the longest row
maxRowLength int
// numColumns stores the (max.) number of columns seen
@ -619,19 +622,19 @@ func (t *Table) getMergedColumnIndices(row rowStr, hint renderHint) mergedColumn
mci := make(mergedColumnIndices)
for colIdx := 0; colIdx < t.numColumns-1; colIdx++ {
// look backward
for otherColIdx := colIdx - 1; colIdx >= 0 && otherColIdx >= 0; otherColIdx-- {
if row[colIdx] != row[otherColIdx] {
for otherColIdx := colIdx + 1; otherColIdx < len(row); otherColIdx++ {
colsEqual := row[colIdx] == row[otherColIdx]
if !colsEqual {
lastEqual := otherColIdx - 1
if colIdx != lastEqual {
mci[colIdx] = lastEqual
colIdx = lastEqual
}
break
} else if colsEqual && otherColIdx == len(row)-1 {
mci[colIdx] = otherColIdx
colIdx = otherColIdx
}
mci.safeAppend(colIdx, otherColIdx)
}
// look forward
for otherColIdx := colIdx + 1; colIdx < len(row) && otherColIdx < len(row); otherColIdx++ {
if row[colIdx] != row[otherColIdx] {
break
}
mci.safeAppend(colIdx, otherColIdx)
}
}
return mci

View file

@ -2,6 +2,7 @@ package table
import (
"reflect"
"sort"
)
// AutoIndexColumnID returns a unique Column ID/Name for the given Column Number.
@ -40,33 +41,7 @@ func isNumber(x interface{}) bool {
return false
}
type mergedColumnIndices map[int]map[int]bool
func (m mergedColumnIndices) mergedLength(colIdx int, maxColumnLengths []int) int {
mergedLength := maxColumnLengths[colIdx]
for otherColIdx := range m[colIdx] {
mergedLength += maxColumnLengths[otherColIdx]
}
return mergedLength
}
func (m mergedColumnIndices) len(colIdx int) int {
return len(m[colIdx]) + 1
}
func (m mergedColumnIndices) safeAppend(colIdx, otherColIdx int) {
// map
if m[colIdx] == nil {
m[colIdx] = make(map[int]bool)
}
m[colIdx][otherColIdx] = true
// reverse map
if m[otherColIdx] == nil {
m[otherColIdx] = make(map[int]bool)
}
m[otherColIdx][colIdx] = true
}
type mergedColumnIndices map[int]int
func objAsSlice(in interface{}) []interface{} {
var out []interface{}
@ -110,3 +85,19 @@ func objIsSlice(in interface{}) bool {
k := reflect.TypeOf(in).Kind()
return k == reflect.Slice || k == reflect.Array
}
func getSortedKeys(input map[int]map[int]int) ([]int, map[int][]int) {
keys := make([]int, 0, len(input))
subkeysMap := make(map[int][]int)
for key, subMap := range input {
keys = append(keys, key)
subkeys := make([]int, 0, len(subMap))
for subkey := range subMap {
subkeys = append(subkeys, subkey)
}
sort.Ints(subkeys)
subkeysMap[key] = subkeys
}
sort.Ints(keys)
return keys, subkeysMap
}

4
vendor/modules.txt vendored
View file

@ -121,8 +121,8 @@ github.com/gorilla/websocket
# github.com/inconshreveable/mousetrap v1.1.0
## explicit; go 1.18
github.com/inconshreveable/mousetrap
# github.com/jedib0t/go-pretty/v6 v6.6.5
## explicit; go 1.17
# github.com/jedib0t/go-pretty/v6 v6.6.6
## explicit; go 1.18
github.com/jedib0t/go-pretty/v6/table
github.com/jedib0t/go-pretty/v6/text
# github.com/jinzhu/inflection v1.0.0