@@ -7,26 +7,50 @@ import (
7
7
"fmt"
8
8
"strings"
9
9
10
- "github.com/AlecAivazis/survey/v2"
10
+ "github.com/charmbracelet/bubbletea"
11
+ "github.com/charmbracelet/bubbles/list"
11
12
"github.com/aws/aws-sdk-go-v2/aws"
12
13
13
14
ecsTypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
14
15
)
15
16
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" )
30
54
}
31
55
32
56
// createOpts builds the initial options for the survey prompts
@@ -46,17 +70,13 @@ func selectCluster(clusterNames []string) (string, error) {
46
70
return clusterNames [0 ], nil
47
71
}
48
72
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 })
53
76
}
54
77
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 )
60
80
if err != nil {
61
81
return "" , err
62
82
}
@@ -77,17 +97,13 @@ func selectService(serviceNames []string) (string, error) {
77
97
78
98
serviceNames = append (serviceNames , "*" )
79
99
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 })
84
103
}
85
104
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 )
91
107
if err != nil {
92
108
return "" , err
93
109
}
@@ -118,17 +134,13 @@ func selectTask(tasks map[string]*ecsTypes.Task) (*ecsTypes.Task, error) {
118
134
taskOpts = append (taskOpts , fmt .Sprintf ("%s | %s | (%s)" , id , taskDefinition , strings .Join (containers , "," )))
119
135
}
120
136
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 })
125
140
}
126
141
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 )
132
144
if err != nil {
133
145
return & ecsTypes.Task {}, err
134
146
}
@@ -155,17 +167,13 @@ func selectContainer(containers *[]ecsTypes.Container) (*ecsTypes.Container, err
155
167
containerNames = append (containerNames , * c .Name )
156
168
}
157
169
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 })
163
173
}
164
174
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 )
169
177
if err != nil {
170
178
return & ecsTypes.Container {}, err
171
179
}
0 commit comments