2021-04-29 16:10:20 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
// A simple program demonstrating the paginator component from the Bubbles
|
|
|
|
// component library.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/paginator"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newModel() model {
|
|
|
|
var items []string
|
|
|
|
for i := 1; i < 101; i++ {
|
|
|
|
text := fmt.Sprintf("Item %d", i)
|
|
|
|
items = append(items, text)
|
|
|
|
}
|
|
|
|
|
2022-01-10 20:42:10 -05:00
|
|
|
p := paginator.New()
|
2021-04-29 16:10:20 -04:00
|
|
|
p.Type = paginator.Dots
|
|
|
|
p.PerPage = 10
|
2021-05-01 09:28:58 -04:00
|
|
|
p.ActiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "235", Dark: "252"}).Render("•")
|
|
|
|
p.InactiveDot = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "250", Dark: "238"}).Render("•")
|
2021-04-29 16:10:20 -04:00
|
|
|
p.SetTotalPages(len(items))
|
|
|
|
|
|
|
|
return model{
|
|
|
|
paginator: p,
|
|
|
|
items: items,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type model struct {
|
|
|
|
items []string
|
|
|
|
paginator paginator.Model
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
2021-05-01 09:28:58 -04:00
|
|
|
case "q", "esc", "ctrl+c":
|
2021-04-29 16:10:20 -04:00
|
|
|
return m, tea.Quit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.paginator, cmd = m.paginator.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) View() string {
|
|
|
|
var b strings.Builder
|
|
|
|
b.WriteString("\n Paginator Example\n\n")
|
|
|
|
start, end := m.paginator.GetSliceBounds(len(m.items))
|
|
|
|
for _, item := range m.items[start:end] {
|
|
|
|
b.WriteString(" • " + item + "\n\n")
|
|
|
|
}
|
|
|
|
b.WriteString(" " + m.paginator.View())
|
|
|
|
b.WriteString("\n\n h/l ←/→ page • q: quit\n")
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
p := tea.NewProgram(newModel())
|
|
|
|
if err := p.Start(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|