-
Notifications
You must be signed in to change notification settings - Fork 9
/
list.go
121 lines (98 loc) · 2.89 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package tui
import (
"context"
"fmt"
"io"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"namespacelabs.dev/foundation/internal/console"
)
const maxHeight = 20
func Select[V list.DefaultItem](ctx context.Context, title string, items []V) (list.DefaultItem, error) {
done := console.EnterInputMode(ctx)
defer done()
downcast := make([]list.Item, len(items))
for k, item := range items {
downcast[k] = item
}
height := 5 + len(items)*3
if height > maxHeight {
height = maxHeight
}
p := tea.NewProgram(initialSelectModel(title, downcast, height))
final, err := p.Run()
if err != nil {
return nil, err
}
return final.(selectModel).selected, nil
}
type selectModel struct {
maxHeight int
list list.Model
selected list.DefaultItem
}
func initialSelectModel(title string, items []list.Item, height int) selectModel {
li := list.New(items, itemDelegate{list.NewDefaultItemStyles()}, 40, height)
li.Title = title
li.SetShowStatusBar(false)
li.SetFilteringEnabled(false)
li.Styles.Title = titleStyle
li.Styles.PaginationStyle = list.DefaultStyles().PaginationStyle
li.Styles.HelpStyle = list.DefaultStyles().HelpStyle
return selectModel{maxHeight: height, list: li}
}
func (m selectModel) Init() tea.Cmd {
return nil
}
func (m selectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter:
if selected, ok := m.list.SelectedItem().(list.DefaultItem); ok {
m.selected = selected
}
return m, tea.Quit
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
}
case tea.WindowSizeMsg:
m.list.SetWidth(msg.Width)
if msg.Height > m.maxHeight {
m.list.SetHeight(m.maxHeight)
} else {
m.list.SetHeight(msg.Height)
}
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m selectModel) View() string {
return listMainStyle.Render(m.list.View())
}
type itemDelegate struct {
styles list.DefaultItemStyles
}
func (d itemDelegate) Height() int { return 2 }
func (d itemDelegate) Spacing() int { return 1 }
func (d itemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil }
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
item, ok := listItem.(list.DefaultItem)
if !ok {
return
}
var titleStyle, descStyle lipgloss.Style
if index == m.Index() {
titleStyle = d.styles.SelectedTitle
descStyle = d.styles.SelectedDesc
} else {
titleStyle = d.styles.NormalTitle
descStyle = d.styles.NormalDesc
}
fmt.Fprintf(w, "%s\n%s", titleStyle.Render(item.Title()), descStyle.Render(item.Description()))
}