summaryrefslogtreecommitdiffhomepage
path: root/packages/tui/internal/theme/manager.go
blob: 420b96dea9233128ad3b621fcb64b1aa193ee1a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package theme

import (
	"fmt"
	"image/color"
	"slices"
	"strconv"
	"strings"
	"sync"

	"github.com/alecthomas/chroma/v2/styles"
	"github.com/charmbracelet/lipgloss/v2"
	"github.com/charmbracelet/lipgloss/v2/compat"
	"github.com/charmbracelet/x/ansi"
)

// Manager handles theme registration, selection, and retrieval.
// It maintains a registry of available themes and tracks the currently active theme.
type Manager struct {
	themes               map[string]Theme
	currentName          string
	currentUsesAnsiCache bool // Cache whether current theme uses ANSI colors
	mu                   sync.RWMutex
}

// Global instance of the theme manager
var globalManager = &Manager{
	themes:      make(map[string]Theme),
	currentName: "",
}

// RegisterTheme adds a new theme to the registry.
// If this is the first theme registered, it becomes the default.
func RegisterTheme(name string, theme Theme) {
	globalManager.mu.Lock()
	defer globalManager.mu.Unlock()

	globalManager.themes[name] = theme

	// If this is the first theme, make it the default
	if globalManager.currentName == "" {
		globalManager.currentName = name
		globalManager.currentUsesAnsiCache = themeUsesAnsiColors(theme)
	}
}

// SetTheme changes the active theme to the one with the specified name.
// Returns an error if the theme doesn't exist.
func SetTheme(name string) error {
	globalManager.mu.Lock()
	defer globalManager.mu.Unlock()
	delete(styles.Registry, "charm")

	theme, exists := globalManager.themes[name]
	if !exists {
		return fmt.Errorf("theme '%s' not found", name)
	}

	globalManager.currentName = name
	globalManager.currentUsesAnsiCache = themeUsesAnsiColors(theme)

	return nil
}

// CurrentTheme returns the currently active theme.
// If no theme is set, it returns nil.
func CurrentTheme() Theme {
	globalManager.mu.RLock()
	defer globalManager.mu.RUnlock()

	if globalManager.currentName == "" {
		return nil
	}

	return globalManager.themes[globalManager.currentName]
}

// CurrentThemeName returns the name of the currently active theme.
func CurrentThemeName() string {
	globalManager.mu.RLock()
	defer globalManager.mu.RUnlock()

	return globalManager.currentName
}

// AvailableThemes returns a list of all registered theme names.
func AvailableThemes() []string {
	globalManager.mu.RLock()
	defer globalManager.mu.RUnlock()

	names := make([]string, 0, len(globalManager.themes))
	for name := range globalManager.themes {
		names = append(names, name)
	}
	slices.SortFunc(names, func(a, b string) int {
		if a == "opencode" {
			return -1
		} else if b == "opencode" {
			return 1
		}
		if a == "system" {
			return -1
		} else if b == "system" {
			return 1
		}
		return strings.Compare(a, b)
	})
	return names
}

// GetTheme returns a specific theme by name.
// Returns nil if the theme doesn't exist.
func GetTheme(name string) Theme {
	globalManager.mu.RLock()
	defer globalManager.mu.RUnlock()

	return globalManager.themes[name]
}

// UpdateSystemTheme updates the system theme with terminal background info
func UpdateSystemTheme(terminalBg color.Color, isDark bool) {
	globalManager.mu.Lock()
	defer globalManager.mu.Unlock()

	dynamicTheme := NewSystemTheme(terminalBg, isDark)
	globalManager.themes["system"] = dynamicTheme
	if globalManager.currentName == "system" {
		globalManager.currentUsesAnsiCache = themeUsesAnsiColors(dynamicTheme)
	}
}

// CurrentThemeUsesAnsiColors returns true if the current theme uses ANSI 0-16 colors
func CurrentThemeUsesAnsiColors() bool {
	// globalManager.mu.RLock()
	// defer globalManager.mu.RUnlock()

	return globalManager.currentUsesAnsiCache
}

// isAnsiColor checks if a color represents an ANSI 0-16 color
func isAnsiColor(c color.Color) bool {
	if _, ok := c.(lipgloss.NoColor); ok {
		return false
	}
	if _, ok := c.(ansi.BasicColor); ok {
		return true
	}

	// For other color types, check if they represent ANSI colors
	// by examining their string representation
	if stringer, ok := c.(fmt.Stringer); ok {
		str := stringer.String()
		// Check if it's a numeric ANSI color (0-15)
		if num, err := strconv.Atoi(str); err == nil && num >= 0 && num <= 15 {
			return true
		}
	}

	return false
}

