Skip to content

Commit dfb2cee

Browse files
committed
Update app to use bubbletea instead of survey
1 parent c29e18f commit dfb2cee

File tree

1 file changed

+59
-51
lines changed

1 file changed

+59
-51
lines changed

internal/select.go

+59-51
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,50 @@ import (
77
"fmt"
88
"strings"
99

10-
"github.com/AlecAivazis/survey/v2"
10+
"github.com/charmbracelet/bubbletea"
11+
"github.com/charmbracelet/bubbles/list"
1112
"github.com/aws/aws-sdk-go-v2/aws"
1213

1314
ecsTypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
1415
)
1516

16-
func init() {
17-
survey.SelectQuestionTemplate = `
18-
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
19-
{{- color .Config.Icons.Question.Format }}{{ .Config.Icons.Question.Text }} {{color "reset"}}
20-
{{- color "default+hb"}}{{ .Message }}{{ .FilterMessage }}{{color "reset"}}
21-
{{- if .ShowAnswer}}{{color "Cyan"}} {{""}}{{color "reset"}}
22-
{{- else}}
23-
{{- " "}}{{- color "Cyan"}}[Type to filter{{- if and .Help (not .ShowHelp)}}, {{ .Config.HelpInput }} for more help{{end}}]{{color "reset"}}{{- "\n"}}
24-
{{- range $ix, $choice := .PageEntries}}
25-
{{- if eq $ix $.SelectedIndex }}{{color $.Config.Icons.SelectFocus.Format }}{{ $.Config.Icons.SelectFocus.Text }} {{else}}{{color "default"}} {{end}}
26-
{{- $choice.Value}}
27-
{{- color "reset"}}{{"\n"}}
28-
{{- end}}
29-
{{- end}}`
17+
type item struct {
18+
title, desc string
19+
}
20+
21+
func (i item) Title() string { return i.title }
22+
func (i item) Description() string { return i.desc }
23+
func (i item) FilterValue() string { return i.title }
24+
25+
func createList(items []list.Item, title string) list.Model {
26+
const defaultWidth = 20
27+
const listHeight = 14
28+
29+
l := list.New(items, list.NewDefaultDelegate(), defaultWidth, listHeight)
30+
l.Title = title
31+
l.SetShowStatusBar(false)
32+
l.SetFilteringEnabled(true)
33+
l.Styles.Title = titleStyle
34+
l.Styles.PaginationStyle = paginationStyle
35+
l.Styles.HelpStyle = helpStyle
36+
37+
return l
38+
}
39+
40+
func runList(l list.Model) (string, error) {
41+
p := tea.NewProgram(l)
42+
m, err := p.StartReturningModel()
43+
if err != nil {
44+
return "", err
45+
}
46+
47+
if l, ok := m.(list.Model); ok {
48+
if len(l.SelectedItems()) > 0 {
49+
return l.SelectedItems()[0].(item).title, nil
50+
}
51+
}
52+
53+
return "", fmt.Errorf("no item selected")
3054
}
3155

3256
// createOpts builds the initial options for the survey prompts
@@ -46,17 +70,13 @@ func selectCluster(clusterNames []string) (string, error) {
4670
return clusterNames[0], nil
4771
}
4872

49-
prompt := &survey.Select{
50-
Message: "Select a cluster:",
51-
Options: clusterNames,
52-
PageSize: pageSize,
73+
var items []list.Item
74+
for _, name := range clusterNames {
75+
items = append(items, item{title: name})
5376
}
5477

55-
var selection string
56-
err := survey.AskOne(prompt, &selection, survey.WithIcons(func(icons *survey.IconSet) {
57-
icons.SelectFocus.Text = "➡"
58-
icons.SelectFocus.Format = "cyan"
59-
}))
78+
l := createList(items, "Select a cluster:")
79+
selection, err := runList(l)
6080
if err != nil {
6181
return "", err
6282
}
@@ -77,17 +97,13 @@ func selectService(serviceNames []string) (string, error) {
7797

7898
serviceNames = append(serviceNames, "*")
7999

80-
prompt := &survey.Select{
81-
Message: fmt.Sprintf("Select a service: %s", Yellow("(choose * to display all tasks)")),
82-
Options: createOpts(serviceNames),
83-
PageSize: pageSize,
100+
var items []list.Item
101+
for _, name := range serviceNames {
102+
items = append(items, item{title: name})
84103
}
85104

86-
var selection string
87-
err := survey.AskOne(prompt, &selection, survey.WithIcons(func(icons *survey.IconSet) {
88-
icons.SelectFocus.Text = "➡"
89-
icons.SelectFocus.Format = "magenta"
90-
}))
105+
l := createList(items, fmt.Sprintf("Select a service: %s", Yellow("(choose * to display all tasks)")))
106+
selection, err := runList(l)
91107
if err != nil {
92108
return "", err
93109
}
@@ -118,17 +134,13 @@ func selectTask(tasks map[string]*ecsTypes.Task) (*ecsTypes.Task, error) {
118134
taskOpts = append(taskOpts, fmt.Sprintf("%s | %s | (%s)", id, taskDefinition, strings.Join(containers, ",")))
119135
}
120136

121-
prompt := &survey.Select{
122-
Message: "Select a task:",
123-
Options: createOpts(taskOpts),
124-
PageSize: pageSize,
137+
var items []list.Item
138+
for _, opt := range taskOpts {
139+
items = append(items, item{title: opt})
125140
}
126141

127-
var selection string
128-
err := survey.AskOne(prompt, &selection, survey.WithIcons(func(icons *survey.IconSet) {
129-
icons.SelectFocus.Text = "➡"
130-
icons.SelectFocus.Format = "green"
131-
}))
142+
l := createList(items, "Select a task:")
143+
selection, err := runList(l)
132144
if err != nil {
133145
return &ecsTypes.Task{}, err
134146
}
@@ -155,17 +167,13 @@ func selectContainer(containers *[]ecsTypes.Container) (*ecsTypes.Container, err
155167
containerNames = append(containerNames, *c.Name)
156168
}
157169

158-
var selection string
159-
var prompt = &survey.Select{
160-
Message: "Multiple containers found, please select:",
161-
Options: createOpts(containerNames),
162-
PageSize: pageSize,
170+
var items []list.Item
171+
for _, name := range containerNames {
172+
items = append(items, item{title: name})
163173
}
164174

165-
err := survey.AskOne(prompt, &selection, survey.WithIcons(func(icons *survey.IconSet) {
166-
icons.SelectFocus.Text = "➡"
167-
icons.SelectFocus.Format = "yellow"
168-
}))
175+
l := createList(items, "Multiple containers found, please select:")
176+
selection, err := runList(l)
169177
if err != nil {
170178
return &ecsTypes.Container{}, err
171179
}

0 commit comments

Comments
 (0)