Involved Source Filesresizing.gorows.go Package table provides a styled table renderer for terminals.util.go
Package-Level Type Names (total 7, in which 5 are exported)
/* sort exporteds by: | */
Data is the interface that wraps the basic methods of a table model. At returns the contents of the cell at the given index. Columns returns the number of columns in the table. Rows returns the number of rows in the table.
*Filter
*StringData
func (*Table).GetData() Data
func DataToMatrix(data Data) (rows [][]string)
func NewFilter(data Data) *Filter
func (*Table).Data(data Data) *Table
Filter applies a filter on some data.dataDatafilterfunc(row int) bool At returns the row at the given index. Columns returns the number of columns in the table. Filter applies the given filter function to the data. Rows returns the number of rows in the table.
*Filter : Data
func NewFilter(data Data) *Filter
func (*Filter).Filter(f func(row int) bool) *Filter
StringData is a string-based implementation of the Data interface.columnsintrows[][]string Append appends the given row to the table. At returns the contents of the cell at the given index. Columns returns the number of columns in the table. Item appends the given row to the table. Rows returns the number of rows in the table.
*StringData : Data
func NewStringData(rows ...[]string) *StringData
func (*StringData).Item(rows ...string) *StringData
StyleFunc is the style function that determines the style of a Cell.
It takes the row and column of the cell as an input and determines the
lipgloss Style to use for that cell position.
Example:
t := table.New().
Headers("Name", "Age").
Row("Kini", 4).
Row("Eli", 1).
Row("Iris", 102).
StyleFunc(func(row, col int) lipgloss.Style {
switch {
case row == 0:
return HeaderStyle
case row%2 == 0:
return EvenRowStyle
default:
return OddRowStyle
}
})
func (*Table).StyleFunc(style StyleFunc) *Table
Table is a type for rendering tables.baseStylelipgloss.Styleborderlipgloss.BorderborderBottomboolborderColumnboolborderHeaderboolborderLeftboolborderRightboolborderRowboolborderStylelipgloss.StyleborderTopbooldataDatafirstVisibleRowIndexintfitContentboolheaders[]stringheightintheights[]intlastVisibleRowIndexintoverflowHeightintstyleFuncStyleFuncuseManualHeightboolwidthintwidths[]intwrapboolyOffsetint BaseStyle sets the base style for the whole table. If you need to set a
background color for the whole table, use this. Border sets the table border. BorderBottom sets the bottom border. BorderColumn sets the column border separator. BorderHeader sets the header separator border. BorderLeft sets the left border. BorderRight sets the right border. BorderRow sets the row border separator. BorderStyle sets the style for the table border. BorderTop sets the top border. ClearRows clears the table rows. Data sets the table data. FirstVisibleRowIndex returns the index of the first visible row in the table. FitContent configures the table to render at its content width—the width
needed to fully fit its content, including cell padding and borders—rather
than expanding to fill the width set via [Table.Width].
When combined with [Table.Width], the configured width acts as a maximum: the
table renders at its content width unless that exceeds the maximum, in which
case it's constrained to the maximum and its content is resized to fit. GetBorderBottom gets the bottom border. GetBorderColumn gets the column border separator. GetBorderHeader gets the header separator border. GetBorderLeft gets the left border. GetBorderRight gets the right border. GetBorderRow gets the row border separator. GetBorderTop gets the top border. GetData returns the table data. GetHeaders returns the table headers. GetHeight returns the height of the table. GetYOffset returns the table rendering offset. Headers sets the table headers. Height sets the table height. LastVisibleRowIndex returns the index of the last visible row in the table. Render returns the table as a string. Row appends a row to the table data. Rows appends rows to the table data. String returns the table as a string. StyleFunc sets the style for a cell based on it's position (row, column). VisibleRows returns the number of visible rows in the table. Width sets the table width, this auto-sizes the columns to fit the width by
either expanding or contracting the widths of each column as a best effort
approach. Wrap dictates whether or not the table content should wrap.
This only applies to data cells. Headers are never wrapped. YOffset sets the table rendering offset. computeHeight computes the height of the table in its current configuration. constructBottomBorder constructs the bottom border for the table given it's current
border configuration and data. constructHeaders constructs the headers for the table given it's current
header configuration and data. constructRow constructs the row for the table given an index and row data
based on the current configuration. If isOverflow is true, the row is
rendered as an overflow row (using ellipsis). constructTopBorder constructs the top border for the table given it's current
border configuration and data. 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. style returns the style for a cell based on it's position (row, column).(*Table) truncateCell(cell string, rowIndex, colIndex int) string
*Table : expvar.Var
*Table : fmt.Stringer
*Table : context.stringer
*Table : os/signal.stringer
*Table : runtime.stringer
func New() *Table
func (*Table).BaseStyle(baseStyle lipgloss.Style) *Table
func (*Table).Border(border lipgloss.Border) *Table
func (*Table).BorderBottom(v bool) *Table
func (*Table).BorderColumn(v bool) *Table
func (*Table).BorderHeader(v bool) *Table
func (*Table).BorderLeft(v bool) *Table
func (*Table).BorderRight(v bool) *Table
func (*Table).BorderRow(v bool) *Table
func (*Table).BorderStyle(style lipgloss.Style) *Table
func (*Table).BorderTop(v bool) *Table
func (*Table).ClearRows() *Table
func (*Table).Data(data Data) *Table
func (*Table).FitContent() *Table
func (*Table).Headers(headers ...string) *Table
func (*Table).Height(h int) *Table
func (*Table).Row(row ...string) *Table
func (*Table).Rows(rows ...[]string) *Table
func (*Table).StyleFunc(style StyleFunc) *Table
func (*Table).Width(w int) *Table
func (*Table).Wrap(w bool) *Table
func (*Table).YOffset(o int) *Table
resizer is a table resizer.allRows[][]stringborderBottomboolborderColumnboolborderHeaderboolborderLeftboolborderRightboolborderRowboolborderTopboolcolumns[]resizerColumnheaders[]stringrowHeights[]inttableHeightinttableWidthintuseManualHeightboolwrapboolyOffsetint // vertical paddings columnCount returns the column count. defaultRowHeights returns the default row heights. detectContentHeight detects the content height. detectTableWidth detects the table width. expandRowHeights expands the row heights. expandTableWidth expands the table width. maxCharCount returns the maximum character count. maxColumnWidths returns the maximum column widths. maxTotal returns the maximum total width. minWidth returns the minimum width floor for a column: horizontal padding
plus at least 1 to ensure the "…" truncation marker or one character is
always visible. A column must never shrink below this floor. optimizedWidths returns the optimized column widths and row heights. shrinkTableWidth shrinks the table width. totalHorizontalBorder returns the total border. totalHorizontalPadding returns the total padding. visibleRowIndexes calculates the indexes of the first and last visible rows
according to the current yOffset and tableHeight. If the table height is not
fixed or if the last row is visible, lastVisibleRowIndex will be -2.
Note that the calculated indexes ignore the header row, so 0 corresponds to
the first data row. The header row is always visible if it exists.
The last return value is the number of cells that the overflow row should
take up for the table to fill the available height. xPaddingForCol returns the horizontal padding for a column. yPaddingForCell returns the horizontal padding for a cell.
func newResizer(tableWidth, tableHeight int, headers []string, rows [][]string) *resizer