-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyles.go
More file actions
181 lines (169 loc) · 5.02 KB
/
styles.go
File metadata and controls
181 lines (169 loc) · 5.02 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
package termwind
import (
"strconv"
"strings"
"github.com/charmbracelet/lipgloss"
)
func ParseClasses(classes []string) Style {
var s Style
for _, class := range classes {
class = strings.TrimSpace(class)
if class == "" {
continue
}
if strings.HasPrefix(class, "ml-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "ml-")); err == nil {
s.MarginLeft = n
}
} else if strings.HasPrefix(class, "mr-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "mr-")); err == nil {
s.MarginRight = n
}
} else if strings.HasPrefix(class, "mt-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "mt-")); err == nil {
s.MarginTop = n
}
} else if strings.HasPrefix(class, "mb-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "mb-")); err == nil {
s.MarginBottom = n
}
} else if strings.HasPrefix(class, "pl-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "pl-")); err == nil {
s.PaddingLeft = n
}
} else if strings.HasPrefix(class, "pr-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "pr-")); err == nil {
s.PaddingRight = n
}
} else if strings.HasPrefix(class, "pt-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "pt-")); err == nil {
s.PaddingTop = n
}
} else if strings.HasPrefix(class, "pb-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "pb-")); err == nil {
s.PaddingBottom = n
}
} else if strings.HasPrefix(class, "px-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "px-")); err == nil {
s.PaddingLeft = n
s.PaddingRight = n
}
} else if strings.HasPrefix(class, "py-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "py-")); err == nil {
s.PaddingTop = n
s.PaddingBottom = n
}
} else if strings.HasPrefix(class, "mx-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "mx-")); err == nil {
s.MarginLeft = n
s.MarginRight = n
}
} else if strings.HasPrefix(class, "my-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "my-")); err == nil {
s.MarginTop = n
s.MarginBottom = n
}
} else if strings.HasPrefix(class, "text-") {
color := strings.TrimPrefix(class, "text-")
if strings.HasPrefix(color, "#") {
s.ForegroundColor = color
} else if code, ok := colors[color]; ok {
s.ForegroundColor = code
}
} else if strings.HasPrefix(class, "bg-") {
color := strings.TrimPrefix(class, "bg-")
if strings.HasPrefix(color, "#") {
s.BackgroundColor = color
} else if code, ok := colors[color]; ok {
s.BackgroundColor = code
}
} else if class == "font-bold" {
s.Bold = true
} else if class == "font-italic" {
s.Italic = true
} else if class == "underline" {
s.Underline = true
} else if class == "line-through" {
s.Strikethrough = true
} else if class == "uppercase" {
s.TextTransform = "uppercase"
} else if class == "lowercase" {
s.TextTransform = "lowercase"
} else if class == "capitalize" {
s.TextTransform = "capitalize"
} else if class == "snakecase" {
s.TextTransform = "snakecase"
} else if class == "truncate" {
s.Truncate = true
} else if strings.HasPrefix(class, "max-w-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "max-w-")); err == nil {
s.MaxWidth = n
}
} else if strings.HasPrefix(class, "min-w-") {
if n, err := strconv.Atoi(strings.TrimPrefix(class, "min-w-")); err == nil {
s.MinWidth = n
}
}
}
return s
}
func buildStyle(el *Element, termWidth int) lipgloss.Style {
if el == nil {
return lipgloss.NewStyle()
}
ls := lipgloss.NewStyle().
Bold(el.Style.Bold).
Italic(el.Style.Italic).
Underline(el.Style.Underline).
Strikethrough(el.Style.Strikethrough)
// Padding
if el.Style.PaddingLeft > 0 {
ls = ls.PaddingLeft(el.Style.PaddingLeft)
}
if el.Style.PaddingRight > 0 {
ls = ls.PaddingRight(el.Style.PaddingRight)
}
if el.Style.PaddingTop > 0 {
ls = ls.PaddingTop(el.Style.PaddingTop)
}
if el.Style.PaddingBottom > 0 {
ls = ls.PaddingBottom(el.Style.PaddingBottom)
}
// Margin — horizontal margins apply to all elements,
// vertical margins are block-level only.
if el.Style.MarginLeft > 0 {
ls = ls.MarginLeft(el.Style.MarginLeft)
}
if el.Style.MarginRight > 0 {
ls = ls.MarginRight(el.Style.MarginRight)
}
if !el.Tag.IsInline() {
if el.Style.MarginTop > 0 {
ls = ls.MarginTop(el.Style.MarginTop)
}
if el.Style.MarginBottom > 0 {
ls = ls.MarginBottom(el.Style.MarginBottom)
}
}
// Colors
if el.Style.ForegroundColor != "" {
ls = ls.Foreground(lipgloss.Color(el.Style.ForegroundColor))
}
if el.Style.BackgroundColor != "" {
ls = ls.Background(lipgloss.Color(el.Style.BackgroundColor))
}
// Width
// Note: Lipgloss doesn't support MinWidth, Width sets a fixed width, not a minimum.
// This means content wider than MinWidth will be truncated unexpectedly.
if el.Style.MinWidth > 0 {
ls = ls.Width(el.Style.MinWidth)
}
if el.Style.Truncate {
maxWidth := el.Style.MaxWidth
if maxWidth <= 0 {
maxWidth = termWidth
}
ls = ls.MaxWidth(maxWidth)
}
return ls
}