How to make a checklist? #603
Unanswered
ghostsquad
asked this question in
Q&A
Replies: 2 comments
0 replies
|
Making a checklist with lipgloss is straightforward — you handle the checked/unchecked state in your model and render it with styled strings. package main
import (
"fmt"
"strings"
"github.com/charmbracelet/lipgloss"
)
var (
checked = lipgloss.NewStyle().Foreground(lipgloss.Color("10")).Render("✓")
unchecked = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).Render("○")
itemStyle = lipgloss.NewStyle().PaddingLeft(2)
selectedStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("212"))
)
type Item struct {
label string
checked bool
}
func renderChecklist(items []Item, cursor int) string {
var sb strings.Builder
for i, item := range items {
icon := unchecked
if item.checked {
icon = checked
}
label := itemStyle.Render(item.label)
if i == cursor {
label = selectedStyle.Render(item.label)
}
sb.WriteString(fmt.Sprintf("%s %s\n", icon, label))
}
return sb.String()
}For the interactive model: func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "up", "k":
if m.cursor > 0 { m.cursor-- }
case "down", "j":
if m.cursor < len(m.items)-1 { m.cursor++ }
case " ", "enter":
m.items[m.cursor].checked = !m.items[m.cursor].checked
}
}
return m, nil
}For a fancier look, |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
The readme shows checkmarks next to some items in a list. I was wondering how?
All reactions