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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
package auth
import (
"errors"
"fmt"
"strings"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
authfield "github.com/kyren223/eko/internal/client/ui/auth/field"
)
var (
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("198"))
cursorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("198"))
noStyle = lipgloss.NewStyle()
errorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#F16265"))
fieldStyle = lipgloss.NewStyle().
PaddingLeft(1).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#007E8A"))
focusedButton = focusedStyle.Render("[ sign-up ]")
blurredButton = fmt.Sprintf("[ %s ]", lipgloss.NewStyle().
Foreground(lipgloss.Color("240")).
Render("sign-up"))
headerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#54D7A9"))
titleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#5874FF"))
)
type Model struct {
width int
height int
focusIndex int
fields []authfield.Model
}
func New() Model {
m := Model{
fields: make([]authfield.Model, 3),
}
var field authfield.Model
for i := range m.fields {
field = authfield.New(32)
field.Input.Cursor.Style = cursorStyle
field.Style = fieldStyle
field.ErrorStyle = errorStyle
field.FocusedStyle = focusedStyle
field.BlurredStyle = noStyle
switch i {
case 0:
field.Header = "Username"
field.Input.Placeholder = "Username"
field.Focus()
field.Input.Validate = func(username string) error {
if len(username) == 0 {
return errors.New("* required field")
}
return nil
}
case 1:
field.Header = "Email (Optional)"
field.Input.Placeholder = "Email"
field.Input.CharLimit = 64
field.Input.Validate = func(email string) error {
if len(email) == 0 {
return errors.New("* required field")
}
var errs []string
if !strings.ContainsRune(email, '@') {
errs = append(errs, "- missing @")
}
if !strings.ContainsRune(email, '.') {
errs = append(errs, "- missing .")
}
proton := strings.Contains(email, "proton.me") || strings.Contains(email, "protonmail.com")
gmail := strings.Contains(email, "gmail.com")
outlook := strings.Contains(email, "hotmail.com")
if !(proton || gmail || outlook) {
errs = append(errs, "- unknown email provider")
}
if errs != nil {
return errors.New(strings.Join(errs, "\n"))
}
return nil
}
case 2:
field.Header = "Password"
field.Input.Placeholder = "Password"
field.Input.EchoMode = textinput.EchoPassword
field.Input.EchoCharacter = '*'
// field.Input.EchoCharacter = '•'
field.Input.Validate = func(password string) error {
if len(password) == 0 {
return errors.New("* required field")
}
return nil
}
}
field.Header = headerStyle.Render(field.Header)
m.fields[i] = field
}
return m
}
func (m Model) Init() tea.Cmd {
return nil
}
func (m Model) View() string {
var builder strings.Builder
builder.WriteRune('\n')
for i := range m.fields {
builder.WriteString(m.fields[i].View())
if i < len(m.fields)-1 {
builder.WriteRune('\n')
}
}
button := &blurredButton
if m.focusIndex == len(m.fields) {
button = &focusedButton
}
submit := lipgloss.NewStyle().
Render(*button)
fmt.Fprintf(&builder, "\n\n%s", submit)
title := titleStyle.Render(`
____ _ ____ _ _ _ _ ___
[__ | | __ |\ | __ | | |__]
___] | |__] | \| |__| |
`)
// Tiny offset so odd numbers will have the extra char on the right, ie: 12 <thing> 13
content := lipgloss.JoinVertical(lipgloss.Center-0.01, title, builder.String())
width := lipgloss.Width(content)
vp := viewport.New(50, 25)
vp.SetContent(content)
vp.Style = vp.Style.
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#007E8A")).
// NOTE: Without -1 it wraps/truncates, not sure why
Padding(0, (vp.Width-width)/2-1)
return lipgloss.Place(
m.width, m.height,
lipgloss.Center, lipgloss.Center,
vp.View(),
)
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width, m.height = msg.Width, msg.Height
return m, nil
case tea.KeyMsg:
key := msg.Type
switch key {
case tea.KeyCtrlC:
return m, tea.Quit
case tea.KeyTab, tea.KeyShiftTab, tea.KeyEnter, tea.KeyUp, tea.KeyDown:
// User pressed submit
if key == tea.KeyEnter && m.focusIndex == len(m.fields) {
var cmds []tea.Cmd
for i, field := range m.fields {
if field.Input.Validate == nil {
continue
}
field.Input.Err = field.Input.Validate(field.Input.Value())
if field.Input.Err != nil {
var cmd tea.Cmd
m.fields[i], cmd = field.Update(msg)
cmds = append(cmds, cmd)
}
}
if len(cmds) == 0 {
return m, tea.Quit
}
return m, tea.Batch(cmds...)
}
// Cycle indexes
if key == tea.KeyUp || key == tea.KeyShiftTab {
m.focusIndex--
} else {
m.focusIndex++
}
if m.focusIndex > len(m.fields) {
m.focusIndex = 0
} else if m.focusIndex < 0 {
m.focusIndex = len(m.fields)
}
cmds := make([]tea.Cmd, len(m.fields))
for i := 0; i <= len(m.fields)-1; i++ {
if i == m.focusIndex {
cmds[i] = m.fields[i].Focus()
} else {
m.fields[i].Blur()
}
}
return m, tea.Batch(cmds...)
}
}
cmd := m.updateInputs(msg)
return m, cmd
}
func (m *Model) updateInputs(msg tea.Msg) tea.Cmd {
cmds := make([]tea.Cmd, len(m.fields))
// Only fields with Focus() set will respond, so it's safe to simply
// update all of them here without any further logic.
for i := range m.fields {
m.fields[i], cmds[i] = m.fields[i].Update(msg)
}
return tea.Batch(cmds...)
}
|