gitstack

grindlemire/go-tui code browser

20.6 KB Go 620 lines 2026-06-03 ยท d15bb9f raw
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package tuigen

import (
	"fmt"
	"strconv"
	"strings"
)

// PaddingAccumulator tracks individual padding values for accumulation
type PaddingAccumulator struct {
	Top, Right, Bottom, Left             int
	HasTop, HasRight, HasBottom, HasLeft bool
}

// Merge combines an individual side class into the accumulator
func (p *PaddingAccumulator) Merge(side string, value int) {
	switch side {
	case "top":
		p.Top = value
		p.HasTop = true
	case "right":
		p.Right = value
		p.HasRight = true
	case "bottom":
		p.Bottom = value
		p.HasBottom = true
	case "left":
		p.Left = value
		p.HasLeft = true
	case "x": // horizontal (left and right)
		p.Left = value
		p.Right = value
		p.HasLeft = true
		p.HasRight = true
	case "y": // vertical (top and bottom)
		p.Top = value
		p.Bottom = value
		p.HasTop = true
		p.HasBottom = true
	}
}

// HasAny returns true if any side has been set
func (p *PaddingAccumulator) HasAny() bool {
	return p.HasTop || p.HasRight || p.HasBottom || p.HasLeft
}

// ToOption generates WithPaddingTRBL() if any sides are set
func (p *PaddingAccumulator) ToOption() string {
	if !p.HasAny() {
		return ""
	}
	return "tui.WithPaddingTRBL(" + strconv.Itoa(p.Top) + ", " + strconv.Itoa(p.Right) + ", " + strconv.Itoa(p.Bottom) + ", " + strconv.Itoa(p.Left) + ")"
}

// MarginAccumulator tracks individual margin values for accumulation
type MarginAccumulator struct {
	Top, Right, Bottom, Left             int
	HasTop, HasRight, HasBottom, HasLeft bool
}

// Merge combines an individual side class into the accumulator
func (m *MarginAccumulator) Merge(side string, value int) {
	switch side {
	case "top":
		m.Top = value
		m.HasTop = true
	case "right":
		m.Right = value
		m.HasRight = true
	case "bottom":
		m.Bottom = value
		m.HasBottom = true
	case "left":
		m.Left = value
		m.HasLeft = true
	case "x": // horizontal (left and right)
		m.Left = value
		m.Right = value
		m.HasLeft = true
		m.HasRight = true
	case "y": // vertical (top and bottom)
		m.Top = value
		m.Bottom = value
		m.HasTop = true
		m.HasBottom = true
	}
}

// HasAny returns true if any side has been set
func (m *MarginAccumulator) HasAny() bool {
	return m.HasTop || m.HasRight || m.HasBottom || m.HasLeft
}

// ToOption generates WithMarginTRBL() if any sides are set
func (m *MarginAccumulator) ToOption() string {
	if !m.HasAny() {
		return ""
	}
	return "tui.WithMarginTRBL(" + strconv.Itoa(m.Top) + ", " + strconv.Itoa(m.Right) + ", " + strconv.Itoa(m.Bottom) + ", " + strconv.Itoa(m.Left) + ")"
}

// IndividualSpacingResult indicates an individual padding/margin class was parsed
type IndividualSpacingResult struct {
	IsPadding bool   // true for padding, false for margin
	Side      string // "top", "right", "bottom", "left", "x", "y"
	Value     int
}

// parseIndividualSpacing checks if a class is an individual padding/margin class
// Returns the result and true if it matched, or zero value and false if not
func parseIndividualSpacing(class string) (IndividualSpacingResult, bool) {
	// Individual padding
	if matches := ptPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: true, Side: "top", Value: n}, true
	}
	if matches := prPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: true, Side: "right", Value: n}, true
	}
	if matches := pbPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: true, Side: "bottom", Value: n}, true
	}
	if matches := plPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: true, Side: "left", Value: n}, true
	}
	if matches := paddingXPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: true, Side: "x", Value: n}, true
	}
	if matches := paddingYPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: true, Side: "y", Value: n}, true
	}

	// Individual margin
	if matches := mtPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: false, Side: "top", Value: n}, true
	}
	if matches := mrPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: false, Side: "right", Value: n}, true
	}
	if matches := mbPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: false, Side: "bottom", Value: n}, true
	}
	if matches := mlPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: false, Side: "left", Value: n}, true
	}
	if matches := mxPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: false, Side: "x", Value: n}, true
	}
	if matches := myPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return IndividualSpacingResult{IsPadding: false, Side: "y", Value: n}, true
	}

	return IndividualSpacingResult{}, false
}

