@@ -3,11 +3,9 @@ package adapter
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "strconv"
7
6
8
- "github.com/charmbracelet/bubbles/list"
7
+ tea_list "github.com/charmbracelet/bubbles/list"
9
8
tea "github.com/charmbracelet/bubbletea"
10
- "github.com/charmbracelet/lipgloss"
11
9
"github.com/msteinert/pam/v2"
12
10
"github.com/ubuntu/authd/internal/brokers/layouts"
13
11
"github.com/ubuntu/authd/internal/brokers/layouts/entries"
@@ -17,10 +15,8 @@ import (
17
15
18
16
// authModeSelectionModel is the model list to select supported authentication modes.
19
17
type authModeSelectionModel struct {
20
- list.Model
21
- focused bool
18
+ List
22
19
23
- clientType PamClientType
24
20
supportedUILayouts []* authd.UILayout
25
21
availableAuthModes []* authd.GAMResponse_AuthenticationMode
26
22
currentAuthModeSelectedID string
@@ -44,9 +40,6 @@ type authModeSelected struct {
44
40
id string
45
41
}
46
42
47
- // authModeSelectionFocused is the internal event to signal that the auth mode selection view is focused.
48
- type authModeSelectionFocused struct {}
49
-
50
43
// selectAuthMode selects current authentication mode.
51
44
func selectAuthMode (id string ) tea.Cmd {
52
45
return func () tea.Msg {
@@ -58,27 +51,8 @@ func selectAuthMode(id string) tea.Cmd {
58
51
59
52
// newAuthModeSelectionModel initializes an empty list with default options of authModeSelectionModel.
60
53
func newAuthModeSelectionModel (clientType PamClientType ) authModeSelectionModel {
61
- // FIXME: decouple UI from data model.
62
- if clientType != InteractiveTerminal {
63
- return authModeSelectionModel {
64
- Model : list .New (nil , itemLayout {}, 0 , 0 ),
65
- clientType : clientType ,
66
- }
67
- }
68
-
69
- l := list .New (nil , itemLayout {}, 80 , 24 )
70
- l .Title = "Select your authentication method"
71
- l .SetShowStatusBar (false )
72
- l .SetShowHelp (false )
73
- l .DisableQuitKeybindings ()
74
-
75
- l .Styles .Title = lipgloss .NewStyle ()
76
- /*l.Styles.PaginationStyle = paginationStyle
77
- l.Styles.HelpStyle = helpStyle*/
78
-
79
54
return authModeSelectionModel {
80
- Model : l ,
81
- clientType : clientType ,
55
+ List : NewList (clientType , "Select your authentication method" ),
82
56
}
83
57
}
84
58
@@ -128,10 +102,6 @@ func (m *authModeSelectionModel) Init() tea.Cmd {
128
102
// Update handles events and actions.
129
103
func (m authModeSelectionModel ) Update (msg tea.Msg ) (authModeSelectionModel , tea.Cmd ) {
130
104
switch msg := msg .(type ) {
131
- case authModeSelectionFocused :
132
- log .Debugf (context .TODO (), "%T: %#v" , m , msg )
133
- m .focused = true
134
-
135
105
case supportedUILayoutsReceived :
136
106
log .Debugf (context .TODO (), "%#v" , msg )
137
107
if len (msg .layouts ) == 0 {
@@ -147,7 +117,7 @@ func (m authModeSelectionModel) Update(msg tea.Msg) (authModeSelectionModel, tea
147
117
log .Debugf (context .TODO (), "%#v" , msg )
148
118
m .availableAuthModes = msg .authModes
149
119
150
- var allAuthModes []list .Item
120
+ var allAuthModes []tea_list .Item
151
121
var firstAuthModeID string
152
122
for _ , a := range m .availableAuthModes {
153
123
if firstAuthModeID == "" {
@@ -167,6 +137,15 @@ func (m authModeSelectionModel) Update(msg tea.Msg) (authModeSelectionModel, tea
167
137
168
138
return m , tea .Sequence (cmds ... )
169
139
140
+ case listItemSelected :
141
+ if ! m .Focused () {
142
+ return m , nil
143
+ }
144
+
145
+ log .Debugf (context .TODO (), "%#v" , msg )
146
+ authMode := convertTo [authModeItem ](msg .item )
147
+ return m , selectAuthMode (authMode .id )
148
+
170
149
case authModeSelected :
171
150
log .Debugf (context .TODO (), "%#v" , msg )
172
151
// Ensure auth mode id is valid
@@ -190,76 +169,11 @@ func (m authModeSelectionModel) Update(msg tea.Msg) (authModeSelectionModel, tea
190
169
})
191
170
}
192
171
193
- if m .clientType != InteractiveTerminal {
194
- return m , nil
195
- }
196
-
197
- // interaction events
198
- if ! m .focused {
199
- return m , nil
200
- }
201
- switch msg := msg .(type ) {
202
- // Key presses
203
- case tea.KeyMsg :
204
- switch msg .String () {
205
- case "enter" :
206
- item := m .SelectedItem ()
207
- if item == nil {
208
- return m , nil
209
- }
210
- authMode := convertTo [authModeItem ](item )
211
- cmd := selectAuthMode (authMode .id )
212
- return m , cmd
213
- case "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" :
214
- // This is necessarily an integer, so above
215
- choice , _ := strconv .Atoi (msg .String ())
216
- items := m .Items ()
217
- if choice > len (items ) {
218
- return m , nil
219
- }
220
- item := items [choice - 1 ]
221
- authMode := convertTo [authModeItem ](item )
222
- cmd := selectAuthMode (authMode .id )
223
- return m , cmd
224
- }
225
- }
226
-
227
172
var cmd tea.Cmd
228
- m .Model , cmd = m .Model .Update (msg )
173
+ m .List , cmd = m .List .Update (msg )
229
174
return m , cmd
230
175
}
231
176
232
- // View renders a text view of the authentication UI.
233
- func (m authModeSelectionModel ) View () string {
234
- if ! m .Focused () {
235
- return ""
236
- }
237
-
238
- return m .Model .View ()
239
- }
240
-
241
- // Focus focuses this model.
242
- func (m * authModeSelectionModel ) Focus () tea.Cmd {
243
- log .Debugf (context .TODO (), "%T: Focus" , m )
244
- return sendEvent (authModeSelectionFocused {})
245
- }
246
-
247
- // Focused returns if this model is focused.
248
- func (m * authModeSelectionModel ) Focused () bool {
249
- return m .focused
250
- }
251
-
252
- // Blur releases the focus from this model.
253
- func (m * authModeSelectionModel ) Blur () {
254
- log .Debugf (context .TODO (), "%T: Blur" , m )
255
- m .focused = false
256
- }
257
-
258
- // WillCaptureEscape returns if this broker may capture Esc typing on keyboard.
259
- func (m authModeSelectionModel ) WillCaptureEscape () bool {
260
- return m .FilterState () == list .Filtering
261
- }
262
-
263
177
// authModeItem is the list item corresponding to an authentication mode.
264
178
type authModeItem struct {
265
179
id string
0 commit comments