settings_general.go

  1package tui
  2
  3import (
  4	"fmt"
  5	"strings"
  6
  7	tea "charm.land/bubbletea/v2"
  8	"github.com/floatpane/matcha/config"
  9	"github.com/floatpane/matcha/i18n"
 10)
 11
 12func (m *Settings) updateGeneral(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
 13	switch msg.String() {
 14	case "up", "k":
 15		if m.generalCursor > 0 {
 16			m.generalCursor--
 17		}
 18	case "down", "j":
 19		if m.generalCursor < 5 {
 20			m.generalCursor++
 21		}
 22	case "enter", "space", "right", "l":
 23		switch m.generalCursor {
 24		case 0: // Image Display
 25			m.cfg.DisableImages = !m.cfg.DisableImages
 26			_ = config.SaveConfig(m.cfg)
 27		case 1: // Contextual Tips
 28			m.cfg.HideTips = !m.cfg.HideTips
 29			_ = config.SaveConfig(m.cfg)
 30		case 2: // Desktop Notifications
 31			m.cfg.DisableNotifications = !m.cfg.DisableNotifications
 32			_ = config.SaveConfig(m.cfg)
 33		case 3: // Date Format
 34			switch m.cfg.DateFormat {
 35			case config.DateFormatEU:
 36				m.cfg.DateFormat = config.DateFormatUS
 37			case config.DateFormatUS:
 38				m.cfg.DateFormat = config.DateFormatISO
 39			default: // or ISO
 40				m.cfg.DateFormat = config.DateFormatEU
 41			}
 42			_ = config.SaveConfig(m.cfg)
 43		case 4: // Language
 44			// Cycle through available languages
 45			langs := i18n.LanguageCodes()
 46			currentLang := m.cfg.GetLanguage()
 47			currentIdx := -1
 48			for i, lang := range langs {
 49				if lang == currentLang {
 50					currentIdx = i
 51					break
 52				}
 53			}
 54			nextIdx := (currentIdx + 1) % len(langs)
 55			m.cfg.Language = langs[nextIdx]
 56			_ = config.SaveConfig(m.cfg)
 57		case 5: // Edit Signature
 58			if msg.String() == "enter" || msg.String() == "right" || msg.String() == "l" {
 59				return m, func() tea.Msg { return GoToSignatureEditorMsg{} }
 60			}
 61		}
 62	}
 63	return m, nil
 64}
 65
 66func (m *Settings) viewGeneral() string {
 67	var b strings.Builder
 68
 69	b.WriteString(titleStyle.Render("General Settings") + "\n\n")
 70
 71	options := []struct {
 72		labelKey string
 73		value    string
 74		tip      string
 75	}{
 76		{"settings_general.disable_images", onOff(m.cfg.DisableImages), "Prevent images from loading automatically in emails."},
 77		{"settings_general.hide_tips", onOff(m.cfg.HideTips), "Hide helpful hints displayed at the bottom of the screen."},
 78		{"settings_general.disable_notifications", onOff(m.cfg.DisableNotifications), "Turn off desktop notifications for new mail."},
 79		{"settings_general.date_format", getDateFormatLabel(m.cfg.DateFormat), "Change how dates and times are displayed."},
 80		{"settings_general.language", getLanguageLabel(m.cfg.GetLanguage()), "Change the interface language. Restart required."},
 81		{"settings_general.signature", getSignatureStatus(), "Configure the signature appended to your outgoing emails."},
 82	}
 83
 84	for i, opt := range options {
 85		cursor := "  "
 86		style := accountItemStyle
 87		if m.generalCursor == i {
 88			cursor = "> "
 89			style = selectedAccountItemStyle
 90		}
 91
 92		label := t(opt.labelKey)
 93		text := fmt.Sprintf("%s: %s", label, opt.value)
 94		if opt.labelKey == "settings_general.signature" {
 95			text = fmt.Sprintf("%s (%s)", label, opt.value)
 96		}
 97
 98		b.WriteString(style.Render(cursor+text) + "\n")
 99	}
100
101	b.WriteString("\n\n")
102
103	if !m.cfg.HideTips && m.generalCursor < len(options) {
104		b.WriteString(TipStyle.Render("Tip: " + options[m.generalCursor].tip))
105	}
106
107	return b.String()
108}
109
110func onOff(b bool) string {
111	if b {
112		return t("settings_general.on")
113	}
114	return t("settings_general.off")
115}
116
117func getDateFormatLabel(f string) string {
118	if f == "" {
119		f = config.DateFormatEU
120	}
121	switch f {
122	case config.DateFormatUS:
123		return "US (MM/DD/YYYY hh:MM AM)"
124	case config.DateFormatISO:
125		return "ISO (YYYY-MM-DD HH:MM)"
126	default:
127		return "EU (DD/MM/YYYY HH:MM)"
128	}
129}
130
131func getSignatureStatus() string {
132	if config.HasSignature() {
133		return t("settings_general.signature_configured")
134	}
135	return t("settings_general.signature_not_configured")
136}
137
138func getLanguageLabel(langCode string) string {
139	if locale, ok := i18n.GetLanguage(langCode); ok {
140		return fmt.Sprintf("%s (%s)", locale.NativeName, locale.Code)
141	}
142	return langCode
143}