6 "charm.land/lipgloss/v2"
7 "github.com/charmbracelet/crush/internal/ui/list"
8 uv "github.com/charmbracelet/ultraviolet"
11// Example demonstrates basic list usage with string items.
15 list.NewStringItem("First item"),
16 list.NewStringItem("Second item"),
17 list.NewStringItem("Third item"),
20 // Create a list with options
21 l := list.New(items...)
28 // Draw to a screen buffer
29 screen := uv.NewScreenBuffer(80, 10)
30 area := uv.Rect(0, 0, 80, 10)
34 output := screen.Render()
38// BorderedItem demonstrates a focusable item with borders.
39type BorderedItem struct {
46func NewBorderedItem(id, content string) *BorderedItem {
54func (b *BorderedItem) ID() string {
58func (b *BorderedItem) Height(width int) int {
59 // Account for border (2 lines for top and bottom)
60 b.width = width // Update width for rendering
61 return lipgloss.Height(b.render())
64func (b *BorderedItem) Draw(scr uv.Screen, area uv.Rectangle) {
65 rendered := b.render()
66 styled := uv.NewStyledString(rendered)
67 styled.Draw(scr, area)
70func (b *BorderedItem) render() string {
71 style := lipgloss.NewStyle().
77 Border(lipgloss.RoundedBorder()).
78 BorderForeground(lipgloss.Color("205"))
81 Border(lipgloss.NormalBorder()).
82 BorderForeground(lipgloss.Color("240"))
85 return style.Render(b.content)
88func (b *BorderedItem) Focus() {
92func (b *BorderedItem) Blur() {
96func (b *BorderedItem) IsFocused() bool {
100// Example demonstrates focusable items with borders.
101func Example_focusable() {
102 // Create focusable items
103 items := []list.Item{
104 NewBorderedItem("1", "Focusable Item 1"),
105 NewBorderedItem("2", "Focusable Item 2"),
106 NewBorderedItem("3", "Focusable Item 3"),
109 // Create list with first item selected and focused
110 l := list.New(items...)
118 screen := uv.NewScreenBuffer(80, 20)
119 area := uv.Rect(0, 0, 80, 20)
120 l.Draw(&screen, area)
122 // The first item will have a colored border since it's focused
123 output := screen.Render()
127// Example demonstrates dynamic item updates.
128func Example_dynamicUpdates() {
129 items := []list.Item{
130 list.NewStringItem("Item 1"),
131 list.NewStringItem("Item 2"),
134 l := list.New(items...)
137 // Draw initial state
138 screen := uv.NewScreenBuffer(80, 10)
139 area := uv.Rect(0, 0, 80, 10)
140 l.Draw(&screen, area)
143 l.UpdateItem(2, list.NewStringItem("Updated Item 2"))
145 // Draw again - only changed item is re-rendered
146 l.Draw(&screen, area)
149 l.AppendItem(list.NewStringItem("New Item 3"))
151 // Draw again - master buffer grows efficiently
152 l.Draw(&screen, area)
154 output := screen.Render()
158// Example demonstrates scrolling with a large list.
159func Example_scrolling() {
161 items := make([]list.Item, 100)
162 for i := range items {
163 items[i] = list.NewStringItem(
164 fmt.Sprintf("Item %d", i),
168 // Create list with small viewport
169 l := list.New(items...)
173 // Draw initial view (shows items 0-9)
174 screen := uv.NewScreenBuffer(80, 10)
175 area := uv.Rect(0, 0, 80, 10)
176 l.Draw(&screen, area)
180 l.Draw(&screen, area) // Now shows items 5-14
182 // Jump to specific item
184 l.Draw(&screen, area) // Now shows item 50 and neighbors
188 l.Draw(&screen, area) // Now shows last 10 items
190 output := screen.Render()
194// VariableHeightItem demonstrates items with different heights.
195type VariableHeightItem struct {
201func NewVariableHeightItem(id string, lines []string) *VariableHeightItem {
202 return &VariableHeightItem{
209func (v *VariableHeightItem) ID() string {
213func (v *VariableHeightItem) Height(width int) int {
217func (v *VariableHeightItem) Draw(scr uv.Screen, area uv.Rectangle) {
219 for i, line := range v.lines {
225 styled := uv.NewStyledString(content)
226 styled.Draw(scr, area)
229// Example demonstrates variable height items.
230func Example_variableHeights() {
231 items := []list.Item{
232 NewVariableHeightItem("1", []string{"Short item"}),
233 NewVariableHeightItem("2", []string{
234 "This is a taller item",
235 "that spans multiple lines",
236 "to demonstrate variable heights",
238 NewVariableHeightItem("3", []string{"Another short item"}),
239 NewVariableHeightItem("4", []string{
240 "A medium height item",
245 l := list.New(items...)
248 screen := uv.NewScreenBuffer(80, 15)
249 area := uv.Rect(0, 0, 80, 15)
250 l.Draw(&screen, area)
252 output := screen.Render()
256// Example demonstrates markdown items in a list.
257func Example_markdown() {
258 // Create markdown items
259 items := []list.Item{
260 list.NewMarkdownItem("# Welcome\n\nThis is a **markdown** item."),
261 list.NewMarkdownItem("## Features\n\n- Supports **bold**\n- Supports *italic*\n- Supports `code`"),
262 list.NewMarkdownItem("### Code Block\n\n```go\nfunc main() {\n fmt.Println(\"Hello\")\n}\n```"),
266 l := list.New(items...)
269 screen := uv.NewScreenBuffer(80, 20)
270 area := uv.Rect(0, 0, 80, 20)
271 l.Draw(&screen, area)
273 output := screen.Render()
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list