Tutorials

Lists

Build beautiful, navigable lists with the list component.

Lists with keyboard nav, filtering, and pagination - built in!

Basic Usage

package main

import (
    "github.com/charmbracelet/bubbles/v2/list"
    tea "github.com/charmbracelet/bubbletea/v2"
)

type item string

func (i item) Title() string       { return string(i) }
func (i item) Description() string { return "" }
func (i item) FilterValue() string { return string(i) }

func main() {
    items := []list.Item{
        item("Raspberry Pi"),
        item("Arduino"),
        item("BeagleBone"),
    }

    l := list.New(items, list.NewDefaultDelegate(), 0, 0)
    l.Title = "My Electronics"

    p := tea.NewProgram(model{list: l}, tea.WithAltScreen())
    p.Run()
}

type model struct{ list list.Model }

func (m model) Init() tea.Cmd { return nil }

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.KeyPressMsg:
        if msg.String() == "ctrl+c" {
            return m, tea.Quit
        }
    case tea.WindowSizeMsg:
        m.list.SetSize(msg.Width, msg.Height)
    }
    var cmd tea.Cmd
    m.list, cmd = m.list.Update(msg)
    return m, cmd
}

func (m model) View() string {
    return m.list.View()
}