table

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package table is a vendored copy of charm.land/bubbles/v2/table (v2.0.0) with the addition of EnsureCursorVisible(). Original: MIT License, Copyright (c) 2020-2026 Charmbracelet, Inc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {
	Title string
	Width int
}

Column defines the table structure.

type KeyMap

type KeyMap struct {
	LineUp       key.Binding
	LineDown     key.Binding
	PageUp       key.Binding
	PageDown     key.Binding
	HalfPageUp   key.Binding
	HalfPageDown key.Binding
	GotoTop      key.Binding
	GotoBottom   key.Binding
}

KeyMap defines keybindings. It satisfies to the help.KeyMap interface, which is used to render the help menu.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns a default set of keybindings.

func (KeyMap) FullHelp

func (km KeyMap) FullHelp() [][]key.Binding

FullHelp implements the KeyMap interface.

func (KeyMap) ShortHelp

func (km KeyMap) ShortHelp() []key.Binding

ShortHelp implements the KeyMap interface.

type Model

type Model struct {
	KeyMap KeyMap
	Help   help.Model

	// RowStyleFunc is an optional callback that returns a style override for a given row index.
	// When set, if it returns non-nil for a row, that style is used instead of the default.
	// For cursor rows (isCursor=true), it takes priority over styles.Selected.
	RowStyleFunc func(index int, isCursor bool) *lipgloss.Style
	// contains filtered or unexported fields
}

Model defines a state for the table widget.

func New

func New(opts ...Option) Model

New creates a new model for the table widget.

func (*Model) Blur

func (m *Model) Blur()

Blur blurs the table, preventing selection or movement.

func (Model) Columns

func (m Model) Columns() []Column

Columns returns the current columns.

func (Model) Cursor

func (m Model) Cursor() int

Cursor returns the index of the selected row.

func (*Model) EnsureCursorVisible

func (m *Model) EnsureCursorVisible()

EnsureCursorVisible adjusts the viewport scroll offset so the cursor row is centered in the viewport. No-op if cursor is already in view.

func (*Model) Focus

func (m *Model) Focus()

Focus focuses the table, allowing the user to move around the rows and interact.

func (Model) Focused

func (m Model) Focused() bool

Focused returns the focus state of the table.

func (*Model) FromValues

func (m *Model) FromValues(value, separator string)

FromValues create the table rows from a simple string. It uses `\n` by default for getting all the rows and the given separator for the fields on each row.

func (*Model) GotoBottom

func (m *Model) GotoBottom()

GotoBottom moves the selection to the last row.

func (*Model) GotoTop

func (m *Model) GotoTop()

GotoTop moves the selection to the first row.

func (Model) Height

func (m Model) Height() int

Height returns the viewport height of the table.

func (Model) HelpView

func (m Model) HelpView() string

HelpView is a helper method for rendering the help menu from the keymap. Note that this view is not rendered by default and you must call it manually in your application, where applicable.

func (*Model) MoveDown

func (m *Model) MoveDown(n int)

MoveDown moves the selection down by any number of rows. It can not go below the last row.

func (*Model) MoveUp

func (m *Model) MoveUp(n int)

MoveUp moves the selection up by any number of rows. It can not go above the first row.

func (*Model) RowAtY added in v0.5.0

func (m *Model) RowAtY(localY int) int

RowAtY maps a y-coordinate (relative to the table's top-left, 0 = header row when a header is shown) to the underlying row index. Returns -1 if y falls on the header row, above it, or past the last data row. Pure lookup — does not mutate any state.

Header height is always 1 because View() always emits exactly one header line (see headersView()); the literal is used here instead of a lipgloss.Height round-trip for speed. If headers ever become optional or multi-line, recompute from headersView().

func (Model) Rows

func (m Model) Rows() []Row

Rows returns the current rows.

func (Model) SelectedRow

func (m Model) SelectedRow() Row

SelectedRow returns the selected row. You can cast it to your own implementation.

func (*Model) SetColumns

func (m *Model) SetColumns(c []Column)

SetColumns sets a new columns state.

func (*Model) SetColumnsAndRows

func (m *Model) SetColumnsAndRows(c []Column, r []Row)

SetColumnsAndRows sets columns and rows in a single operation, calling UpdateViewport only once.

func (*Model) SetContentWidth

func (m *Model) SetContentWidth(w int)

SetContentWidth sets the total content width for horizontal scroll calculations.

func (*Model) SetCursor

func (m *Model) SetCursor(n int)

SetCursor sets the cursor position in the table.

func (*Model) SetHeight

func (m *Model) SetHeight(h int)

SetHeight sets the height of the viewport of the table.

func (*Model) SetLayout

func (m *Model) SetLayout(cols []Column, w, h int)

SetLayout sets columns, width, and height in a single operation, calling UpdateViewport only once instead of three times.

func (*Model) SetRows

func (m *Model) SetRows(r []Row)

SetRows sets a new rows state.

func (*Model) SetStyles

func (m *Model) SetStyles(s Styles)

SetStyles sets the table styles.

func (*Model) SetWidth

func (m *Model) SetWidth(w int)

SetWidth sets the width of the viewport of the table.

func (*Model) SetXOffset

func (m *Model) SetXOffset(n int)

SetXOffset sets the horizontal scroll offset, clamped to valid range.

func (Model) Update

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)

Update is the Bubble Tea update loop.

func (*Model) UpdateViewport

func (m *Model) UpdateViewport()

UpdateViewport updates the list content based on the previously defined columns and rows.

func (Model) View

func (m Model) View() string

View renders the component.

func (Model) Width

func (m Model) Width() int

Width returns the viewport width of the table.

func (Model) XOffset

func (m Model) XOffset() int

XOffset returns the current horizontal scroll offset.

type Option

type Option func(*Model)

Option is used to set options in New. For example:

table := New(WithColumns([]Column{{Title: "ID", Width: 10}}))

func WithColumns

func WithColumns(cols []Column) Option

WithColumns sets the table columns (headers).

func WithFocused

func WithFocused(f bool) Option

WithFocused sets the focus state of the table.

func WithHeight

func WithHeight(h int) Option

WithHeight sets the height of the table.

func WithKeyMap

func WithKeyMap(km KeyMap) Option

WithKeyMap sets the key map.

func WithRows

func WithRows(rows []Row) Option

WithRows sets the table rows (data).

func WithStyles

func WithStyles(s Styles) Option

WithStyles sets the table styles.

func WithWidth

func WithWidth(w int) Option

WithWidth sets the width of the table.

type Row

type Row []string

Row represents one line in the table.

type Styles

type Styles struct {
	Header   lipgloss.Style
	Cell     lipgloss.Style
	Selected lipgloss.Style
}

Styles contains style definitions for this list component. By default, these values are generated by DefaultStyles.

func DefaultStyles

func DefaultStyles() Styles

DefaultStyles returns a set of default style definitions for this table.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL