Skip to content
Myr

Add paginator example

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Elio Esteves Duarte · committed by Christian Rocha · parent 295f7bd · Browse files · 1 file+76 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+76 added−0 removed

  • other:+76 added−0 removed

1 file+76 added−0 removed

added:examples/paginator/main.go+76 added
1package main23// A simple program demonstrating the paginator component from the Bubbles4// component library.56import (7 "fmt"8 "log"9 "strings"1011 "github.com/charmbracelet/bubbles/paginator"12 "github.com/charmbracelet/lipgloss"1314 tea "github.com/charmbracelet/bubbletea"15)1617func newModel() model {18 var items []string19 for i := 1; i < 101; i++ {20 text := fmt.Sprintf("Item %d", i)21 items = append(items, text)22 }2324 p := paginator.NewModel()25 p.Type = paginator.Dots26 p.PerPage = 1027 p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#847A85", Dark: "#979797"}).Render("•")28 p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#C2B8C2", Dark: "#4D4D4D"}).Render("•")29 p.SetTotalPages(len(items))3031 return model{32 paginator: p,33 items: items,34 }35}3637type model struct {38 items []string39 paginator paginator.Model40}4142func (m model) Init() tea.Cmd {43 return nil44}4546func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {47 var cmd tea.Cmd48 switch msg := msg.(type) {49 case tea.KeyMsg:50 switch msg.String() {51 case "q":52 return m, tea.Quit53 }54 }55 m.paginator, cmd = m.paginator.Update(msg)56 return m, cmd57}5859func (m model) View() string {60 var b strings.Builder61 b.WriteString("\n Paginator Example\n\n")62 start, end := m.paginator.GetSliceBounds(len(m.items))63 for _, item := range m.items[start:end] {64 b.WriteString(" • " + item + "\n\n")65 }66 b.WriteString(" " + m.paginator.View())67 b.WriteString("\n\n h/l ←/→ page • q: quit\n")68 return b.String()69}7071func main() {72 p := tea.NewProgram(newModel())73 if err := p.Start(); err != nil {74 log.Fatal(err)75 }76}