Documentation ¶
Overview ¶
Package table contains a Bubble Tea component for an interactive and customizable table.
The simplest useful table can be created with table.New(...).WithRows(...). Row data should map to the column keys, as shown below. Note that extra data will simply not be shown, while missing data will be safely blank in the row's cell.
const ( // This is not necessary, but recommended to avoid typos columnKeyName = "name" columnKeyCount = "count" ) // Define the columns and how they appear columns := []table.Column{ table.NewColumn(columnKeyName, "Name", 10), table.NewColumn(columnKeyCount, "Count", 6), } // Define the data that will be in the table, mapping to the column keys rows := []table.Row{ table.NewRow(table.RowData{ columnKeyName: "Cheeseburger", columnKeyCount: 3, }), table.NewRow(table.RowData{ columnKeyName: "Fries", columnKeyCount: 2, }), } // Create the table tbl := table.New(columns).WithRows(rows) // Use it like any Bubble Tea component in your view tbl.View()
Index ¶
- type Border
- type Column
- type KeyMap
- type Model
- func (m Model) Border(border Border) Model
- func (m Model) BorderDefault() Model
- func (m *Model) CurrentPage() int
- func (m Model) Filtered(filtered bool) Model
- func (m Model) Focused(focused bool) Model
- func (m *Model) GetCanFilter() bool
- func (m *Model) GetColumnSorting() []SortColumn
- func (m *Model) GetCurrentFilter() string
- func (m *Model) GetIsFilterActive() bool
- func (m Model) GetVisibleRows() []Row
- func (m Model) HeaderStyle(style lipgloss.Style) Model
- func (m Model) HighlightStyle(style lipgloss.Style) Model
- func (m Model) HighlightedRow() Row
- func (m Model) Init() tea.Cmd
- func (m Model) KeyMap() KeyMap
- func (m *Model) MaxPages() int
- func (m Model) PageDown() Model
- func (m Model) PageFirst() Model
- func (m Model) PageLast() Model
- func (m *Model) PageSize() int
- func (m Model) PageUp() Model
- func (m Model) SelectableRows(selectable bool) Model
- func (m Model) SelectedRows() []Row
- func (m Model) SortByAsc(columnKey string) Model
- func (m Model) SortByDesc(columnKey string) Model
- func (m Model) StartFilterTyping() Model
- func (m Model) ThenSortByAsc(columnKey string) Model
- func (m Model) ThenSortByDesc(columnKey string) Model
- func (m *Model) TotalRows() int
- func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)
- func (m Model) View() string
- func (m *Model) VisibleIndices() (start, end int)
- func (m Model) WithBaseStyle(style lipgloss.Style) Model
- func (m Model) WithColumns(columns []Column) Model
- func (m Model) WithCurrentPage(currentPage int) Model
- func (m Model) WithFilterInput(input textinput.Model) Model
- func (m Model) WithFooterVisibility(visibility bool) Model
- func (m Model) WithHighlightedRow(index int) Model
- func (m Model) WithKeyMap(keyMap KeyMap) Model
- func (m Model) WithNoPagination() Model
- func (m Model) WithPageSize(pageSize int) Model
- func (m Model) WithRows(rows []Row) Model
- func (m Model) WithSelectedText(unselected, selected string) Model
- func (m Model) WithStaticFooter(footer string) Model
- func (m Model) WithTargetWidth(totalWidth int) Model
- type Row
- type RowData
- type SortColumn
- type SortDirection
- type StyledCell
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Border ¶ added in v0.2.0
type Border struct { Top string Left string Right string Bottom string TopRight string TopLeft string BottomRight string BottomLeft string TopJunction string LeftJunction string RightJunction string BottomJunction string InnerJunction string InnerDivider string // contains filtered or unexported fields }
Border defines the borders in and around the table.
type Column ¶ added in v0.3.0
type Column struct {
// contains filtered or unexported fields
}
Column is a column in the table.
func NewColumn ¶ added in v0.3.0
NewColumn creates a new fixed-width column with the given information.
func NewFlexColumn ¶ added in v0.8.0
NewFlexColumn creates a new flexible width column that tries to fill in the total table width. If multiple flex columns exist, each will measure against each other depending on their flexFactor. For example, if both have a flexFactor of 1, they will have equal width. If one has a flexFactor of 1 and the other has a flexFactor of 3, the second will be 3 times larger than the first. You must use WithTargetWidth if you have any flex columns, so that the table knows how much width it should fill.
func (Column) WithFiltered ¶ added in v0.7.0
WithFiltered sets whether the column should be considered for filtering (true) or not (false).
type KeyMap ¶ added in v0.4.0
type KeyMap struct { RowDown key.Binding RowUp key.Binding RowSelectToggle key.Binding PageDown key.Binding PageUp key.Binding PageFirst key.Binding PageLast key.Binding // Filter allows the user to start typing and filter the rows. Filter key.Binding // FilterBlur is the key that stops the user's input from typing into the filter. FilterBlur key.Binding // FilterClear will clear the filter while it's blurred. FilterClear key.Binding }
KeyMap defines the keybindings for the table when it's focused.
func DefaultKeyMap ¶ added in v0.4.0
func DefaultKeyMap() KeyMap
DefaultKeyMap returns a set of sensible defaults for controlling a focused table.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is the main table model. Create using New().
func (Model) BorderDefault ¶ added in v0.2.0
BorderDefault uses the basic square border, useful to reset the border if it was changed somehow.
func (*Model) CurrentPage ¶ added in v0.5.0
CurrentPage returns the current page that the table is on, starting from an index of 1.
func (Model) Filtered ¶ added in v0.7.0
Filtered allows the table to show rows that match the filter.
func (Model) Focused ¶
Focused allows the table to show highlighted rows and take in controls of up/down/space/etc to let the user navigate the table and interact with it.
func (*Model) GetCanFilter ¶ added in v0.11.1
GetCanFilter returns true if the table enables filtering at all. This does not say whether a filter is currently active, only that the feature is enabled.
func (*Model) GetColumnSorting ¶ added in v0.11.1
func (m *Model) GetColumnSorting() []SortColumn
GetColumnSorting returns the current sorting rules for the table as a list of SortColumns, which are applied from first to last. This means that data will be grouped by the later elements in the list. The returned list is a copy and modifications will have no effect.
func (*Model) GetCurrentFilter ¶ added in v0.11.1
GetCurrentFilter returns the current filter text being applied, or an empty string if none is applied.
func (*Model) GetIsFilterActive ¶ added in v0.11.1
GetIsFilterActive returns true if the table is currently being filtered. This does not say whether the table CAN be filtered, only whether or not a filter is actually currently being applied.
func (Model) GetVisibleRows ¶ added in v0.7.0
GetVisibleRows return sorted and filtered rows.
func (Model) HeaderStyle ¶
HeaderStyle sets the style to apply to the header text, such as color or bold.
func (Model) HighlightStyle ¶
HighlightStyle sets a custom style to use when the row is being highlighted by the cursor.
func (Model) HighlightedRow ¶
HighlightedRow returns the full Row that's currently highlighted by the user.
func (*Model) MaxPages ¶ added in v0.5.0
MaxPages returns the maximum number of pages that are visible.
func (Model) PageDown ¶ added in v0.9.0
PageDown goes to the next page of a paginated table, wrapping to the first page if the table is already on the last page.
func (*Model) PageSize ¶ added in v0.5.0
PageSize returns the current page size for the table, or 0 if there is no pagination enabled.
func (Model) PageUp ¶ added in v0.9.0
PageUp goes to the previous page of a paginated table, wrapping to the last page if the table is already on the first page.
func (Model) SelectableRows ¶
SelectableRows sets whether or not rows are selectable. If set, adds a column in the front that acts as a checkbox and responds to controls if Focused.
func (Model) SelectedRows ¶
SelectedRows returns all rows that have been set as selected by the user.
func (Model) SortByAsc ¶ added in v0.6.0
SortByAsc sets the main sorting column to the given key, in ascending order. If a previous sort was used, it is replaced by the given column each time this function is called. Values are sorted as numbers if possible, or just as simple string comparisons if not numbers.
func (Model) SortByDesc ¶ added in v0.6.0
SortByDesc sets the main sorting column to the given key, in descending order. If a previous sort was used, it is replaced by the given column each time this function is called. Values are sorted as numbers if possible, or just as simple string comparisons if not numbers.
func (Model) StartFilterTyping ¶ added in v0.7.0
StartFilterTyping focuses the text input to allow user typing to filter.
func (Model) ThenSortByAsc ¶ added in v0.6.0
ThenSortByAsc provides a secondary sort after the first, in ascending order. Can be chained multiple times, applying to smaller subgroups each time.
func (Model) ThenSortByDesc ¶ added in v0.6.0
ThenSortByDesc provides a secondary sort after the first, in descending order. Can be chained multiple times, applying to smaller subgroups each time.
func (*Model) TotalRows ¶ added in v0.5.0
TotalRows returns the current total row count of the table. If the table is paginated, this is the total number of rows across all pages.
func (Model) View ¶
View renders the table. It does not end in a newline, so that it can be composed with other elements more consistently.
func (*Model) VisibleIndices ¶ added in v0.5.0
VisibleIndices returns the current visible rows by their 0 based index. Useful for custom pagination footers.
func (Model) WithBaseStyle ¶ added in v0.6.3
WithBaseStyle applies a base style as the default for everything in the table. This is useful for border colors, default alignment, default color, etc.
func (Model) WithColumns ¶ added in v0.10.0
WithColumns sets the visible columns for the table, so that columns can be added/removed/resized or headers rewritten.
func (Model) WithCurrentPage ¶ added in v0.9.0
WithCurrentPage sets the current page (1 as the first page) of a paginated table, bounded to the total number of pages. The current selected row will be set to the top row of the page if the page changed.
func (Model) WithFilterInput ¶ added in v0.12.0
WithFilterInput makes the table use the provided text input bubble for filtering rather than using the built-in default. This allows for external text input controls to be used.
func (Model) WithFooterVisibility ¶ added in v0.12.0
WithFooterVisibility sets the visibility of the footer.
func (Model) WithHighlightedRow ¶ added in v0.8.1
WithHighlightedRow sets the highlighted row to the given index.
func (Model) WithKeyMap ¶ added in v0.4.0
WithKeyMap sets the key map to use for controls when focused.
func (Model) WithNoPagination ¶ added in v0.5.0
WithNoPagination disable pagination in the table.
func (Model) WithPageSize ¶ added in v0.5.0
WithPageSize enables pagination using the given page size. This can be called again at any point to resize the height of the table.
func (Model) WithSelectedText ¶ added in v0.6.0
WithSelectedText describes what text to show when selectable rows are enabled. The selectable column header will use the selected text string.
func (Model) WithStaticFooter ¶ added in v0.4.0
WithStaticFooter adds a footer that only displays the given text.
func (Model) WithTargetWidth ¶ added in v0.8.0
WithTargetWidth sets the total target width of the table, including borders. This only takes effect when using flex columns. When using flex columns, columns will stretch to fill out to the total width given here.
type Row ¶
Row represents a row in the table with some data keyed to the table columns> Can have a style applied to it such as color/bold. Create using NewRow().
type RowData ¶
type RowData map[string]interface{}
RowData is a map of string column keys to interface{} data. Data with a key that matches a column key will be displayed. Data with a key that does not match a column key will not be displayed, but will remain attached to the Row. This can be useful for attaching hidden metadata for future reference when retrieving rows.
type SortColumn ¶ added in v0.11.1
type SortColumn struct { ColumnKey string Direction SortDirection }
SortColumn describes which column should be sorted and how.
type SortDirection ¶ added in v0.11.1
type SortDirection int
SortDirection indicates whether a column should sort by ascending or descending.
const ( // SortDirectionAsc indicates the column should be in ascending order. SortDirectionAsc SortDirection = iota // SortDirectionDesc indicates the column should be in descending order. SortDirectionDesc )
type StyledCell ¶ added in v0.5.0
StyledCell represents a cell in the table that has a particular style applied. The cell style takes highest precedence and will overwrite more general styles from the row, column, or table as a whole. This style should be generally limited to colors, font style, and alignments - spacing style such as margin will break the table format.
func NewStyledCell ¶ added in v0.5.0
func NewStyledCell(data interface{}, style lipgloss.Style) StyledCell
NewStyledCell creates an entry that can be set in the row data and show as styled with the given style.