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
- func (c Column) Filterable() bool
- func (c Column) FlexFactor() int
- func (c Column) FmtString() string
- func (c Column) IsFlex() bool
- func (c Column) Key() string
- func (c Column) Style() lipgloss.Style
- func (c Column) Title() string
- func (c Column) Width() int
- func (c Column) WithFiltered(filterable bool) Column
- func (c Column) WithFormatString(fmtString string) Column
- func (c Column) WithStyle(style lipgloss.Style) Column
- type KeyMap
- type Model
- func (m Model) Border(border Border) Model
- func (m Model) BorderDefault() Model
- func (m Model) BorderRounded() Model
- func (m *Model) CurrentPage() int
- func (m Model) Filtered(filtered bool) Model
- func (m Model) Focused(focused bool) Model
- func (m Model) FullHelp() [][]key.Binding
- func (m *Model) GetCanFilter() bool
- func (m *Model) GetColumnSorting() []SortColumn
- func (m *Model) GetCurrentFilter() string
- func (m *Model) GetFocused() bool
- func (m *Model) GetFooterVisibility() bool
- func (m *Model) GetHeaderVisibility() bool
- func (m *Model) GetHighlightedRowIndex() int
- func (m *Model) GetHorizontalScrollColumnOffset() int
- func (m *Model) GetIsFilterActive() bool
- func (m *Model) GetIsFilterInputFocused() bool
- func (m *Model) GetLastUpdateUserEvents() []UserEvent
- func (m *Model) GetPaginationWrapping() 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) ScrollLeft() Model
- func (m Model) ScrollRight() Model
- func (m Model) SelectableRows(selectable bool) Model
- func (m Model) SelectedRows() []Row
- func (m Model) ShortHelp() []key.Binding
- 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) WithAdditionalFullHelpKeys(keys []key.Binding) Model
- func (m Model) WithAdditionalShortHelpKeys(keys []key.Binding) Model
- func (m Model) WithAllRowsDeselected() Model
- 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) WithFilterInputValue(value string) Model
- func (m Model) WithFooterVisibility(visibility bool) Model
- func (m Model) WithHeaderVisibility(visibility bool) Model
- func (m Model) WithHighlightedRow(index int) Model
- func (m Model) WithHorizontalFreezeColumnCount(columnsToFreeze int) Model
- func (m Model) WithKeyMap(keyMap KeyMap) Model
- func (m Model) WithMaxTotalWidth(maxTotalWidth int) Model
- func (m Model) WithMinimumHeight(minimumHeight int) Model
- func (m Model) WithMissingDataIndicator(str string) Model
- func (m Model) WithMissingDataIndicatorStyled(styled StyledCell) Model
- func (m Model) WithMultiline(multiline bool) Model
- func (m Model) WithNoPagination() Model
- func (m Model) WithPageSize(pageSize int) Model
- func (m Model) WithPaginationWrapping(wrapping bool) Model
- func (m Model) WithRowStyleFunc(f func(RowStyleFuncInput) lipgloss.Style) 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 RowStyleFuncInput
- type SortColumn
- type SortDirection
- type StyledCell
- type UserEvent
- type UserEventFilterInputFocused
- type UserEventFilterInputUnfocused
- type UserEventHighlightedIndexChanged
- type UserEventRowSelectToggled
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) Filterable ¶ added in v0.15.5
Filterable returns whether the column is filterable.
func (Column) FlexFactor ¶ added in v0.15.5
FlexFactor returns the flex factor of the column.
func (Column) WithFiltered ¶ added in v0.7.0
WithFiltered sets whether the column should be considered for filtering (true) or not (false).
func (Column) WithFormatString ¶ added in v0.14.6
WithFormatString sets the format string used by fmt.Sprintf to display the data. If not set, the default is "%v" for all data types. Intended mainly for numeric formatting.
Since data is of the interface{} type, make sure that all data in the column is of the expected type or the format may fail. For example, hardcoding '3' instead of '3.0' and using '%.2f' will fail because '3' is an integer.
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 // ScrollRight will move one column to the right when overflow occurs. ScrollRight key.Binding // ScrollLeft will move one column to the left when overflow occurs. ScrollLeft 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 with help text.
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) BorderRounded ¶ added in v0.13.5
BorderRounded uses a thin, rounded border.
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) FullHelp ¶ added in v0.17.1
FullHelp returns a multi row view of all the helpkeys that are defined. Needed to fullfil the 'help.Model' interface. Also appends all user defined extra keys to the help.
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) GetFocused ¶ added in v0.13.4
GetFocused returns whether or not the table is focused and is receiving inputs.
func (*Model) GetFooterVisibility ¶ added in v0.15.3
GetFooterVisibility returns true if the footer has been set to visible (default) or false if the footer has been set to hidden. Note that even if the footer is visible it will only be rendered if it has contents.
func (*Model) GetHeaderVisibility ¶ added in v0.13.5
GetHeaderVisibility returns true if the header has been set to visible (default) or false if the header has been set to hidden.
func (*Model) GetHighlightedRowIndex ¶ added in v0.13.4
GetHighlightedRowIndex returns the index of the Row that's currently highlighted by the user.
func (*Model) GetHorizontalScrollColumnOffset ¶ added in v0.13.4
GetHorizontalScrollColumnOffset returns how many columns to the right the table has been scrolled. 0 means the table is all the way to the left, which is the starting default.
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) GetIsFilterInputFocused ¶ added in v0.14.5
GetIsFilterInputFocused returns true if the table's built-in filter input is currently focused.
func (*Model) GetLastUpdateUserEvents ¶ added in v0.14.0
GetLastUpdateUserEvents returns a list of events that happened due to user input in the last Update call. This is useful to look for triggers such as whether the user moved to a new highlighted row.
func (*Model) GetPaginationWrapping ¶ added in v0.14.2
GetPaginationWrapping returns true if pagination wrapping is enabled, or false if disabled. If disabled, navigating through pages will stop at the first and last pages.
func (*Model) GetVisibleRows ¶ added in v0.7.0
GetVisibleRows returns 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. This should not be used with WithRowStyleFunc. Instead, use the IsHighlighted field in the style function.
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) ScrollLeft ¶ added in v0.13.0
ScrollLeft moves one column to the left. Use with WithMaxTotalWidth.
func (Model) ScrollRight ¶ added in v0.13.0
ScrollRight moves one column to the right. Use with WithMaxTotalWidth.
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) ShortHelp ¶ added in v0.17.1
ShortHelp just returns a single row of help views. Needed to fullfil the 'help.Model' interface. Also appends all user defined extra keys to the help.
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) WithAdditionalFullHelpKeys ¶ added in v0.17.1
WithAdditionalFullHelpKeys enables you to add more keybindings to the 'full help' view.
func (Model) WithAdditionalShortHelpKeys ¶ added in v0.17.1
WithAdditionalShortHelpKeys enables you to add more keybindings to the 'short help' view.
func (Model) WithAllRowsDeselected ¶ added in v0.15.0
WithAllRowsDeselected deselects any rows that are currently selected.
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) WithFilterInputValue ¶ added in v0.14.3
WithFilterInputValue sets the filter value to the given string, immediately applying it as if the user had typed it in. Useful for external filter inputs that are not necessarily a text input.
func (Model) WithFooterVisibility ¶ added in v0.12.0
WithFooterVisibility sets the visibility of the footer.
func (Model) WithHeaderVisibility ¶ added in v0.13.5
WithHeaderVisibility sets the visibility of the header.
func (Model) WithHighlightedRow ¶ added in v0.8.1
WithHighlightedRow sets the highlighted row to the given index.
func (Model) WithHorizontalFreezeColumnCount ¶ added in v0.13.0
WithHorizontalFreezeColumnCount freezes the given number of columns to the left side. This is useful for things like ID or Name columns that should always be visible even when scrolling.
func (Model) WithKeyMap ¶ added in v0.4.0
WithKeyMap sets the key map to use for controls when focused.
func (Model) WithMaxTotalWidth ¶ added in v0.13.0
WithMaxTotalWidth sets the maximum total width that the table should render. If this width is exceeded by either the target width or by the total width of all the columns (including borders!), anything extra will be treated as overflow and horizontal scrolling will be enabled to see the rest.
func (Model) WithMinimumHeight ¶ added in v0.15.4
WithMinimumHeight sets the minimum total height of the table, including borders.
func (Model) WithMissingDataIndicator ¶ added in v0.13.5
WithMissingDataIndicator sets an indicator to use when data for a column is not found in a given row. Note that this is for completely missing data, an empty string or other zero value that is explicitly set is not considered to be missing.
func (Model) WithMissingDataIndicatorStyled ¶ added in v0.13.5
func (m Model) WithMissingDataIndicatorStyled(styled StyledCell) Model
WithMissingDataIndicatorStyled sets a styled indicator to use when data for a column is not found in a given row. Note that this is for completely missing data, an empty string or other zero value that is explicitly set is not considered to be missing.
func (Model) WithMultiline ¶ added in v0.15.6
WithMultiline sets whether or not to wrap text in cells to multiple lines.
func (Model) WithNoPagination ¶ added in v0.5.0
WithNoPagination disables 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) WithPaginationWrapping ¶ added in v0.14.2
WithPaginationWrapping sets whether to wrap around from the beginning to the end when navigating through pages. Defaults to true.
func (Model) WithRowStyleFunc ¶ added in v0.16.0
func (m Model) WithRowStyleFunc(f func(RowStyleFuncInput) lipgloss.Style) Model
WithRowStyleFunc sets a function that can be used to apply a style to each row based on the row data. This is useful for things like zebra striping or other data-based styles. It can be safely set to nil to remove it later. This style is applied after the base style and before individual row styles. This will override any HighlightStyle settings.
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 RowStyleFuncInput ¶ added in v0.16.0
type RowStyleFuncInput struct { // Index is the index of the row, starting at 0. Index int // Row is the full row data. Row Row // IsHighlighted is true if the row is currently highlighted. IsHighlighted bool }
RowStyleFuncInput is the input to the style function that can be applied to each row. This is useful for things like zebra striping or other data-based styles.
Note that we use a struct here to allow for future expansion while keeping backwards compatibility.
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.
type UserEvent ¶ added in v0.14.0
type UserEvent interface{}
UserEvent is some state change that has occurred due to user input. These will ONLY be generated when a user has interacted directly with the table. These will NOT be generated when code programmatically changes values in the table.
type UserEventFilterInputFocused ¶ added in v0.14.5
type UserEventFilterInputFocused struct{}
UserEventFilterInputFocused indicates that the user has focused the filter text input, so that any other typing will type into the filter field. Only activates for the built-in filter text box.
type UserEventFilterInputUnfocused ¶ added in v0.14.5
type UserEventFilterInputUnfocused struct{}
UserEventFilterInputUnfocused indicates that the user has unfocused the filter text input, which means the user is done typing into the filter field. Only activates for the built-in filter text box.
type UserEventHighlightedIndexChanged ¶ added in v0.14.0
type UserEventHighlightedIndexChanged struct { // PreviousRow is the row that was selected before the change. PreviousRowIndex int // SelectedRow is the row index that is now selected SelectedRowIndex int }
UserEventHighlightedIndexChanged indicates that the user has scrolled to a new row.
type UserEventRowSelectToggled ¶ added in v0.14.1
UserEventRowSelectToggled indicates that the user has either selected or deselected a row by toggling the selection. The event contains information about which row index was selected and whether it was selected or deselected.