// ParseTailwindClass parses a single Tailwind class and returns its mapping
func ParseTailwindClass(class string) (TailwindMapping, bool) {
	class = strings.TrimSpace(class)
	if class == "" {
		return TailwindMapping{}, false
	}

	// Check static mappings first
	if mapping, ok := tailwindClasses[class]; ok {
		return mapping, true
	}

	// Check dynamic patterns
	if matches := gapPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithGap(" + strconv.Itoa(n) + ")"}, true
	}

	if matches := paddingPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithPadding(" + strconv.Itoa(n) + ")"}, true
	}

	if matches := paddingXPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithPaddingTRBL(0, " + strconv.Itoa(n) + ", 0, " + strconv.Itoa(n) + ")"}, true
	}

	if matches := paddingYPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithPaddingTRBL(" + strconv.Itoa(n) + ", 0, " + strconv.Itoa(n) + ", 0)"}, true
	}

	if matches := marginPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithMargin(" + strconv.Itoa(n) + ")"}, true
	}

	if matches := widthPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithWidth(" + strconv.Itoa(n) + ")"}, true
	}

	if matches := heightPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithHeight(" + strconv.Itoa(n) + ")"}, true
	}

	if matches := minWidthPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithMinWidth(" + strconv.Itoa(n) + ")"}, true
	}

	if matches := maxWidthPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithMaxWidth(" + strconv.Itoa(n) + ")"}, true
	}

	if matches := minHeightPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithMinHeight(" + strconv.Itoa(n) + ")"}, true
	}

	if matches := maxHeightPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithMaxHeight(" + strconv.Itoa(n) + ")"}, true
	}

	// Width fraction patterns (w-1/2, w-2/3, etc.)
	if matches := widthFractionPattern.FindStringSubmatch(class); matches != nil {
		numerator, _ := strconv.Atoi(matches[1])
		denominator, _ := strconv.Atoi(matches[2])
		if denominator != 0 {
			percent := float64(numerator) / float64(denominator) * 100
			return TailwindMapping{Option: "tui.WithWidthPercent(" + strconv.FormatFloat(percent, 'f', 2, 64) + ")"}, true
		}
	}

	// Height fraction patterns (h-1/2, h-2/3, etc.)
	if matches := heightFractionPattern.FindStringSubmatch(class); matches != nil {
		numerator, _ := strconv.Atoi(matches[1])
		denominator, _ := strconv.Atoi(matches[2])
		if denominator != 0 {
			percent := float64(numerator) / float64(denominator) * 100
			return TailwindMapping{Option: "tui.WithHeightPercent(" + strconv.FormatFloat(percent, 'f', 2, 64) + ")"}, true
		}
	}

	// Width keyword patterns (w-full, w-auto)
	if matches := widthKeywordPattern.FindStringSubmatch(class); matches != nil {
		keyword := matches[1]
		switch keyword {
		case "full":
			return TailwindMapping{Option: "tui.WithWidthPercent(100.00)"}, true
		case "auto":
			return TailwindMapping{Option: "tui.WithWidthAuto()"}, true
		}
	}

	// Height keyword patterns (h-full, h-auto)
	if matches := heightKeywordPattern.FindStringSubmatch(class); matches != nil {
		keyword := matches[1]
		switch keyword {
		case "full":
			return TailwindMapping{Option: "tui.WithHeightPercent(100.00)"}, true
		case "auto":
			return TailwindMapping{Option: "tui.WithHeightAuto()"}, true
		}
	}

	// Flex grow pattern (flex-grow-N)
	if matches := flexGrowPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithFlexGrow(" + strconv.Itoa(n) + ")"}, true
	}

	// Flex shrink pattern (flex-shrink-N)
	if matches := flexShrinkPattern.FindStringSubmatch(class); matches != nil {
		n, _ := strconv.Atoi(matches[1])
		return TailwindMapping{Option: "tui.WithFlexShrink(" + strconv.Itoa(n) + ")"}, true
	}

	// Arbitrary hex color patterns (must be checked before gradients)
	if matches := textHexPattern.FindStringSubmatch(class); matches != nil {
		r, g, b, ok := parseHexToRGB(matches[1])
		if ok {
			return TailwindMapping{IsTextStyle: true, TextMethod: fmt.Sprintf("Foreground(tui.RGBColor(%d, %d, %d))", r, g, b), NeedsImport: "tui"}, true
		}
	}

	if matches := bgHexPattern.FindStringSubmatch(class); matches != nil {
		r, g, b, ok := parseHexToRGB(matches[1])
		if ok {
			return TailwindMapping{Option: fmt.Sprintf("tui.WithBackground(tui.NewStyle().Background(tui.RGBColor(%d, %d, %d)))", r, g, b), NeedsImport: "tui"}, true
		}
	}

	if matches := borderHexPattern.FindStringSubmatch(class); matches != nil {
		r, g, b, ok := parseHexToRGB(matches[1])
		if ok {
			return TailwindMapping{Option: fmt.Sprintf("tui.WithBorderStyle(tui.NewStyle().Foreground(tui.RGBColor(%d, %d, %d)))", r, g, b), NeedsImport: "tui"}, true
		}
	}

	if matches := scrollbarHexPattern.FindStringSubmatch(class); matches != nil {
		r, g, b, ok := parseHexToRGB(matches[1])
		if ok {
			return TailwindMapping{Option: fmt.Sprintf("tui.WithScrollbarStyle(tui.NewStyle().Foreground(tui.RGBColor(%d, %d, %d)))", r, g, b), NeedsImport: "tui"}, true
		}
	}

	if matches := scrollbarThumbHexPattern.FindStringSubmatch(class); matches != nil {
		r, g, b, ok := parseHexToRGB(matches[1])
		if ok {
			return TailwindMapping{Option: fmt.Sprintf("tui.WithScrollbarThumbStyle(tui.NewStyle().Foreground(tui.RGBColor(%d, %d, %d)))", r, g, b), NeedsImport: "tui"}, true
		}
	}

	// Gradient patterns
	if matches := textGradientPattern.FindStringSubmatch(class); matches != nil {
		var startColorName, endColorName string
		direction := "tui.GradientHorizontal"

		// Check if class ends with a direction suffix
		if strings.HasSuffix(class, "-v") || strings.HasSuffix(class, "-dd") || strings.HasSuffix(class, "-du") || strings.HasSuffix(class, "-h") {
			// Has direction suffix - use first alternative
			startColorName = matches[1]
			endColorName = matches[2]
			if matches[3] != "" {
				switch matches[3] {
				case "v":
					direction = "tui.GradientVertical"
				case "dd":
					direction = "tui.GradientDiagonalDown"
				case "du":
					direction = "tui.GradientDiagonalUp"
				}
			}
		} else {
			// No direction suffix - parse manually by matching known color names from the end
			prefix := "text-gradient-"
			rest := strings.TrimPrefix(class, prefix)
			// Try to match known color names from the end (check longer names first to avoid partial matches)
			colorNames := []string{"bright-red", "bright-green", "bright-blue", "bright-cyan", "bright-magenta", "bright-yellow", "bright-white", "bright-black", "red", "green", "blue", "cyan", "magenta", "yellow", "white", "black"}
			for _, colorName := range colorNames {
				suffix := "-" + colorName
				if strings.HasSuffix(rest, suffix) {
					endColorName = colorName
					startColorName = strings.TrimSuffix(rest, suffix)
					break
				}
			}
			if endColorName == "" {
				// Fallback: use regex matches if available
				if matches[4] != "" && matches[5] != "" {
					startColorName = matches[4]
					endColorName = matches[5]
				} else {
					// Last resort: split on last hyphen
					lastHyphen := strings.LastIndex(rest, "-")
					if lastHyphen > 0 {
						startColorName = rest[:lastHyphen]
						endColorName = rest[lastHyphen+1:]
					}
				}
			}
		}

		startColor := colorNameToColor(startColorName)
		endColor := colorNameToColor(endColorName)
		option := "tui.WithTextGradient(tui.NewGradient(" + startColor + ", " + endColor + ").WithDirection(" + direction + "))"
		return TailwindMapping{Option: option, NeedsImport: "tui"}, true
	}

	if matches := bgGradientPattern.FindStringSubmatch(class); matches != nil {
		var startColorName, endColorName string
		direction := "tui.GradientHorizontal"

		// Check if class ends with a direction suffix
		if strings.HasSuffix(class, "-v") || strings.HasSuffix(class, "-dd") || strings.HasSuffix(class, "-du") || strings.HasSuffix(class, "-h") {
			// Has direction suffix - use first alternative
			startColorName = matches[1]
			endColorName = matches[2]
			if matches[3] != "" {
				switch matches[3] {
				case "v":
					direction = "tui.GradientVertical"
				case "dd":
					direction = "tui.GradientDiagonalDown"
				case "du":
					direction = "tui.GradientDiagonalUp"
				}
			}
		} else {
			// No direction suffix - parse manually by matching known color names from the end
			prefix := "bg-gradient-"
			rest := strings.TrimPrefix(class, prefix)
			colorNames := []string{"bright-red", "bright-green", "bright-blue", "bright-cyan", "bright-magenta", "bright-yellow", "bright-white", "bright-black", "red", "green", "blue", "cyan", "magenta", "yellow", "white", "black"}
			for _, colorName := range colorNames {
				if strings.HasSuffix(rest, "-"+colorName) {
					endColorName = colorName
					startColorName = strings.TrimSuffix(rest, "-"+colorName)
					break
				}
			}
			if endColorName == "" {
				lastHyphen := strings.LastIndex(rest, "-")
				if lastHyphen > 0 {
					startColorName = rest[:lastHyphen]
					endColorName = rest[lastHyphen+1:]
				}
			}
		}

		startColor := colorNameToColor(startColorName)
		endColor := colorNameToColor(endColorName)
		option := "tui.WithBackgroundGradient(tui.NewGradient(" + startColor + ", " + endColor + ").WithDirection(" + direction + "))"
		return TailwindMapping{Option: option, NeedsImport: "tui"}, true
	}

	if matches := borderGradientPattern.FindStringSubmatch(class); matches != nil {
		var startColorName, endColorName string
		direction := "tui.GradientHorizontal"

		// Check if class ends with a direction suffix
		if strings.HasSuffix(class, "-v") || strings.HasSuffix(class, "-dd") || strings.HasSuffix(class, "-du") || strings.HasSuffix(class, "-h") {
			// Has direction suffix - use first alternative
			startColorName = matches[1]
			endColorName = matches[2]
			if matches[3] != "" {
				switch matches[3] {
				case "v":
					direction = "tui.GradientVertical"
				case "dd":
					direction = "tui.GradientDiagonalDown"
				case "du":
					direction = "tui.GradientDiagonalUp"
				}
			}
		} else {
			// No direction suffix - parse manually by matching known color names from the end
			prefix := "border-gradient-"
			rest := strings.TrimPrefix(class, prefix)
			colorNames := []string{"bright-red", "bright-green", "bright-blue", "bright-cyan", "bright-magenta", "bright-yellow", "bright-white", "bright-black", "red", "green", "blue", "cyan", "magenta", "yellow", "white", "black"}
			for _, colorName := range colorNames {
				if strings.HasSuffix(rest, "-"+colorName) {
					endColorName = colorName
					startColorName = strings.TrimSuffix(rest, "-"+colorName)
					break
				}
			}
			if endColorName == "" {
				lastHyphen := strings.LastIndex(rest, "-")
				if lastHyphen > 0 {
					startColorName = rest[:lastHyphen]
					endColorName = rest[lastHyphen+1:]
				}
			}
		}

		startColor := colorNameToColor(startColorName)
		endColor := colorNameToColor(endColorName)
		option := "tui.WithBorderGradient(tui.NewGradient(" + startColor + ", " + endColor + ").WithDirection(" + direction + "))"
		return TailwindMapping{Option: option, NeedsImport: "tui"}, true
	}

	// Individual padding/margin classes - these are valid but handled separately in ParseTailwindClasses
	if _, ok := parseIndividualSpacing(class); ok {
		// Return a marker mapping - actual handling is done in ParseTailwindClasses
		return TailwindMapping{}, true
	}

	// Unknown class - silently ignore
	return TailwindMapping{}, false
}

