-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathformmodel.go
172 lines (148 loc) · 3.95 KB
/
formmodel.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package adapter
import (
"context"
"slices"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/ubuntu/authd/internal/brokers/layouts"
"github.com/ubuntu/authd/internal/brokers/layouts/entries"
"github.com/ubuntu/authd/internal/proto/authd"
"github.com/ubuntu/authd/log"
)
// formModel is the form layout type to allow authentication and return a password.
type formModel struct {
label string
focusableModels []authenticationComponent
focusIndex int
wait bool
}
// newFormModel initializes and return a new formModel.
func newFormModel(label, entryType, buttonLabel string, wait bool) formModel {
var focusableModels []authenticationComponent
// TODO: add digits and force validation.
switch entryType {
case entries.Chars, entries.CharsPassword:
entry := newTextInputModel(entryType)
focusableModels = append(focusableModels, &entry)
label = strings.TrimSuffix(label, ":") + ":"
}
if buttonLabel != "" {
button := newAuthReselectionButtonModel(buttonLabel)
focusableModels = append(focusableModels, button)
}
return formModel{
label: label,
wait: wait,
focusableModels: focusableModels,
}
}
// Init initializes formModel.
func (m formModel) Init() tea.Cmd {
var commands []tea.Cmd
for _, c := range m.focusableModels {
commands = append(commands, c.Init())
}
return tea.Batch(commands...)
}
// Update handles events and actions.
func (m formModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case startAuthentication:
// Reset the entry.
for _, fm := range m.focusableModels {
switch entry := fm.(type) {
case *textinputModel:
entry.SetValue("")
}
}
if !m.wait {
return m, nil
}
return m, tea.Sequence(m.updateFocusModel(msg), sendEvent(isAuthenticatedRequested{
item: &authd.IARequest_AuthenticationData_Wait{Wait: layouts.True},
}))
}
switch msg := msg.(type) {
// Key presses
case tea.KeyMsg:
switch msg.String() {
case "enter":
if m.focusIndex >= len(m.focusableModels) {
return m, nil
}
entry := m.focusableModels[m.focusIndex]
switch entry := entry.(type) {
case *textinputModel:
return m, sendEvent(isAuthenticatedRequested{
item: &authd.IARequest_AuthenticationData_Secret{
Secret: entry.Value(),
},
})
}
case "tab":
m.focusIndex++
if m.focusIndex == len(m.focusableModels) {
m.focusIndex = 0
}
var cmd tea.Cmd
for i, fm := range m.focusableModels {
if i != m.focusIndex {
fm.Blur()
continue
}
cmd = fm.Focus()
}
return m, cmd
}
}
return m, m.updateFocusModel(msg)
}
func (m *formModel) updateFocusModel(msg tea.Msg) tea.Cmd {
if m.focusIndex >= len(m.focusableModels) {
return nil
}
model, cmd := m.focusableModels[m.focusIndex].Update(msg)
m.focusableModels[m.focusIndex] = convertTo[authenticationComponent](model)
return cmd
}
// View renders a text view of the form.
func (m formModel) View() string {
var fields []string
if m.label != "" {
fields = append(fields, m.label)
}
for _, fm := range m.focusableModels {
fields = append(fields, fm.View())
}
return lipgloss.JoinVertical(lipgloss.Left,
fields...,
)
}
// Focus focuses this model.
func (m formModel) Focus() tea.Cmd {
log.Debugf(context.TODO(), "%T: Focus", m)
if m.focusIndex >= len(m.focusableModels) {
return nil
}
return m.focusableModels[m.focusIndex].Focus()
}
// Blur releases the focus from this model.
func (m formModel) Blur() {
log.Debugf(context.TODO(), "%T: Blur", m)
if m.focusIndex >= len(m.focusableModels) {
return
}
m.focusableModels[m.focusIndex].Blur()
}
// Focused returns whether this model is focused.
func (m formModel) Focused() bool {
if len(m.focusableModels) == 0 {
// We consider the model being focused in this case, since there's nothing
// to interact with, but we want to be able to draw.
return true
}
return slices.ContainsFunc(m.focusableModels, func(ac authenticationComponent) bool {
return ac.Focused()
})
}