@@ -21,6 +21,15 @@ type ChooseUI struct {
21
21
22
22
// The users' choice.
23
23
chosen string
24
+
25
+ // app is the global application
26
+ app * tview.Application
27
+
28
+ // list contains the global list of text-entries.
29
+ list * tview.List
30
+
31
+ // inputField contains the global text-input field.
32
+ inputField * tview.InputField
24
33
}
25
34
26
35
// New creates a new UI, allowing the user to select from the available options.
@@ -30,81 +39,69 @@ func New(choices []string) *ChooseUI {
30
39
}
31
40
32
41
// Choose launches our user interface.
33
- func (ui * ChooseUI ) Choose () string {
42
+ func (ui * ChooseUI ) SetupUI () {
34
43
35
44
//
36
45
// Create the console-GUI application.
37
46
//
38
- app : = tview .NewApplication ()
47
+ ui . app = tview .NewApplication ()
39
48
40
49
//
41
50
// Create a list to hold our files.
42
51
//
43
- list : = tview .NewList ()
44
- list .ShowSecondaryText (false )
45
- list .SetWrapAround (false )
52
+ ui . list = tview .NewList ()
53
+ ui . list .ShowSecondaryText (false )
54
+ ui . list .SetWrapAround (false )
46
55
47
56
//
48
57
// Add all the choices to it.
49
58
//
50
59
for _ , entry := range ui .Choices {
51
- list .AddItem (entry , "" , ' ' , nil )
60
+ ui . list .AddItem (entry , "" , ' ' , nil )
52
61
}
53
62
54
- //
55
- // If the user presses return in the list then choose that item.
56
- //
57
- list .SetInputCapture (func (event * tcell.EventKey ) * tcell.EventKey {
58
- if event .Key () == tcell .KeyEnter {
59
- selected := list .GetCurrentItem ()
60
- ui .chosen , _ = list .GetItemText (selected )
61
- app .Stop ()
62
- }
63
- return event
64
- })
65
-
66
63
//
67
64
// Create a filter input-view
68
65
//
69
- inputField : = tview .NewInputField ().
66
+ ui . inputField = tview .NewInputField ().
70
67
SetLabel ("Filter: " ).
71
68
SetDoneFunc (func (key tcell.Key ) {
72
69
if key == tcell .KeyEnter {
73
70
74
71
// get the selected index
75
- selected := list .GetCurrentItem ()
72
+ selected := ui . list .GetCurrentItem ()
76
73
77
74
// less than the entry count?
78
- if list .GetItemCount () > 0 {
79
- ui .chosen , _ = list .GetItemText (selected )
75
+ if ui . list .GetItemCount () > 0 {
76
+ ui .chosen , _ = ui . list .GetItemText (selected )
80
77
}
81
- app .Stop ()
78
+ ui . app .Stop ()
82
79
}
83
80
})
84
81
85
82
//
86
83
// Setup the filter-function, to filter the list to
87
84
// only matches present in the input-field
88
85
//
89
- inputField .SetAutocompleteFunc (func (currentText string ) (entries []string ) {
86
+ ui . inputField .SetAutocompleteFunc (func (currentText string ) (entries []string ) {
90
87
// Get text
91
88
input := strings .TrimSpace (currentText )
92
89
93
90
// empty? All items should be visible
94
91
if input == "" {
95
- list .Clear ()
92
+ ui . list .Clear ()
96
93
for _ , entry := range ui .Choices {
97
- list .AddItem (entry , "" , ' ' , nil )
94
+ ui . list .AddItem (entry , "" , ' ' , nil )
98
95
}
99
96
return
100
97
}
101
98
102
99
// Otherwise filter by input
103
100
input = strings .ToLower (input )
104
- list .Clear ()
101
+ ui . list .Clear ()
105
102
for _ , entry := range ui .Choices {
106
103
if strings .Contains (strings .ToLower (entry ), input ) {
107
- list .AddItem (entry , "" , ' ' , nil )
104
+ ui . list .AddItem (entry , "" , ' ' , nil )
108
105
}
109
106
}
110
107
@@ -120,64 +117,92 @@ func (ui *ChooseUI) Choose() string {
120
117
// Create a layout grid, add the filter-box and the list.
121
118
//
122
119
grid := tview .NewFlex ().SetFullScreen (true ).SetDirection (tview .FlexRow )
123
- grid .AddItem (inputField , 1 , 0 , true )
124
- grid .AddItem (list , 0 , 1 , false )
120
+ grid .AddItem (ui . inputField , 1 , 0 , true )
121
+ grid .AddItem (ui . list , 0 , 1 , false )
125
122
grid .AddItem (help , 2 , 1 , false )
126
123
124
+ ui .app .SetRoot (grid , true ).SetFocus (grid ).EnableMouse (true )
125
+
126
+ }
127
+
128
+ func (ui * ChooseUI ) SetupKeyBinding () {
129
+
130
+ //
131
+ // If the user presses return in the list then choose that item.
132
+ //
133
+ ui .list .SetInputCapture (func (event * tcell.EventKey ) * tcell.EventKey {
134
+ if event .Key () == tcell .KeyEnter {
135
+ selected := ui .list .GetCurrentItem ()
136
+ ui .chosen , _ = ui .list .GetItemText (selected )
137
+ ui .app .Stop ()
138
+ }
139
+ return event
140
+ })
141
+
127
142
//
128
143
// Global keyboard handler, use "TAB" to switch focus.
129
144
//
130
145
// Arrows and HOME/END work as expected regardless of focus-state
131
146
//
132
- app .SetInputCapture (func (event * tcell.EventKey ) * tcell.EventKey {
147
+ ui . app .SetInputCapture (func (event * tcell.EventKey ) * tcell.EventKey {
133
148
switch event .Key () {
134
149
135
150
// Home
136
151
case tcell .KeyHome :
137
- list .SetCurrentItem (0 )
152
+ ui . list .SetCurrentItem (0 )
138
153
139
154
// End
140
155
case tcell .KeyEnd :
141
- list .SetCurrentItem (list .GetItemCount ())
156
+ ui . list .SetCurrentItem (ui . list .GetItemCount ())
142
157
143
158
// Up arrow
144
159
case tcell .KeyUp :
145
- selected := list .GetCurrentItem ()
160
+ selected := ui . list .GetCurrentItem ()
146
161
if selected > 0 {
147
162
selected --
148
163
} else {
149
- selected = list .GetItemCount ()
164
+ selected = ui . list .GetItemCount ()
150
165
}
151
- list .SetCurrentItem (selected )
166
+ ui . list .SetCurrentItem (selected )
152
167
return nil
153
168
154
169
// Down arrow
155
170
case tcell .KeyDown :
156
- selected := list .GetCurrentItem ()
171
+ selected := ui . list .GetCurrentItem ()
157
172
selected ++
158
- list .SetCurrentItem (selected )
173
+ ui . list .SetCurrentItem (selected )
159
174
return nil
160
175
161
176
// TAB
162
177
case tcell .KeyTab , tcell .KeyBacktab :
163
- if list .HasFocus () {
164
- app .SetFocus (inputField )
178
+ if ui . list .HasFocus () {
179
+ ui . app .SetFocus (ui . inputField )
165
180
} else {
166
- app .SetFocus (list )
181
+ ui . app .SetFocus (ui . list )
167
182
}
168
183
return nil
169
184
170
185
// Escape
171
186
case tcell .KeyEscape :
172
- app .Stop ()
187
+ ui . app .Stop ()
173
188
}
174
189
return event
175
190
})
176
191
192
+ }
193
+
194
+ // Choose launches our user interface.
195
+ func (ui * ChooseUI ) Choose () string {
196
+
197
+ ui .SetupUI ()
198
+
199
+ ui .SetupKeyBinding ()
200
+
177
201
//
178
202
// Launch the application.
179
203
//
180
- if err := app .SetRoot (grid , true ).SetFocus (grid ).EnableMouse (true ).Run (); err != nil {
204
+ err := ui .app .Run ()
205
+ if err != nil {
181
206
panic (err )
182
207
}
183
208
0 commit comments