-
Notifications
You must be signed in to change notification settings - Fork 63
/
multiselect.go
128 lines (108 loc) · 2.55 KB
/
multiselect.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
122
123
124
125
126
127
128
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package multiselect
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"kraftkit.sh/tui"
)
var selectedText = lipgloss.NewStyle().
Foreground(lipgloss.Color("32")).
Render
// MultiSelect is a utility method used in a CLI context to prompt the
// user given a slice of options based on the generic type.
func MultiSelect[T fmt.Stringer](question string, options ...T) ([]T, error) {
mapped := make(map[string]T)
items := make([]item, 0, len(options))
for _, option := range options {
mapped[option.String()] = option
items = append(items, item{
text: option.String(),
})
}
p := tea.NewProgram(&model{
question: question,
options: items,
item: 0,
})
// Run returns the model as a tea.Model.
m, err := p.Run()
if err != nil {
return nil, fmt.Errorf("could not start multi selection prompt: %w", err)
}
mo := m.(*model)
selected := make([]T, 0, len(options))
for _, opt := range mo.options {
if opt.checked {
selected = append(selected, mapped[opt.text])
}
}
return selected, nil
}
type model struct {
question string
options []item
item int
quitting bool
}
type item struct {
text string
checked bool
}
func (m model) Init() tea.Cmd {
return nil
}
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch typed := msg.(type) {
case tea.KeyMsg:
return m, m.handleKeyMsg(typed)
}
return m, nil
}
func (m *model) handleKeyMsg(msg tea.KeyMsg) tea.Cmd {
switch msg.String() {
case "esc", "ctrl+c":
m.quitting = true
return tea.Quit
case "enter":
m.quitting = true
return tea.Quit
case " ", "x", "y":
m.options[m.item].checked = !m.options[m.item].checked
case "up":
if m.item > 0 {
m.item--
}
case "down":
if m.item+1 < len(m.options) {
m.item++
}
}
return nil
}
func (m *model) View() string {
out := tui.TextWhiteBgBlue("[?]") + " " + m.question + ":\n"
for i, item := range m.options {
var text string
check := ""
if item.checked {
check = tui.TextWhiteBgGreen("[x]")
} else {
check = tui.TextLightGray("[ ]")
}
if i == m.item && !m.quitting {
text = selectedText("▸ " + item.text)
} else {
text = " " + item.text
}
out += fmt.Sprintf("%s %s\n", check, text)
}
if !m.quitting {
out += "\n"
out += "use arrow keys; space, y or x to select; enter to continue"
}
return out
}