Skip to content

Commit

Permalink
feat: add CC and BCC inputs if --cc or --bcc is specified. (#65)
Browse files Browse the repository at this point in the history
This commit adds CC + BCC hidden inputs and are shown if the user passes
in some value to the `-cc` or `--bcc` flags.
  • Loading branch information
maaslalani authored Apr 25, 2024
1 parent ef84570 commit 7f2ec5e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
2 changes: 2 additions & 0 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ func sendResendEmail(to, _, _ []string, from, subject, body string, attachments
From: from,
To: to,
Subject: subject,
Cc: cc,
Bcc: bcc,
Html: html.String(),
Text: body,
Attachments: makeAttachments(attachments),
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ var rootCmd = &cobra.Command{
p := tea.NewProgram(NewModel(resend.SendEmailRequest{
From: from,
To: to,
Bcc: bcc,
Cc: cc,
Subject: subject,
Text: body,
Attachments: makeAttachments(attachments),
Expand Down
78 changes: 75 additions & 3 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type State int
const (
editingFrom State = iota
editingTo
editingCc
editingBcc
editingSubject
editingBody
editingAttachments
Expand Down Expand Up @@ -68,8 +70,9 @@ type Model struct {
// This is a list of file paths which are picked with a filepicker.
Attachments list.Model

Cc textinput.Model
Bcc textinput.Model
showCc bool
Cc textinput.Model
Bcc textinput.Model

// filepicker is used to pick file attachments.
filepicker filepicker.Model
Expand Down Expand Up @@ -102,6 +105,24 @@ func NewModel(defaults resend.SendEmailRequest, deliveryMethod DeliveryMethod) M
to.Placeholder = "[email protected]"
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 = "[email protected]"
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 = "[email protected]"
bcc.SetValue(strings.Join(defaults.Bcc, ToSeparator))

subject := textinput.New()
subject.Prompt = "Subject "
subject.PromptStyle = labelStyle.Copy()
Expand Down Expand Up @@ -172,6 +193,9 @@ func NewModel(defaults resend.SendEmailRequest, deliveryMethod DeliveryMethod) M
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,
Expand Down Expand Up @@ -225,6 +249,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
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
Expand All @@ -244,8 +276,16 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.state = hoveringSendButton
case editingTo:
m.state = editingFrom
case editingSubject:
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:
Expand Down Expand Up @@ -286,6 +326,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
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)
Expand Down Expand Up @@ -320,8 +366,18 @@ func (m *Model) blurInputs() {
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
Expand All @@ -342,6 +398,16 @@ func (m *Model) focusActiveInput() {
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
Expand Down Expand Up @@ -376,6 +442,12 @@ func (m Model) View() string {
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())
Expand Down

0 comments on commit 7f2ec5e

Please sign in to comment.