signature.go

 1package tui
 2
 3import (
 4	"github.com/charmbracelet/bubbles/textarea"
 5	tea "github.com/charmbracelet/bubbletea"
 6	"github.com/charmbracelet/lipgloss"
 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.Focus()
23
24	// Load existing signature
25	if sig, err := config.LoadSignature(); err == nil && sig != "" {
26		ta.SetValue(sig)
27	}
28
29	return &SignatureEditor{
30		textarea: ta,
31	}
32}
33
34// Init initializes the signature editor model.
35func (m *SignatureEditor) Init() tea.Cmd {
36	return textarea.Blink
37}
38
39// Update handles messages for the signature editor model.
40func (m *SignatureEditor) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
41	var cmd tea.Cmd
42
43	switch msg := msg.(type) {
44	case tea.WindowSizeMsg:
45		m.width = msg.Width
46		m.height = msg.Height
47		m.textarea.SetWidth(msg.Width - 4)
48		m.textarea.SetHeight(msg.Height - 10)
49		return m, nil
50
51	case tea.KeyMsg:
52		switch msg.Type {
53		case tea.KeyCtrlC:
54			return m, tea.Quit
55		case tea.KeyEsc:
56			// Save and go back to settings
57			signature := m.textarea.Value()
58			go config.SaveSignature(signature)
59			return m, func() tea.Msg { return GoToSettingsMsg{} }
60		}
61	}
62
63	m.textarea, cmd = m.textarea.Update(msg)
64	return m, cmd
65}
66
67// View renders the signature editor screen.
68func (m *SignatureEditor) View() string {
69	title := titleStyle.Render("Email Signature")
70	hint := accountEmailStyle.Render("This signature will be appended to your emails.")
71
72	return lipgloss.JoinVertical(lipgloss.Left,
73		title,
74		hint,
75		"",
76		m.textarea.View(),
77		"",
78		helpStyle.Render("esc: save & back"),
79	)
80}