// adaptiveColorUsesAnsi checks if an AdaptiveColor uses ANSI colors
func adaptiveColorUsesAnsi(ac compat.AdaptiveColor) bool {
	if isAnsiColor(ac.Dark) {
		return true
	}
	if isAnsiColor(ac.Light) {
		return true
	}
	return false
}

// themeUsesAnsiColors checks if a theme uses any ANSI 0-16 colors
func themeUsesAnsiColors(theme Theme) bool {
	if theme == nil {
		return false
	}

	return adaptiveColorUsesAnsi(theme.Primary()) ||
		adaptiveColorUsesAnsi(theme.Secondary()) ||
		adaptiveColorUsesAnsi(theme.Accent()) ||
		adaptiveColorUsesAnsi(theme.Error()) ||
		adaptiveColorUsesAnsi(theme.Warning()) ||
		adaptiveColorUsesAnsi(theme.Success()) ||
		adaptiveColorUsesAnsi(theme.Info()) ||
		adaptiveColorUsesAnsi(theme.Text()) ||
		adaptiveColorUsesAnsi(theme.TextMuted()) ||
		adaptiveColorUsesAnsi(theme.Background()) ||
		adaptiveColorUsesAnsi(theme.BackgroundPanel()) ||
		adaptiveColorUsesAnsi(theme.BackgroundElement()) ||
		adaptiveColorUsesAnsi(theme.Border()) ||
		adaptiveColorUsesAnsi(theme.BorderActive()) ||
		adaptiveColorUsesAnsi(theme.BorderSubtle()) ||
		adaptiveColorUsesAnsi(theme.DiffAdded()) ||
		adaptiveColorUsesAnsi(theme.DiffRemoved()) ||
		adaptiveColorUsesAnsi(theme.DiffContext()) ||
		adaptiveColorUsesAnsi(theme.DiffHunkHeader()) ||
		adaptiveColorUsesAnsi(theme.DiffHighlightAdded()) ||
		adaptiveColorUsesAnsi(theme.DiffHighlightRemoved()) ||
		adaptiveColorUsesAnsi(theme.DiffAddedBg()) ||
		adaptiveColorUsesAnsi(theme.DiffRemovedBg()) ||
		adaptiveColorUsesAnsi(theme.DiffContextBg()) ||
		adaptiveColorUsesAnsi(theme.DiffLineNumber()) ||
		adaptiveColorUsesAnsi(theme.DiffAddedLineNumberBg()) ||
		adaptiveColorUsesAnsi(theme.DiffRemovedLineNumberBg()) ||
		adaptiveColorUsesAnsi(theme.MarkdownText()) ||
		adaptiveColorUsesAnsi(theme.MarkdownHeading()) ||
		adaptiveColorUsesAnsi(theme.MarkdownLink()) ||
		adaptiveColorUsesAnsi(theme.MarkdownLinkText()) ||
		adaptiveColorUsesAnsi(theme.MarkdownCode()) ||
		adaptiveColorUsesAnsi(theme.MarkdownBlockQuote()) ||
		adaptiveColorUsesAnsi(theme.MarkdownEmph()) ||
		adaptiveColorUsesAnsi(theme.MarkdownStrong()) ||
		adaptiveColorUsesAnsi(theme.MarkdownHorizontalRule()) ||
		adaptiveColorUsesAnsi(theme.MarkdownListItem()) ||
		adaptiveColorUsesAnsi(theme.MarkdownListEnumeration()) ||
		adaptiveColorUsesAnsi(theme.MarkdownImage()) ||
		adaptiveColorUsesAnsi(theme.MarkdownImageText()) ||
		adaptiveColorUsesAnsi(theme.MarkdownCodeBlock()) ||
		adaptiveColorUsesAnsi(theme.SyntaxComment()) ||
		adaptiveColorUsesAnsi(theme.SyntaxKeyword()) ||
		adaptiveColorUsesAnsi(theme.SyntaxFunction()) ||
		adaptiveColorUsesAnsi(theme.SyntaxVariable()) ||
		adaptiveColorUsesAnsi(theme.SyntaxString()) ||
		adaptiveColorUsesAnsi(theme.SyntaxNumber()) ||
		adaptiveColorUsesAnsi(theme.SyntaxType()) ||
		adaptiveColorUsesAnsi(theme.SyntaxOperator()) ||
		adaptiveColorUsesAnsi(theme.SyntaxPunctuation())
}