// colorNameToColor maps a color name string to the corresponding tui.Color constant.
func colorNameToColor(name string) string {
	switch name {
	case "red":
		return "tui.Red"
	case "green":
		return "tui.Green"
	case "blue":
		return "tui.Blue"
	case "cyan":
		return "tui.Cyan"
	case "magenta":
		return "tui.Magenta"
	case "yellow":
		return "tui.Yellow"
	case "white":
		return "tui.White"
	case "black":
		return "tui.Black"
	case "bright-red":
		return "tui.BrightRed"
	case "bright-green":
		return "tui.BrightGreen"
	case "bright-blue":
		return "tui.BrightBlue"
	case "bright-cyan":
		return "tui.BrightCyan"
	case "bright-magenta":
		return "tui.BrightMagenta"
	case "bright-yellow":
		return "tui.BrightYellow"
	case "bright-white":
		return "tui.BrightWhite"
	case "bright-black":
		return "tui.BrightBlack"
	default:
		// Default to black if unknown
		return "tui.Black"
	}
}

// TailwindParseResult contains the parsed results from a class string
type TailwindParseResult struct {
	Options      []string        // Direct element options
	TextMethods  []string        // Text style methods to chain
	NeedsImports map[string]bool // Imports needed
}

// ParseTailwindClasses parses a full class attribute string
func ParseTailwindClasses(classes string) TailwindParseResult {
	result := TailwindParseResult{
		NeedsImports: make(map[string]bool),
	}

	// Accumulators for individual padding/margin classes
	var paddingAcc PaddingAccumulator
	var marginAcc MarginAccumulator

	for class := range strings.FieldsSeq(classes) {
		// First, check if it's an individual padding/margin class
		if spacing, ok := parseIndividualSpacing(class); ok {
			if spacing.IsPadding {
				paddingAcc.Merge(spacing.Side, spacing.Value)
			} else {
				marginAcc.Merge(spacing.Side, spacing.Value)
			}
			continue
		}

		mapping, ok := ParseTailwindClass(class)
		if !ok {
			continue
		}

		if mapping.IsTextStyle {
			result.TextMethods = append(result.TextMethods, mapping.TextMethod)
		} else if mapping.Option != "" {
			result.Options = append(result.Options, mapping.Option)
		}

		if mapping.NeedsImport != "" {
			result.NeedsImports[mapping.NeedsImport] = true
		}
	}

	// Add accumulated padding if any sides were set
	if paddingOpt := paddingAcc.ToOption(); paddingOpt != "" {
		result.Options = append(result.Options, paddingOpt)
	}

	// Add accumulated margin if any sides were set
	if marginOpt := marginAcc.ToOption(); marginOpt != "" {
		result.Options = append(result.Options, marginOpt)
	}

	return result
}

