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	width    int
14	height   int
15}
16
17// NewSignatureEditor creates a new signature editor model.
18func NewSignatureEditor() *SignatureEditor {
19	ta := textarea.New()
20	ta.Placeholder = "Enter your email signature...\n\nExample:\nBest regards,\nDrew"
21	ta.SetHeight(10)
22	ta.SetStyles(ThemedTextAreaStyles())
23	ta.Focus()
24
25	// Load existing signature
26	if sig, err := config.LoadSignature(); err == nil && sig != "" {
27		ta.SetValue(sig)
28	}
29
30	return &SignatureEditor{
31		textarea: ta,
32	}
33}
34
35// Init initializes the signature editor model.
36func (m *SignatureEditor) Init() tea.Cmd {
37	return textarea.Blink
38}
39
40// Update handles messages for the signature editor model.
41func (m *SignatureEditor) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
42	var cmd tea.Cmd
43
44	switch msg := msg.(type) {
45	case tea.WindowSizeMsg:
46		m.width = msg.Width
47		m.height = msg.Height
48		m.textarea.SetWidth(msg.Width - 4)
49		m.textarea.SetHeight(msg.Height - 10)
50		return m, nil
51
52	case tea.KeyPressMsg:
53		switch msg.String() {
54		case "ctrl+c":
55			return m, tea.Quit
56		case "esc":
57			// Save and go back to settings
58			signature := m.textarea.Value()
59			go config.SaveSignature(signature)
60			return m, func() tea.Msg { return GoToSettingsMsg{} }
61		}
62	}
63
64	m.textarea, cmd = m.textarea.Update(msg)
65	return m, cmd
66}
67
68// View renders the signature editor screen.
69func (m *SignatureEditor) View() tea.View {
70	title := titleStyle.Render("Email Signature")
71	hint := accountEmailStyle.Render("This signature will be appended to your emails.")
72
73	return tea.NewView(lipgloss.JoinVertical(lipgloss.Left,
74		title,
75		hint,
76		"",
77		m.textarea.View(),
78		"",
79		helpStyle.Render("esc: save & back"),
80	))
81}