-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyles.go
More file actions
290 lines (249 loc) · 6.95 KB
/
Copy pathstyles.go
File metadata and controls
290 lines (249 loc) · 6.95 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package api
import (
"strings"
"sync"
"github.com/flanksource/clicky/api/tailwind"
"github.com/muesli/termenv"
)
// Global cache for ResolveStyles to avoid repeated parsing
var (
styleCache = make(map[string]Class)
styleCacheLock sync.RWMutex
)
func ResolveStyles(styles ...string) Class {
// Create cache key from all style strings
cacheKey := strings.Join(styles, "|")
// Check cache first
styleCacheLock.RLock()
if cached, ok := styleCache[cacheKey]; ok {
styleCacheLock.RUnlock()
return cached
}
styleCacheLock.RUnlock()
var resolved Class
// Process each style string
for _, styleStr := range styles {
if styleStr == "" {
continue
}
// Split into individual classes
classes := strings.Fields(styleStr)
for _, class := range classes {
// Parse colors
if strings.HasPrefix(class, "text-") && !tailwind.IsTextUtilityClass(class) {
color := tailwind.Color(class)
if color != "" {
resolved.Foreground = &Color{Hex: color}
}
} else if strings.HasPrefix(class, "bg-") {
color := tailwind.Color(class)
if color != "" {
resolved.Background = &Color{Hex: color}
}
}
// Parse font properties
parsedStyle := tailwind.ParseStyle(class)
// Initialize Font if needed
if resolved.Font == nil {
resolved.Font = &Font{}
}
// Apply font family
if strings.HasPrefix(class, "font-family-") {
fontName := strings.TrimPrefix(class, "font-family-")
switch strings.ToLower(fontName) {
case "arial":
resolved.Font.Name = "Arial"
case "times":
resolved.Font.Name = "Times"
case "helvetica":
resolved.Font.Name = "Helvetica"
case "courier":
resolved.Font.Name = "Courier"
case "georgia":
resolved.Font.Name = "Georgia"
case "verdana":
resolved.Font.Name = "Verdana"
default:
// Allow custom font names (case-sensitive for exact match)
resolved.Font.Name = fontName
}
}
// Apply font weight
switch class {
case "bold", "font-bold", "font-semibold", "font-medium":
resolved.Font.Bold = true
case "font-normal":
resolved.Font.Bold = false
}
// Apply font style
switch class {
case "italic", "font-italic":
resolved.Font.Italic = true
case "not-italic":
resolved.Font.Italic = false
}
// Apply text decoration
switch class {
case "underline":
resolved.Font.Underline = true
case "no-underline":
resolved.Font.Underline = false
}
if class == "line-through" || class == "strikethrough" || class == "text-strikethrough" {
resolved.Font.Strikethrough = true
}
// Apply faint/opacity
switch class {
case "font-light", "font-thin", "font-extralight", "opacity-50", "opacity-75", "opacity-25":
resolved.Font.Faint = true
case "opacity-100":
resolved.Font.Faint = false
}
// Parse font size
if fontSize := tailwind.ParseFontSize(class); fontSize > 0 {
resolved.Font.Size = fontSize
}
// Parse padding
top, right, bottom, left := tailwind.ParsePadding(class)
if top != nil || right != nil || bottom != nil || left != nil {
if resolved.Padding == nil {
resolved.Padding = &Padding{}
}
// Apply non-nil values, converting to Point type
if top != nil {
resolved.Padding.Top = NewPoint(*top)
}
if right != nil {
resolved.Padding.Right = NewPoint(*right)
}
if bottom != nil {
resolved.Padding.Bottom = NewPoint(*bottom)
}
if left != nil {
resolved.Padding.Left = NewPoint(*left)
}
}
// Apply colors from parsed style (as fallback)
if parsedStyle.Foreground != "" && resolved.Foreground == nil {
resolved.Foreground = &Color{Hex: parsedStyle.Foreground}
}
if parsedStyle.Background != "" && resolved.Background == nil {
resolved.Background = &Color{Hex: parsedStyle.Background}
}
}
}
// Store in cache before returning
styleCacheLock.Lock()
styleCache[cacheKey] = resolved
styleCacheLock.Unlock()
return resolved
}
// ApplyTailwindStyle processes Tailwind CSS classes and applies text transformations,
// returning both the transformed text and parsed style information.
func ApplyTailwindStyle(text, styleStr string) (string, TailwindStyle) {
transformedText, twStyle := tailwind.ApplyStyle(text, styleStr)
// Convert to our TailwindStyle struct
style := TailwindStyle{
Foreground: twStyle.Foreground,
Background: twStyle.Background,
Bold: twStyle.Bold,
Faint: twStyle.Faint,
Italic: twStyle.Italic,
Underline: twStyle.Underline,
Strikethrough: twStyle.Strikethrough,
TextTransform: twStyle.TextTransform,
}
return transformedText, style
}
func classToTailwindStyle(class Class) TailwindStyle {
style := TailwindStyle{}
// Apply colors
if class.Foreground != nil {
style.Foreground = class.Foreground.Hex
}
if class.Background != nil {
style.Background = class.Background.Hex
}
// Apply font properties
if class.Font != nil {
style.Bold = class.Font.Bold
style.Faint = class.Font.Faint
style.Italic = class.Font.Italic
style.Underline = class.Font.Underline
style.Strikethrough = class.Font.Strikethrough
}
return style
}
// TailwindStyle contains parsed CSS styling information extracted from Tailwind classes.
type TailwindStyle struct {
Foreground string
Background string
Font Font
Bold bool
Faint bool
Italic bool
Underline bool
Strikethrough bool
TextTransform string
}
func formatANSI(text string, style TailwindStyle) string {
if text == "" {
return ""
}
output := termenv.NewOutput(termenv.DefaultOutput().Writer(), termenv.WithProfile(termenv.ANSI))
termStyle := output.String(text)
// Apply text decorations
if style.Bold {
termStyle = termStyle.Bold()
}
if style.Faint {
termStyle = termStyle.Faint()
}
if style.Italic {
termStyle = termStyle.Italic()
}
if style.Underline {
termStyle = termStyle.Underline()
}
// Apply foreground color using termenv
if style.Foreground != "" {
if color := hexToTermenvColor(style.Foreground); color != nil {
termStyle = termStyle.Foreground(color)
}
}
// Apply background color using termenv
if style.Background != "" {
if color := hexToTermenvColor(style.Background); color != nil {
termStyle = termStyle.Background(color)
}
}
// Handle strikethrough manually since termenv doesn't support it
result := termStyle.String()
if style.Strikethrough {
// Remove any existing reset codes and add strikethrough
if strings.HasSuffix(result, "\x1b[0m") {
result = strings.TrimSuffix(result, "\x1b[0m")
result = "\x1b[9m" + result + "\x1b[0m"
} else {
result = "\x1b[9m" + result + "\x1b[29m"
}
}
return result
}
func hexToTermenvColor(hex string) termenv.Color {
if hex == "" {
return nil
}
// Handle special colors
switch hex {
case "transparent":
return nil
case "currentColor":
return termenv.ANSIColor(termenv.ANSIBrightWhite)
}
// Convert hex to termenv color
if strings.HasPrefix(hex, "#") {
return termenv.RGBColor(hex)
}
return nil
}