// parseHexToRGB parses a 3 or 6 digit hex color string to RGB components.
func parseHexToRGB(hex string) (r, g, b uint8, ok bool) {
	if len(hex) == 3 {
		// Expand shorthand: "abc" -> "aabbcc"
		hex = string([]byte{hex[0], hex[0], hex[1], hex[1], hex[2], hex[2]})
	}
	if len(hex) != 6 {
		return 0, 0, 0, false
	}
	rv, err := strconv.ParseUint(hex[0:2], 16, 8)
	if err != nil {
		return 0, 0, 0, false
	}
	gv, err := strconv.ParseUint(hex[2:4], 16, 8)
	if err != nil {
		return 0, 0, 0, false
	}
	bv, err := strconv.ParseUint(hex[4:6], 16, 8)
	if err != nil {
		return 0, 0, 0, false
	}
	return uint8(rv), uint8(gv), uint8(bv), true
}

// BuildTextStyleOption builds the combined text style option from accumulated methods
func BuildTextStyleOption(methods []string) string {
	if len(methods) == 0 {
		return ""
	}

	var builder strings.Builder
	builder.WriteString("tui.WithTextStyle(tui.NewStyle()")
	for _, method := range methods {
		builder.WriteString(".")
		builder.WriteString(method)
	}
	builder.WriteString(")")
	return builder.String()
}