-
Notifications
You must be signed in to change notification settings - Fork 51
/
model.go
473 lines (427 loc) · 11.8 KB
/
model.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package main
import (
"os"
"strings"
"time"
"github.com/charmbracelet/bubbles/filepicker"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/exp/ordered"
"github.com/resendlabs/resend-go"
)
// State is the current state of the application.
type State int
const (
editingFrom State = iota
editingTo
editingCc
editingBcc
editingSubject
editingBody
editingAttachments
hoveringSendButton
pickingFile
sendingEmail
)
// DeliveryMethod is the method of delivery for the email.
type DeliveryMethod int
const (
// None is the default delivery method.
None DeliveryMethod = iota
// Resend uses https://resend.com to send an email.
Resend
// SMTP uses an SMTP server to send an email.
SMTP
// Unknown is set when the user has not chosen a single delivery method.
// i.e. multiple delivery methods are set.
Unknown
)
// Model is Pop's application model.
type Model struct {
// state represents the current state of the application.
state State
// DeliveryMethod is whether we are using DeliveryMethod or Resend.
DeliveryMethod DeliveryMethod
// From represents the sender's email address.
From textinput.Model
// To represents the recipient's email address.
// This can be a comma-separated list of addresses.
To textinput.Model
// Subject represents the email's subject.
Subject textinput.Model
// Body represents the email's body.
// This can be written in markdown and will be converted to HTML.
Body textarea.Model
// Attachments represents the email's attachments.
// This is a list of file paths which are picked with a filepicker.
Attachments list.Model
showCc bool
Cc textinput.Model
Bcc textinput.Model
// filepicker is used to pick file attachments.
filepicker filepicker.Model
loadingSpinner spinner.Model
help help.Model
keymap KeyMap
quitting bool
abort bool
err error
}
// NewModel returns a new model for the application.
func NewModel(defaults resend.SendEmailRequest, deliveryMethod DeliveryMethod) Model {
from := textinput.New()
from.Prompt = "From "
from.Placeholder = "me@example.com"
from.PromptStyle = labelStyle.Copy()
from.PromptStyle = labelStyle
from.TextStyle = textStyle
from.Cursor.Style = cursorStyle
from.PlaceholderStyle = placeholderStyle
from.SetValue(defaults.From)
to := textinput.New()
to.Prompt = "To "
to.PromptStyle = labelStyle.Copy()
to.Cursor.Style = cursorStyle
to.PlaceholderStyle = placeholderStyle
to.TextStyle = textStyle
to.Placeholder = "you@example.com"
to.SetValue(strings.Join(defaults.To, ToSeparator))
cc := textinput.New()
cc.Prompt = "Cc "
cc.PromptStyle = labelStyle.Copy()
cc.Cursor.Style = cursorStyle
cc.PlaceholderStyle = placeholderStyle
cc.TextStyle = textStyle
cc.Placeholder = "cc@example.com"
cc.SetValue(strings.Join(defaults.Cc, ToSeparator))
bcc := textinput.New()
bcc.Prompt = "Bcc "
bcc.PromptStyle = labelStyle.Copy()
bcc.Cursor.Style = cursorStyle
bcc.PlaceholderStyle = placeholderStyle
bcc.TextStyle = textStyle
bcc.Placeholder = "bcc@example.com"
bcc.SetValue(strings.Join(defaults.Bcc, ToSeparator))
subject := textinput.New()
subject.Prompt = "Subject "
subject.PromptStyle = labelStyle.Copy()
subject.Cursor.Style = cursorStyle
subject.PlaceholderStyle = placeholderStyle
subject.TextStyle = textStyle
subject.Placeholder = "Hello!"
subject.SetValue(defaults.Subject)
body := textarea.New()
body.Placeholder = "# Email"
body.ShowLineNumbers = false
body.FocusedStyle.CursorLine = activeTextStyle
body.FocusedStyle.Prompt = activeLabelStyle
body.FocusedStyle.Text = activeTextStyle
body.FocusedStyle.Placeholder = placeholderStyle
body.BlurredStyle.CursorLine = textStyle
body.BlurredStyle.Prompt = labelStyle
body.BlurredStyle.Text = textStyle
body.BlurredStyle.Placeholder = placeholderStyle
body.Cursor.Style = cursorStyle
body.CharLimit = 4000
body.SetValue(defaults.Text)
// Adjust for signature (if none, this is a no-op)
body.CursorUp()
body.CursorUp()
body.Blur()
// Decide which input to focus.
var state State
switch {
case defaults.From == "":
state = editingFrom
case len(defaults.To) == 0:
state = editingTo
case defaults.Subject == "":
state = editingSubject
case defaults.Text == "":
state = editingBody
}
attachments := list.New([]list.Item{}, attachmentDelegate{}, 0, 3)
attachments.DisableQuitKeybindings()
attachments.SetShowTitle(true)
attachments.Title = "Attachments"
attachments.Styles.Title = labelStyle
attachments.Styles.TitleBar = labelStyle
attachments.Styles.NoItems = placeholderStyle
attachments.SetShowHelp(false)
attachments.SetShowStatusBar(false)
attachments.SetStatusBarItemName("attachment", "attachments")
attachments.SetShowPagination(false)
for _, a := range defaults.Attachments {
attachments.InsertItem(0, attachment(a.Filename))
}
picker := filepicker.New()
picker.CurrentDirectory, _ = os.UserHomeDir()
loadingSpinner := spinner.New()
loadingSpinner.Style = activeLabelStyle
loadingSpinner.Spinner = spinner.Dot
m := Model{
state: state,
From: from,
To: to,
showCc: len(cc.Value()) > 0 || len(bcc.Value()) > 0,
Cc: cc,
Bcc: bcc,
Subject: subject,
Body: body,
Attachments: attachments,
filepicker: picker,
help: help.New(),
keymap: DefaultKeybinds(),
loadingSpinner: loadingSpinner,
DeliveryMethod: deliveryMethod,
}
m.focusActiveInput()
return m
}
// Init initializes the model.
func (m Model) Init() tea.Cmd {
return tea.Batch(
m.From.Cursor.BlinkCmd(),
)
}
type clearErrMsg struct{}
func clearErrAfter(d time.Duration) tea.Cmd {
return tea.Tick(d, func(t time.Time) tea.Msg {
return clearErrMsg{}
})
}
// Update is the update loop for the model.
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case sendEmailSuccessMsg:
m.quitting = true
return m, tea.Quit
case sendEmailFailureMsg:
m.blurInputs()
m.state = editingFrom
m.focusActiveInput()
m.err = msg
return m, clearErrAfter(10 * time.Second)
case clearErrMsg:
m.err = nil
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keymap.NextInput):
m.blurInputs()
switch m.state {
case editingFrom:
m.state = editingTo
m.To.Focus()
case editingTo:
if m.showCc {
m.state = editingCc
} else {
m.state = editingSubject
}
case editingCc:
m.state = editingBcc
case editingBcc:
m.state = editingSubject
case editingSubject:
m.state = editingBody
case editingBody:
m.state = editingAttachments
case editingAttachments:
m.state = hoveringSendButton
case hoveringSendButton:
m.state = editingFrom
}
m.focusActiveInput()
case key.Matches(msg, m.keymap.PrevInput):
m.blurInputs()
switch m.state {
case editingFrom:
m.state = hoveringSendButton
case editingTo:
m.state = editingFrom
case editingCc:
m.state = editingTo
case editingBcc:
m.state = editingCc
case editingSubject:
if m.showCc {
m.state = editingBcc
} else {
m.state = editingTo
}
case editingBody:
m.state = editingSubject
case editingAttachments:
m.state = editingBody
case hoveringSendButton:
m.state = editingAttachments
}
m.focusActiveInput()
case key.Matches(msg, m.keymap.Back):
m.state = editingAttachments
m.updateKeymap()
return m, nil
case key.Matches(msg, m.keymap.Send):
m.state = sendingEmail
return m, tea.Batch(
m.loadingSpinner.Tick,
m.sendEmailCmd(),
)
case key.Matches(msg, m.keymap.Attach):
m.state = pickingFile
return m, m.filepicker.Init()
case key.Matches(msg, m.keymap.Unattach):
m.Attachments.RemoveItem(m.Attachments.Index())
m.Attachments.SetHeight(ordered.Max(len(m.Attachments.Items()), 1) + 2)
case key.Matches(msg, m.keymap.Quit):
m.quitting = true
m.abort = true
return m, tea.Quit
}
}
m.updateKeymap()
var cmds []tea.Cmd
var cmd tea.Cmd
m.From, cmd = m.From.Update(msg)
cmds = append(cmds, cmd)
m.To, cmd = m.To.Update(msg)
cmds = append(cmds, cmd)
if m.showCc {
m.Cc, cmd = m.Cc.Update(msg)
cmds = append(cmds, cmd)
m.Bcc, cmd = m.Bcc.Update(msg)
cmds = append(cmds, cmd)
}
m.Subject, cmd = m.Subject.Update(msg)
cmds = append(cmds, cmd)
m.Body, cmd = m.Body.Update(msg)
cmds = append(cmds, cmd)
m.filepicker, cmd = m.filepicker.Update(msg)
cmds = append(cmds, cmd)
switch m.state {
case pickingFile:
if didSelect, path := m.filepicker.DidSelectFile(msg); didSelect {
m.Attachments.InsertItem(0, attachment(path))
m.Attachments.SetHeight(len(m.Attachments.Items()) + 2)
m.state = editingAttachments
m.updateKeymap()
}
case editingAttachments:
m.Attachments, cmd = m.Attachments.Update(msg)
cmds = append(cmds, cmd)
case sendingEmail:
m.loadingSpinner, cmd = m.loadingSpinner.Update(msg)
cmds = append(cmds, cmd)
}
m.help, cmd = m.help.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m *Model) blurInputs() {
m.From.Blur()
m.To.Blur()
m.Subject.Blur()
m.Body.Blur()
if m.showCc {
m.Cc.Blur()
m.Bcc.Blur()
}
m.From.PromptStyle = labelStyle
m.To.PromptStyle = labelStyle
if m.showCc {
m.Cc.PromptStyle = labelStyle
m.Cc.TextStyle = textStyle
m.Bcc.PromptStyle = labelStyle
m.Bcc.TextStyle = textStyle
}
m.Subject.PromptStyle = labelStyle
m.From.TextStyle = textStyle
m.To.TextStyle = textStyle
m.Subject.TextStyle = textStyle
m.Attachments.Styles.Title = labelStyle
m.Attachments.SetDelegate(attachmentDelegate{false})
}
func (m *Model) focusActiveInput() {
switch m.state {
case editingFrom:
m.From.PromptStyle = activeLabelStyle
m.From.TextStyle = activeTextStyle
m.From.Focus()
m.From.CursorEnd()
case editingTo:
m.To.PromptStyle = activeLabelStyle
m.To.TextStyle = activeTextStyle
m.To.Focus()
m.To.CursorEnd()
case editingCc:
m.Cc.PromptStyle = activeLabelStyle
m.Cc.TextStyle = activeTextStyle
m.Cc.Focus()
m.Cc.CursorEnd()
case editingBcc:
m.Bcc.PromptStyle = activeLabelStyle
m.Bcc.TextStyle = activeTextStyle
m.Bcc.Focus()
m.Bcc.CursorEnd()
case editingSubject:
m.Subject.PromptStyle = activeLabelStyle
m.Subject.TextStyle = activeTextStyle
m.Subject.Focus()
m.Subject.CursorEnd()
case editingBody:
m.Body.Focus()
m.Body.CursorEnd()
case editingAttachments:
m.Attachments.Styles.Title = activeLabelStyle
m.Attachments.SetDelegate(attachmentDelegate{true})
}
}
// View displays the application.
func (m Model) View() string {
if m.quitting {
return ""
}
switch m.state {
case pickingFile:
return "\n" + activeLabelStyle.Render("Attachments") + " " + commentStyle.Render(m.filepicker.CurrentDirectory) +
"\n\n" + m.filepicker.View()
case sendingEmail:
return "\n " + m.loadingSpinner.View() + "Sending email"
}
var s strings.Builder
s.WriteString(m.From.View())
s.WriteString("\n")
s.WriteString(m.To.View())
s.WriteString("\n")
if m.showCc {
s.WriteString(m.Cc.View())
s.WriteString("\n")
s.WriteString(m.Bcc.View())
s.WriteString("\n")
}
s.WriteString(m.Subject.View())
s.WriteString("\n\n")
s.WriteString(m.Body.View())
s.WriteString("\n\n")
s.WriteString(m.Attachments.View())
s.WriteString("\n")
if m.state == hoveringSendButton && m.canSend() {
s.WriteString(sendButtonActiveStyle.Render("Send"))
} else if m.state == hoveringSendButton {
s.WriteString(sendButtonInactiveStyle.Render("Send"))
} else {
s.WriteString(sendButtonStyle.Render("Send"))
}
s.WriteString("\n\n")
s.WriteString(m.help.View(m.keymap))
if m.err != nil {
s.WriteString("\n\n")
s.WriteString(errorStyle.Render(m.err.Error()))
}
return paddedStyle.Render(s.String())
}