Skip to content

Instantly share code, notes, and snippets.

@rorycl
Last active April 8, 2024 13:56
Show Gist options
  • Select an option

  • Save rorycl/264a56ab4e4d15c07c63cb5fc651f771 to your computer and use it in GitHub Desktop.

Select an option

Save rorycl/264a56ab4e4d15c07c63cb5fc651f771 to your computer and use it in GitHub Desktop.
bubbletea/list first item padding issue
package main
import (
"fmt"
"os"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var docStyle = lipgloss.NewStyle().Margin(1, 2)
type item struct {
title, desc string
}
func (i item) Title() string { return i.title }
func (i item) Description() string { return i.desc }
func (i item) FilterValue() string { return i.title }
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.KeyMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m model) View() string {
return docStyle.Render(m.list.View())
}
func main() {
items := []list.Item{
item{title: "Raspberry Pi’s", desc: "I have ’em all over my house"},
item{title: "Nutella", desc: "It's good on toast"},
item{title: "Bitter melon", desc: "It cools you down"},
item{title: "Nice socks", desc: "And by that I mean socks without holes"},
item{title: "Eight hours of sleep", desc: "I had this once"},
item{title: "Cats", desc: "Usually"},
item{title: "Plantasia, the album", desc: "My plants love it too"},
item{title: "Pour over coffee", desc: "It takes forever to make though"},
item{title: "VR", desc: "Virtual reality...what is there to say?"},
item{title: "Noguchi Lamps", desc: "Such pleasing organic forms"},
item{title: "Linux", desc: "Pretty much the best OS"},
item{title: "Business school", desc: "Just kidding"},
item{title: "Pottery", desc: "Wet clay is a great feeling"},
item{title: "Shampoo", desc: "Nothing like clean hair"},
item{title: "Table tennis", desc: "It’s surprisingly exhausting"},
item{title: "Milk crates", desc: "Great for packing in your extra stuff"},
item{title: "Afternoon tea", desc: "Especially the tea sandwich part"},
item{title: "Stickers", desc: "The thicker the vinyl the better"},
item{title: "20° Weather", desc: "Celsius, not Fahrenheit"},
item{title: "Warm light", desc: "Like around 2700 Kelvin"},
item{title: "The vernal equinox", desc: "The autumnal equinox is pretty good too"},
item{title: "Gaffer’s tape", desc: "Basically sticky fabric"},
item{title: "Terrycloth", desc: "In other words, towel fabric"},
}
ddStyles := list.NewDefaultItemStyles()
ddStyles.NormalTitle.PaddingTop(1)
ddStyles.DimmedTitle.PaddingTop(1)
ddStyles.SelectedTitle.PaddingTop(1)
ndd := func() list.DefaultDelegate {
ldd := list.DefaultDelegate{
ShowDescription: true,
Styles: ddStyles,
}
ldd.SetHeight(2)
ldd.SetSpacing(1)
return ldd
}
m := model{list: list.New(items, ndd(), 0, 0)}
m.list.Title = "My Fave Things"
p := tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
@rorycl
Copy link
Copy Markdown
Author

rorycl commented Apr 8, 2024

The Go bubbletea list bubble seems to have an issue with the first item in the list moving into the status/title area if it is given a padding value.

Instead of
normal

with a padding-top value of 1

	ddStyles := list.NewDefaultItemStyles()
	ddStyles.NormalTitle.PaddingTop(1)
	ddStyles.DimmedTitle.PaddingTop(1)
	ddStyles.SelectedTitle.PaddingTop(1)
	ndd := func() list.DefaultDelegate {
		ldd := list.DefaultDelegate{
			ShowDescription: true,
			Styles:          ddStyles,
		}
		ldd.SetHeight(2)
		ldd.SetSpacing(1)
		return ldd
	}
	m := model{list: list.New(items, ndd(), 0, 0)}

the following appears:
problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment