signature.go

 1package tui
 2
 3import (
 4	"charm.land/bubbles/v2/textarea"
 5	tea "charm.land/bubbletea/v2"
 6	"charm.land/lipgloss/v2"
 7	"github.com/floatpane/matcha/config"
 8)
 9
10// SignatureEditor displays the signature editing screen.
11type SignatureEditor struct {
12	textarea  textarea.Model
13	accountID string
14	width     int
15	height    int
16}
17
18// NewSignatureEditor creates a new signature editor model.
19func NewSignatureEditor(accountID string) *SignatureEditor {
20	ta := textarea.New()
21	ta.Placeholder = "Enter your email signature...\n\nExample:\nBest regards,\nDrew"
22	ta.SetHeight(10)
23	ta.SetStyles(ThemedTextAreaStyles())
24	ta.Focus()
25
26	// Load existing signature
27	if accountID != "" {
28		if sig, err := config.LoadRawAccountSignature(&config.Account{ID: accountID}); err == nil && sig != "" {
29			ta.SetValue(sig)
30		}
31	} else {
32		if sig, err := config.LoadSignature(); err == nil && sig != "" {
33			ta.SetValue(sig)
34		}
35	}
36
37	return &SignatureEditor{
38		textarea:  ta,
39		accountID: accountID,
40	}
41}
42
43// Init initializes the signature editor model.
44func (m *SignatureEditor) Init() tea.Cmd {
45	return textarea.Blink
46}
47
48// Update handles messages for the signature editor model.
49func (m *SignatureEditor) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
50	var cmd tea.Cmd
51
52	switch msg := msg.(type) {
53	case tea.WindowSizeMsg:
54		m.width = msg.Width
55		m.height = msg.Height
56		m.textarea.SetWidth(msg.Width - 4)
57		m.textarea.SetHeight(msg.Height - 10)
58		return m, nil
59
60	case tea.KeyPressMsg:
61		switch msg.String() {
62		case "ctrl+c":
63			return m, tea.Quit
64		case "esc":
65			// Save and go back to settings
66			signature := m.textarea.Value()
67			if m.accountID != "" {
68				go config.SaveSignatureForAccount(m.accountID, signature) //nolint:errcheck
69			} else {
70				go config.SaveSignature(signature) //nolint:errcheck
71			}
72			return m, func() tea.Msg { return GoToSettingsMsg{} }
73		}
74	}
75
76	m.textarea, cmd = m.textarea.Update(msg)
77	return m, cmd
78}
79
80// View renders the signature editor screen.
81func (m *SignatureEditor) View() tea.View {
82	title := titleStyle.Render("Email Signature")
83	hint := accountEmailStyle.Render("This signature will be appended to your emails.")
84
85	return tea.NewView(lipgloss.JoinVertical(lipgloss.Left,
86		title,
87		hint,
88		"",
89		m.textarea.View(),
90		"",
91		helpStyle.Render("esc: save & back"),
92	))
93}