forked from toolshed/abra
build: go 1.24
We were running behind and there were quite some deprecations to update. This was mostly in the upstream copy/pasta package but seems quite minimal.
This commit is contained in:
418
vendor/github.com/charmbracelet/lipgloss/table/resizing.go
generated
vendored
Normal file
418
vendor/github.com/charmbracelet/lipgloss/table/resizing.go
generated
vendored
Normal file
@ -0,0 +1,418 @@
|
||||
package table
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
)
|
||||
|
||||
// resize resizes the table to fit the specified width.
|
||||
//
|
||||
// Given a user defined table width, we must ensure the table is exactly that
|
||||
// width. This must account for all borders, column, separators, and column
|
||||
// data.
|
||||
//
|
||||
// In the case where the table is narrower than the specified table width,
|
||||
// we simply expand the columns evenly to fit the width.
|
||||
// For example, a table with 3 columns takes up 50 characters total, and the
|
||||
// width specified is 80, we expand each column by 10 characters, adding 30
|
||||
// to the total width.
|
||||
//
|
||||
// In the case where the table is wider than the specified table width, we
|
||||
// _could_ simply shrink the columns evenly but this would result in data
|
||||
// being truncated (perhaps unnecessarily). The naive approach could result
|
||||
// in very poor cropping of the table data. So, instead of shrinking columns
|
||||
// evenly, we calculate the median non-whitespace length of each column, and
|
||||
// shrink the columns based on the largest median.
|
||||
//
|
||||
// For example,
|
||||
//
|
||||
// ┌──────┬───────────────┬──────────┐
|
||||
// │ Name │ Age of Person │ Location │
|
||||
// ├──────┼───────────────┼──────────┤
|
||||
// │ Kini │ 40 │ New York │
|
||||
// │ Eli │ 30 │ London │
|
||||
// │ Iris │ 20 │ Paris │
|
||||
// └──────┴───────────────┴──────────┘
|
||||
//
|
||||
// Median non-whitespace length vs column width of each column:
|
||||
//
|
||||
// Name: 4 / 5
|
||||
// Age of Person: 2 / 15
|
||||
// Location: 6 / 10
|
||||
//
|
||||
// The biggest difference is 15 - 2, so we can shrink the 2nd column by 13.
|
||||
func (t *Table) resize() {
|
||||
hasHeaders := len(t.headers) > 0
|
||||
rows := dataToMatrix(t.data)
|
||||
r := newResizer(t.width, t.height, t.headers, rows)
|
||||
r.wrap = t.wrap
|
||||
r.borderColumn = t.borderColumn
|
||||
r.yPaddings = make([][]int, len(r.allRows))
|
||||
|
||||
var allRows [][]string
|
||||
if hasHeaders {
|
||||
allRows = append([][]string{t.headers}, rows...)
|
||||
} else {
|
||||
allRows = rows
|
||||
}
|
||||
|
||||
r.rowHeights = r.defaultRowHeights()
|
||||
|
||||
for i, row := range allRows {
|
||||
r.yPaddings[i] = make([]int, len(row))
|
||||
|
||||
for j := range row {
|
||||
column := &r.columns[j]
|
||||
|
||||
// Making sure we're passing the right index to `styleFunc`. The header row should be `-1` and
|
||||
// the others should start from `0`.
|
||||
rowIndex := i
|
||||
if hasHeaders {
|
||||
rowIndex--
|
||||
}
|
||||
style := t.styleFunc(rowIndex, j)
|
||||
|
||||
topMargin, rightMargin, bottomMargin, leftMargin := style.GetMargin()
|
||||
topPadding, rightPadding, bottomPadding, leftPadding := style.GetPadding()
|
||||
|
||||
totalHorizontalPadding := leftMargin + rightMargin + leftPadding + rightPadding
|
||||
column.xPadding = max(column.xPadding, totalHorizontalPadding)
|
||||
column.fixedWidth = max(column.fixedWidth, style.GetWidth())
|
||||
|
||||
r.rowHeights[i] = max(r.rowHeights[i], style.GetHeight())
|
||||
|
||||
totalVerticalPadding := topMargin + bottomMargin + topPadding + bottomPadding
|
||||
r.yPaddings[i][j] = totalVerticalPadding
|
||||
}
|
||||
}
|
||||
|
||||
// A table width wasn't specified. In this case, detect according to
|
||||
// content width.
|
||||
if r.tableWidth <= 0 {
|
||||
r.tableWidth = r.detectTableWidth()
|
||||
}
|
||||
|
||||
t.widths, t.heights = r.optimizedWidths()
|
||||
}
|
||||
|
||||
// resizerColumn is a column in the resizer.
|
||||
type resizerColumn struct {
|
||||
index int
|
||||
min int
|
||||
max int
|
||||
median int
|
||||
rows [][]string
|
||||
xPadding int // horizontal padding
|
||||
fixedWidth int
|
||||
}
|
||||
|
||||
// resizer is a table resizer.
|
||||
type resizer struct {
|
||||
tableWidth int
|
||||
tableHeight int
|
||||
headers []string
|
||||
allRows [][]string
|
||||
rowHeights []int
|
||||
columns []resizerColumn
|
||||
|
||||
wrap bool
|
||||
borderColumn bool
|
||||
yPaddings [][]int // vertical paddings
|
||||
}
|
||||
|
||||
// newResizer creates a new resizer.
|
||||
func newResizer(tableWidth, tableHeight int, headers []string, rows [][]string) *resizer {
|
||||
r := &resizer{
|
||||
tableWidth: tableWidth,
|
||||
tableHeight: tableHeight,
|
||||
headers: headers,
|
||||
}
|
||||
|
||||
if len(headers) > 0 {
|
||||
r.allRows = append([][]string{headers}, rows...)
|
||||
} else {
|
||||
r.allRows = rows
|
||||
}
|
||||
|
||||
for _, row := range r.allRows {
|
||||
for i, cell := range row {
|
||||
cellLen := lipgloss.Width(cell)
|
||||
|
||||
// Header or first row. Just add as is.
|
||||
if len(r.columns) <= i {
|
||||
r.columns = append(r.columns, resizerColumn{
|
||||
index: i,
|
||||
min: cellLen,
|
||||
max: cellLen,
|
||||
median: cellLen,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
r.columns[i].rows = append(r.columns[i].rows, row)
|
||||
r.columns[i].min = min(r.columns[i].min, cellLen)
|
||||
r.columns[i].max = max(r.columns[i].max, cellLen)
|
||||
}
|
||||
}
|
||||
for j := range r.columns {
|
||||
widths := make([]int, len(r.columns[j].rows))
|
||||
for i, row := range r.columns[j].rows {
|
||||
widths[i] = lipgloss.Width(row[j])
|
||||
}
|
||||
r.columns[j].median = median(widths)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// optimizedWidths returns the optimized column widths and row heights.
|
||||
func (r *resizer) optimizedWidths() (colWidths, rowHeights []int) {
|
||||
if r.maxTotal() <= r.tableWidth {
|
||||
return r.expandTableWidth()
|
||||
}
|
||||
return r.shrinkTableWidth()
|
||||
}
|
||||
|
||||
// detectTableWidth detects the table width.
|
||||
func (r *resizer) detectTableWidth() int {
|
||||
return r.maxCharCount() + r.totalHorizontalPadding() + r.totalHorizontalBorder()
|
||||
}
|
||||
|
||||
// expandTableWidth expands the table width.
|
||||
func (r *resizer) expandTableWidth() (colWidths, rowHeights []int) {
|
||||
colWidths = r.maxColumnWidths()
|
||||
|
||||
for {
|
||||
totalWidth := sum(colWidths) + r.totalHorizontalBorder()
|
||||
if totalWidth >= r.tableWidth {
|
||||
break
|
||||
}
|
||||
|
||||
shorterColumnIndex := 0
|
||||
shorterColumnWidth := math.MaxInt32
|
||||
|
||||
for j, width := range colWidths {
|
||||
if width == r.columns[j].fixedWidth {
|
||||
continue
|
||||
}
|
||||
if width < shorterColumnWidth {
|
||||
shorterColumnWidth = width
|
||||
shorterColumnIndex = j
|
||||
}
|
||||
}
|
||||
|
||||
colWidths[shorterColumnIndex]++
|
||||
}
|
||||
|
||||
rowHeights = r.expandRowHeigths(colWidths)
|
||||
return
|
||||
}
|
||||
|
||||
// shrinkTableWidth shrinks the table width.
|
||||
func (r *resizer) shrinkTableWidth() (colWidths, rowHeights []int) {
|
||||
colWidths = r.maxColumnWidths()
|
||||
|
||||
// Cut width of columns that are way too big.
|
||||
shrinkBiggestColumns := func(veryBigOnly bool) {
|
||||
for {
|
||||
totalWidth := sum(colWidths) + r.totalHorizontalBorder()
|
||||
if totalWidth <= r.tableWidth {
|
||||
break
|
||||
}
|
||||
|
||||
bigColumnIndex := -math.MaxInt32
|
||||
bigColumnWidth := -math.MaxInt32
|
||||
|
||||
for j, width := range colWidths {
|
||||
if width == r.columns[j].fixedWidth {
|
||||
continue
|
||||
}
|
||||
if veryBigOnly {
|
||||
if width >= (r.tableWidth/2) && width > bigColumnWidth { //nolint:mnd
|
||||
bigColumnWidth = width
|
||||
bigColumnIndex = j
|
||||
}
|
||||
} else {
|
||||
if width > bigColumnWidth {
|
||||
bigColumnWidth = width
|
||||
bigColumnIndex = j
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if bigColumnIndex < 0 || colWidths[bigColumnIndex] == 0 {
|
||||
break
|
||||
}
|
||||
colWidths[bigColumnIndex]--
|
||||
}
|
||||
}
|
||||
|
||||
// Cut width of columns that differ the most from the median.
|
||||
shrinkToMedian := func() {
|
||||
for {
|
||||
totalWidth := sum(colWidths) + r.totalHorizontalBorder()
|
||||
if totalWidth <= r.tableWidth {
|
||||
break
|
||||
}
|
||||
|
||||
biggestDiffToMedian := -math.MaxInt32
|
||||
biggestDiffToMedianIndex := -math.MaxInt32
|
||||
|
||||
for j, width := range colWidths {
|
||||
if width == r.columns[j].fixedWidth {
|
||||
continue
|
||||
}
|
||||
diffToMedian := width - r.columns[j].median
|
||||
if diffToMedian > 0 && diffToMedian > biggestDiffToMedian {
|
||||
biggestDiffToMedian = diffToMedian
|
||||
biggestDiffToMedianIndex = j
|
||||
}
|
||||
}
|
||||
|
||||
if biggestDiffToMedianIndex <= 0 || colWidths[biggestDiffToMedianIndex] == 0 {
|
||||
break
|
||||
}
|
||||
colWidths[biggestDiffToMedianIndex]--
|
||||
}
|
||||
}
|
||||
|
||||
shrinkBiggestColumns(true)
|
||||
shrinkToMedian()
|
||||
shrinkBiggestColumns(false)
|
||||
|
||||
return colWidths, r.expandRowHeigths(colWidths)
|
||||
}
|
||||
|
||||
// expandRowHeigths expands the row heights.
|
||||
func (r *resizer) expandRowHeigths(colWidths []int) (rowHeights []int) {
|
||||
rowHeights = r.defaultRowHeights()
|
||||
if !r.wrap {
|
||||
return rowHeights
|
||||
}
|
||||
for i, row := range r.allRows {
|
||||
for j, cell := range row {
|
||||
height := r.detectContentHeight(cell, colWidths[j]-r.xPaddingForCol(j)) + r.xPaddingForCell(i, j)
|
||||
if height > rowHeights[i] {
|
||||
rowHeights[i] = height
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// defaultRowHeights returns the default row heights.
|
||||
func (r *resizer) defaultRowHeights() (rowHeights []int) {
|
||||
rowHeights = make([]int, len(r.allRows))
|
||||
for i := range rowHeights {
|
||||
if i < len(r.rowHeights) {
|
||||
rowHeights[i] = r.rowHeights[i]
|
||||
}
|
||||
if rowHeights[i] < 1 {
|
||||
rowHeights[i] = 1
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// maxColumnWidths returns the maximum column widths.
|
||||
func (r *resizer) maxColumnWidths() []int {
|
||||
maxColumnWidths := make([]int, len(r.columns))
|
||||
for i, col := range r.columns {
|
||||
if col.fixedWidth > 0 {
|
||||
maxColumnWidths[i] = col.fixedWidth
|
||||
} else {
|
||||
maxColumnWidths[i] = col.max + r.xPaddingForCol(col.index)
|
||||
}
|
||||
}
|
||||
return maxColumnWidths
|
||||
}
|
||||
|
||||
// columnCount returns the column count.
|
||||
func (r *resizer) columnCount() int {
|
||||
return len(r.columns)
|
||||
}
|
||||
|
||||
// maxCharCount returns the maximum character count.
|
||||
func (r *resizer) maxCharCount() int {
|
||||
var count int
|
||||
for _, col := range r.columns {
|
||||
if col.fixedWidth > 0 {
|
||||
count += col.fixedWidth - r.xPaddingForCol(col.index)
|
||||
} else {
|
||||
count += col.max
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// maxTotal returns the maximum total width.
|
||||
func (r *resizer) maxTotal() (maxTotal int) {
|
||||
for j, column := range r.columns {
|
||||
if column.fixedWidth > 0 {
|
||||
maxTotal += column.fixedWidth
|
||||
} else {
|
||||
maxTotal += column.max + r.xPaddingForCol(j)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// totalHorizontalPadding returns the total padding.
|
||||
func (r *resizer) totalHorizontalPadding() (totalHorizontalPadding int) {
|
||||
for _, col := range r.columns {
|
||||
totalHorizontalPadding += col.xPadding
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// xPaddingForCol returns the horizontal padding for a column.
|
||||
func (r *resizer) xPaddingForCol(j int) int {
|
||||
if j >= len(r.columns) {
|
||||
return 0
|
||||
}
|
||||
return r.columns[j].xPadding
|
||||
}
|
||||
|
||||
// xPaddingForCell returns the horizontal padding for a cell.
|
||||
func (r *resizer) xPaddingForCell(i, j int) int {
|
||||
if i >= len(r.yPaddings) || j >= len(r.yPaddings[i]) {
|
||||
return 0
|
||||
}
|
||||
return r.yPaddings[i][j]
|
||||
}
|
||||
|
||||
// totalHorizontalBorder returns the total border.
|
||||
func (r *resizer) totalHorizontalBorder() int {
|
||||
return (r.columnCount() * r.borderPerCell()) + r.extraBorder()
|
||||
}
|
||||
|
||||
// borderPerCell returns number of border chars per cell.
|
||||
func (r *resizer) borderPerCell() int {
|
||||
if r.borderColumn {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// extraBorder returns the number of the extra border char at the end of the table.
|
||||
func (r *resizer) extraBorder() int {
|
||||
if r.borderColumn {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// detectContentHeight detects the content height.
|
||||
func (r *resizer) detectContentHeight(content string, width int) (height int) {
|
||||
if width == 0 {
|
||||
return 1
|
||||
}
|
||||
content = strings.ReplaceAll(content, "\r\n", "\n")
|
||||
for _, line := range strings.Split(content, "\n") {
|
||||
height += strings.Count(ansi.Wrap(line, width, ""), "\n") + 1
|
||||
}
|
||||
return
|
||||
}
|
||||
16
vendor/github.com/charmbracelet/lipgloss/table/rows.go
generated
vendored
16
vendor/github.com/charmbracelet/lipgloss/table/rows.go
generated
vendored
@ -111,3 +111,19 @@ func (m *Filter) Rows() int {
|
||||
|
||||
return j
|
||||
}
|
||||
|
||||
// dataToMatrix converts an object that implements the Data interface to a table.
|
||||
func dataToMatrix(data Data) (rows [][]string) {
|
||||
numRows := data.Rows()
|
||||
numCols := data.Columns()
|
||||
rows = make([][]string, numRows)
|
||||
|
||||
for i := 0; i < numRows; i++ {
|
||||
rows[i] = make([]string, numCols)
|
||||
|
||||
for j := 0; j < numCols; j++ {
|
||||
rows[i][j] = data.At(i, j)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
142
vendor/github.com/charmbracelet/lipgloss/table/table.go
generated
vendored
142
vendor/github.com/charmbracelet/lipgloss/table/table.go
generated
vendored
@ -61,6 +61,7 @@ type Table struct {
|
||||
height int
|
||||
useManualHeight bool
|
||||
offset int
|
||||
wrap bool
|
||||
|
||||
// widths tracks the width of each column.
|
||||
widths []int
|
||||
@ -83,6 +84,7 @@ func New() *Table {
|
||||
borderLeft: true,
|
||||
borderRight: true,
|
||||
borderTop: true,
|
||||
wrap: true,
|
||||
data: NewStringData(),
|
||||
}
|
||||
}
|
||||
@ -209,11 +211,20 @@ func (t *Table) Height(h int) *Table {
|
||||
}
|
||||
|
||||
// Offset sets the table rendering offset.
|
||||
//
|
||||
// Warning: you may declare Offset only after setting Rows. Otherwise it will be
|
||||
// ignored.
|
||||
func (t *Table) Offset(o int) *Table {
|
||||
t.offset = o
|
||||
return t
|
||||
}
|
||||
|
||||
// Wrap dictates whether or not the table content should wrap.
|
||||
func (t *Table) Wrap(w bool) *Table {
|
||||
t.wrap = w
|
||||
return t
|
||||
}
|
||||
|
||||
// String returns the table as a string.
|
||||
func (t *Table) String() string {
|
||||
hasHeaders := len(t.headers) > 0
|
||||
@ -231,120 +242,8 @@ func (t *Table) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the widths.
|
||||
t.widths = make([]int, max(len(t.headers), t.data.Columns()))
|
||||
t.heights = make([]int, btoi(hasHeaders)+t.data.Rows())
|
||||
|
||||
// The style function may affect width of the table. It's possible to set
|
||||
// the StyleFunc after the headers and rows. Update the widths for a final
|
||||
// time.
|
||||
for i, cell := range t.headers {
|
||||
t.widths[i] = max(t.widths[i], lipgloss.Width(t.style(HeaderRow, i).Render(cell)))
|
||||
t.heights[0] = max(t.heights[0], lipgloss.Height(t.style(HeaderRow, i).Render(cell)))
|
||||
}
|
||||
|
||||
for r := 0; r < t.data.Rows(); r++ {
|
||||
for i := 0; i < t.data.Columns(); i++ {
|
||||
cell := t.data.At(r, i)
|
||||
|
||||
rendered := t.style(r, i).Render(cell)
|
||||
t.heights[r+btoi(hasHeaders)] = max(t.heights[r+btoi(hasHeaders)], lipgloss.Height(rendered))
|
||||
t.widths[i] = max(t.widths[i], lipgloss.Width(rendered))
|
||||
}
|
||||
}
|
||||
|
||||
// Table Resizing Logic.
|
||||
//
|
||||
// Given a user defined table width, we must ensure the table is exactly that
|
||||
// width. This must account for all borders, column, separators, and column
|
||||
// data.
|
||||
//
|
||||
// In the case where the table is narrower than the specified table width,
|
||||
// we simply expand the columns evenly to fit the width.
|
||||
// For example, a table with 3 columns takes up 50 characters total, and the
|
||||
// width specified is 80, we expand each column by 10 characters, adding 30
|
||||
// to the total width.
|
||||
//
|
||||
// In the case where the table is wider than the specified table width, we
|
||||
// _could_ simply shrink the columns evenly but this would result in data
|
||||
// being truncated (perhaps unnecessarily). The naive approach could result
|
||||
// in very poor cropping of the table data. So, instead of shrinking columns
|
||||
// evenly, we calculate the median non-whitespace length of each column, and
|
||||
// shrink the columns based on the largest median.
|
||||
//
|
||||
// For example,
|
||||
// ┌──────┬───────────────┬──────────┐
|
||||
// │ Name │ Age of Person │ Location │
|
||||
// ├──────┼───────────────┼──────────┤
|
||||
// │ Kini │ 40 │ New York │
|
||||
// │ Eli │ 30 │ London │
|
||||
// │ Iris │ 20 │ Paris │
|
||||
// └──────┴───────────────┴──────────┘
|
||||
//
|
||||
// Median non-whitespace length vs column width of each column:
|
||||
//
|
||||
// Name: 4 / 5
|
||||
// Age of Person: 2 / 15
|
||||
// Location: 6 / 10
|
||||
//
|
||||
// The biggest difference is 15 - 2, so we can shrink the 2nd column by 13.
|
||||
|
||||
width := t.computeWidth()
|
||||
|
||||
if width < t.width && t.width > 0 {
|
||||
// Table is too narrow, expand the columns evenly until it reaches the
|
||||
// desired width.
|
||||
var i int
|
||||
for width < t.width {
|
||||
t.widths[i]++
|
||||
width++
|
||||
i = (i + 1) % len(t.widths)
|
||||
}
|
||||
} else if width > t.width && t.width > 0 {
|
||||
// Table is too wide, calculate the median non-whitespace length of each
|
||||
// column, and shrink the columns based on the largest difference.
|
||||
columnMedians := make([]int, len(t.widths))
|
||||
for c := range t.widths {
|
||||
trimmedWidth := make([]int, t.data.Rows())
|
||||
for r := 0; r < t.data.Rows(); r++ {
|
||||
renderedCell := t.style(r+btoi(hasHeaders), c).Render(t.data.At(r, c))
|
||||
nonWhitespaceChars := lipgloss.Width(strings.TrimRight(renderedCell, " "))
|
||||
trimmedWidth[r] = nonWhitespaceChars + 1
|
||||
}
|
||||
|
||||
columnMedians[c] = median(trimmedWidth)
|
||||
}
|
||||
|
||||
// Find the biggest differences between the median and the column width.
|
||||
// Shrink the columns based on the largest difference.
|
||||
differences := make([]int, len(t.widths))
|
||||
for i := range t.widths {
|
||||
differences[i] = t.widths[i] - columnMedians[i]
|
||||
}
|
||||
|
||||
for width > t.width {
|
||||
index, _ := largest(differences)
|
||||
if differences[index] < 1 {
|
||||
break
|
||||
}
|
||||
|
||||
shrink := min(differences[index], width-t.width)
|
||||
t.widths[index] -= shrink
|
||||
width -= shrink
|
||||
differences[index] = 0
|
||||
}
|
||||
|
||||
// Table is still too wide, begin shrinking the columns based on the
|
||||
// largest column.
|
||||
for width > t.width {
|
||||
index, _ := largest(t.widths)
|
||||
if t.widths[index] < 1 {
|
||||
break
|
||||
}
|
||||
t.widths[index]--
|
||||
width--
|
||||
}
|
||||
}
|
||||
// Do all the sizing calculations for width and height.
|
||||
t.resize()
|
||||
|
||||
var sb strings.Builder
|
||||
|
||||
@ -393,15 +292,6 @@ func (t *Table) String() string {
|
||||
Render(sb.String())
|
||||
}
|
||||
|
||||
// computeWidth computes the width of the table in it's current configuration.
|
||||
func (t *Table) computeWidth() int {
|
||||
width := sum(t.widths) + btoi(t.borderLeft) + btoi(t.borderRight)
|
||||
if t.borderColumn {
|
||||
width += len(t.widths) - 1
|
||||
}
|
||||
return width
|
||||
}
|
||||
|
||||
// computeHeight computes the height of the table in it's current configuration.
|
||||
func (t *Table) computeHeight() int {
|
||||
hasHeaders := len(t.headers) > 0
|
||||
@ -553,13 +443,17 @@ func (t *Table) constructRow(index int, isOverflow bool) string {
|
||||
}
|
||||
|
||||
cellStyle := t.style(index, c)
|
||||
if !t.wrap {
|
||||
length := (cellWidth * height) - cellStyle.GetHorizontalPadding()
|
||||
cell = ansi.Truncate(cell, length, "…")
|
||||
}
|
||||
cells = append(cells, cellStyle.
|
||||
// Account for the margins in the cell sizing.
|
||||
Height(height-cellStyle.GetVerticalMargins()).
|
||||
MaxHeight(height).
|
||||
Width(t.widths[c]-cellStyle.GetHorizontalMargins()).
|
||||
MaxWidth(t.widths[c]).
|
||||
Render(ansi.Truncate(cell, cellWidth*height, "…")))
|
||||
Render(cell))
|
||||
|
||||
if c < t.data.Columns()-1 && t.borderColumn {
|
||||
cells = append(cells, left)
|
||||
|
||||
18
vendor/github.com/charmbracelet/lipgloss/table/util.go
generated
vendored
18
vendor/github.com/charmbracelet/lipgloss/table/util.go
generated
vendored
@ -20,7 +20,7 @@ func max(a, b int) int { //nolint:predeclared
|
||||
return b
|
||||
}
|
||||
|
||||
// min returns the greater of two integers.
|
||||
// min returns the smaller of two integers.
|
||||
func min(a, b int) int { //nolint:predeclared
|
||||
if a < b {
|
||||
return a
|
||||
@ -45,20 +45,8 @@ func median(n []int) int {
|
||||
return 0
|
||||
}
|
||||
if len(n)%2 == 0 {
|
||||
h := len(n) / 2 //nolint:gomnd
|
||||
return (n[h-1] + n[h]) / 2 //nolint:gomnd
|
||||
h := len(n) / 2 //nolint:mnd
|
||||
return (n[h-1] + n[h]) / 2 //nolint:mnd
|
||||
}
|
||||
return n[len(n)/2]
|
||||
}
|
||||
|
||||
// largest returns the largest element and it's index from a slice of integers.
|
||||
func largest(n []int) (int, int) { //nolint:unparam
|
||||
var largest, index int
|
||||
for i, e := range n {
|
||||
if n[i] > n[index] {
|
||||
largest = e
|
||||
index = i
|
||||
}
|
||||
}
|
||||
return index, largest
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user