Documentation ¶
Overview ¶
Package list provides a feature-rich Bubble Tea component for browsing a general purpose list of items. It features optional filtering, pagination, help, status messages, and a spinner to indicate activity.
Index ¶
- type DefaultDelegate
- func (d DefaultDelegate) FullHelp() [][]key.Binding
- func (d DefaultDelegate) Height() int
- func (d DefaultDelegate) Render(w io.Writer, m Model, index int, item Item)
- func (d *DefaultDelegate) SetSpacing(i int)
- func (d DefaultDelegate) ShortHelp() []key.Binding
- func (d DefaultDelegate) Spacing() int
- func (d DefaultDelegate) Update(msg tea.Msg, m *Model) tea.Cmd
- type DefaultItem
- type DefaultItemStyles
- type FilterMatchesMessage
- type FilterState
- type Item
- type ItemDelegate
- type KeyMap
- type Model
- func (m Model) Cursor() int
- func (m *Model) CursorDown()
- func (m *Model) CursorUp()
- func (m *Model) DisableQuitKeybindings()
- func (m Model) FilterState() FilterState
- func (m Model) FilterValue() string
- func (m Model) FilteringEnabled() bool
- func (m Model) FullHelp() [][]key.Binding
- func (m Model) Height() int
- func (m Model) Index() int
- func (m *Model) InsertItem(index int, item Item) tea.Cmd
- func (m Model) Items() []Item
- func (m Model) MatchesForItem(index int) []int
- func (m *Model) NewStatusMessage(s string) tea.Cmd
- func (m Model) NextPage()
- func (m Model) PrevPage()
- func (m *Model) RemoveItem(index int)
- func (m *Model) ResetFilter()
- func (m *Model) ResetSelected()
- func (m *Model) Select(index int)
- func (m Model) SelectedItem() Item
- func (m *Model) SetDelegate(d ItemDelegate)
- func (m *Model) SetFilteringEnabled(v bool)
- func (m *Model) SetHeight(v int)
- func (m *Model) SetItem(index int, item Item) tea.Cmd
- func (m *Model) SetItems(i []Item) tea.Cmd
- func (m *Model) SetShowFilter(v bool)
- func (m *Model) SetShowHelp(v bool)
- func (m *Model) SetShowPagination(v bool)
- func (m *Model) SetShowStatusBar(v bool)
- func (m *Model) SetShowTitle(v bool)
- func (m *Model) SetSize(width, height int)
- func (m *Model) SetSpinner(spinner spinner.Spinner)
- func (m *Model) SetWidth(v int)
- func (m Model) SettingFilter() bool
- func (m Model) ShortHelp() []key.Binding
- func (m Model) ShowFilter() bool
- func (m Model) ShowHelp() bool
- func (m *Model) ShowPagination() bool
- func (m Model) ShowStatusBar() bool
- func (m Model) ShowTitle() bool
- func (m *Model) StartSpinner() tea.Cmd
- func (m *Model) StopSpinner()
- func (m *Model) ToggleSpinner() tea.Cmd
- func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)
- func (m Model) View() string
- func (m Model) VisibleItems() []Item
- func (m Model) Width() int
- type Styles
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DefaultDelegate ¶
type DefaultDelegate struct { ShowDescription bool Styles DefaultItemStyles UpdateFunc func(tea.Msg, *Model) tea.Cmd ShortHelpFunc func() []key.Binding FullHelpFunc func() [][]key.Binding // contains filtered or unexported fields }
DefaultDelegate is a standard delegate designed to work in lists. It's styled by DefaultItemStyles, which can be customized as you like.
The description line can be hidden by setting Description to false, which renders the list as single-line-items. The spacing between items can be set with the SetSpacing method.
Setting UpdateFunc is optional. If it's set it will be called when the ItemDelegate called, which is called when the list's Update function is invoked.
Settings ShortHelpFunc and FullHelpFunc is optional. They can can be set to include items in the list's default short and full help menus.
func NewDefaultDelegate ¶
func NewDefaultDelegate() DefaultDelegate
NewDefaultDelegate creates a new delegate with default styles.
func (DefaultDelegate) FullHelp ¶
func (d DefaultDelegate) FullHelp() [][]key.Binding
FullHelp returns the delegate's full help.
func (DefaultDelegate) Height ¶
func (d DefaultDelegate) Height() int
Height returns the delegate's preferred height.
func (*DefaultDelegate) SetSpacing ¶
func (d *DefaultDelegate) SetSpacing(i int)
SetSpacing set the delegate's spacing.
func (DefaultDelegate) ShortHelp ¶
func (d DefaultDelegate) ShortHelp() []key.Binding
ShortHelp returns the delegate's short help.
func (DefaultDelegate) Spacing ¶
func (d DefaultDelegate) Spacing() int
Spacing returns the delegate's spacing.
type DefaultItem ¶
DefaultItem describes an items designed to work with DefaultDelegate.
type DefaultItemStyles ¶
type DefaultItemStyles struct { // The Normal state. NormalTitle lipgloss.Style NormalDesc lipgloss.Style // The selected item state. SelectedTitle lipgloss.Style SelectedDesc lipgloss.Style // The dimmed state, for when the filter input is initially activated. DimmedTitle lipgloss.Style DimmedDesc lipgloss.Style // Charcters matching the current filter, if any. FilterMatch lipgloss.Style }
DefaultItemStyles defines styling for a default list item. See DefaultItemView for when these come into play.
func NewDefaultItemStyles ¶
func NewDefaultItemStyles() (s DefaultItemStyles)
NewDefaultItemStyles returns style definitions for a default item. See DefaultItemView for when these come into play.
type FilterMatchesMessage ¶
type FilterMatchesMessage []filteredItem
type FilterState ¶
type FilterState int
FilterState describes the current filtering state on the model.
const ( Unfiltered FilterState = iota // no filter set Filtering // user is actively setting a filter FilterApplied // a filter is applied and user is not editing filter )
Possible filter states.
func (FilterState) String ¶
func (f FilterState) String() string
String returns a human-readable string of the current filter state.
type Item ¶
type Item interface { // Filter value is the value we use when filtering against this item when // we're filtering the list. FilterValue() string }
Item is an item that appears in the list.
type ItemDelegate ¶
type ItemDelegate interface { // Render renders the item's view. Render(w io.Writer, m Model, index int, item Item) // Height is the height of the list item. Height() int // Spacing is the size of the horizontal gap between list items in cells. Spacing() int // Update is the update loop for items. All messages in the list's update // loop will pass through here except when the user is setting a filter. // Use this method to perform item-level updates appropriate to this // delegate. Update(msg tea.Msg, m *Model) tea.Cmd }
ItemDelegate encapsulates the general functionality for all list items. The benefit to separating this logic from the item itself is that you can change the functionality of items without changing the actual items themselves.
Note that if the delegate also implements help.KeyMap delegate-related help items will be added to the help view.
type KeyMap ¶
type KeyMap struct { // Keybindings used when browsing the list. CursorUp key.Binding CursorDown key.Binding NextPage key.Binding PrevPage key.Binding GoToStart key.Binding GoToEnd key.Binding Filter key.Binding ClearFilter key.Binding DeleteSelection key.Binding // Keybindings used when setting a filter. CancelWhileFiltering key.Binding AcceptWhileFiltering key.Binding // Help toggle keybindings. ShowFullHelp key.Binding CloseFullHelp key.Binding // The quit keybinding. This won't be caught when filtering. Quit key.Binding // The quit-no-matter-what keybinding. This will be caught when filtering. ForceQuit key.Binding }
KeyMap defines keybindings. It satisfies to the help.KeyMap interface, which is used to render the menu menu.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns a default set of keybindings.
type Model ¶
type Model struct { Title string Styles Styles // Key mappings for navigating the list. KeyMap KeyMap // Additional key mappings for the short and full help views. This allows // you to add additional key mappings to the help menu without // re-implementing the help component. Of course, you can also disable the // list's help component and implement a new one if you need more // flexibility. AdditionalShortHelpKeys func() []key.Binding AdditionalFullHelpKeys func() []key.Binding Paginator paginator.Model Help help.Model FilterInput textinput.Model // How long status messages should stay visible. By default this is // 1 second. StatusMessageLifetime time.Duration // contains filtered or unexported fields }
Model contains the state of this component.
func NewModel ¶
func NewModel(items []Item, delegate ItemDelegate, width, height int) Model
NewModel returns a new model with sensible defaults.
func (*Model) CursorDown ¶
func (m *Model) CursorDown()
CursorDown moves the cursor down. This can also advance the state to the next page.
func (*Model) CursorUp ¶
func (m *Model) CursorUp()
CursorUp moves the cursor up. This can also move the state to the previous page.
func (*Model) DisableQuitKeybindings ¶
func (m *Model) DisableQuitKeybindings()
Helper for disabling the keybindings used for quitting, incase you want to handle this elsewhere in your application.
func (Model) FilterState ¶
func (m Model) FilterState() FilterState
FilterState returns the current filter state.
func (Model) FilterValue ¶
FilterValue returns the current value of the filter.
func (Model) FilteringEnabled ¶
FilteringEnabled returns whether or not filtering is enabled.
func (Model) FullHelp ¶
FullHelp returns bindings to show the full help view. It's part of the help.KeyMap interface.
func (Model) Index ¶
Index returns the index of the currently selected item as it appears in the entire slice of items.
func (*Model) InsertItem ¶
Insert an item at the given index. This returns a command.
func (Model) MatchesForItem ¶
MatchesForItem returns rune positions matched by the current filter, if any. Use this to style runes matched by the active filter.
See DefaultItemView for a usage example.
func (*Model) NewStatusMessage ¶
NewStatusMessage sets a new status message, which will show for a limited amount of time. Note that this also returns a command.
func (Model) PrevPage ¶
func (m Model) PrevPage()
PrevPage moves to the previous page, if available.
func (*Model) RemoveItem ¶
RemoveItem removes an item at the given index. If the index is out of bounds this will be a no-op. O(n) complexity, which probably won't matter in the case of a TUI.
func (*Model) ResetFilter ¶
func (m *Model) ResetFilter()
ResetFilter resets the current filtering state.
func (*Model) ResetSelected ¶
func (m *Model) ResetSelected()
ResetSelected resets the selected item to the first item in the first page of the list.
func (Model) SelectedItem ¶
SelectedItems returns the current selected item in the list.
func (*Model) SetFilteringEnabled ¶
SetFilteringEnabled enables or disables filtering. Note that this is different from ShowFilter, which merely hides or shows the input view.
func (*Model) SetShowFilter ¶
SetShowFilter shows or hides the filer bar. Note that this does not disable filtering, it simply hides the built-in filter view. This allows you to use the FilterInput to render the filtering UI differently without having to re-implement filtering from scratch.
To disable filtering entirely use EnableFiltering.
func (*Model) SetShowHelp ¶
SetShowHelp shows or hides the help view.
func (*Model) SetShowPagination ¶
ShowingPagination hides or shoes the paginator. Note that pagination will still be active, it simply won't be displayed.
func (*Model) SetShowStatusBar ¶
SetShowStatusBar shows or hides the view that displays metadata about the list, such as item counts.
func (*Model) SetShowTitle ¶
SetShowTitle shows or hides the title bar.
func (*Model) SetSpinner ¶
SetSpinner allows to set the spinner style.
func (Model) SettingFilter ¶
SettingFilter returns whether or not the user is currently editing the filter value. It's purely a convenience method for the following:
m.FilterState() == Filtering
It's included here because it's a common thing to check for when implementing this component.
func (Model) ShortHelp ¶
ShortHelp returns bindings to show in the abbreviated help view. It's part of the help.KeyMap interface.
func (Model) ShowFilter ¶
ShowFilter returns whether or not the filter is set to be rendered. Note that this is separate from FilteringEnabled, so filtering can be hidden yet still invoked. This allows you to render filtering differently without having to re-implement it from scratch.
func (*Model) ShowPagination ¶
ShowPagination returns whether the pagination is visible.
func (Model) ShowStatusBar ¶
ShowStatusBar returns whether or not the status bar is set to be rendered.
func (*Model) StartSpinner ¶
StartSpinner starts the spinner. Note that this returns a command.
func (*Model) ToggleSpinner ¶
Toggle the spinner. Note that this also returns a command.
func (Model) VisibleItems ¶
VisibleItems returns the total items available to be shown.
type Styles ¶
type Styles struct { TitleBar lipgloss.Style Title lipgloss.Style Spinner lipgloss.Style FilterPrompt lipgloss.Style FilterCursor lipgloss.Style // Default styling for matched characters in a filter. This can be // overridden by delegates. DefaultFilterCharacterMatch lipgloss.Style StatusBar lipgloss.Style StatusEmpty lipgloss.Style StatusBarActiveFilter lipgloss.Style StatusBarFilterCount lipgloss.Style NoItems lipgloss.Style PaginationStyle lipgloss.Style HelpStyle lipgloss.Style // Styled characters. ActivePaginationDot lipgloss.Style InactivePaginationDot lipgloss.Style ArabicPagination lipgloss.Style DividerDot lipgloss.Style }
Styles contains style definitions for this list component. By default, these values are generated by DefaultStyles.
func DefaultStyles ¶
func DefaultStyles() (s Styles)
DefaultStyles returns a set of default style definitions for this list component.