-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.go
More file actions
220 lines (195 loc) · 5.37 KB
/
component.go
File metadata and controls
220 lines (195 loc) · 5.37 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
package mailgen
import (
"bytes"
"fmt"
htmltemplate "html/template"
"strconv"
"strings"
"unicode"
)
// Component represents a part of the email message, such as a button, line, or table.
type Component interface {
// HTML generates the HTML representation of the component using the provided template.
HTML(tmpl *htmltemplate.Template) (string, error)
// PlainText generates the plain text representation of the component.
PlainText() (string, error)
}
var _ Component = &Table{}
var _ Component = &Action{}
var _ Component = &Line{}
// Action represents a button or link in the email.
type Action struct {
// Text is the text displayed on the button.
Text string
// Link is the URL the button points to.
Link string
// Color is hex color code for the button, e.g. "#3869D4".
Color string
// NoFallback if true, the action will not have a fallback text.
NoFallback bool
FallbackText string
}
// Line represents a simple text line in the email.
type Line struct {
Text string
}
// Table represents a structured table in the email.
// It contains data entries and column definitions.
//
// Example usage:
//
// table := mailgen.Table{
// Data: [][]mailgen.Entry{
// {
// {"Key": "name", "Value": "John Doe"},
// {"Key": "email", "Value": "john@example.com"},
// },
// },
// Columns: mailgen.Columns{
// CustomWidth: map[string]string{
// "name": "200px",
// "email": "300px",
// },
// CustomAlign: map[string]string{
// "name": "left",
// "email": "right",
// },
// },
// }
type Table struct {
// Data contains the rows of the table, each row is a slice of Entry.
// Each Entry has a Key and Value, where Key is the column name.
Data [][]Entry
// Columns defines column properties like width and alignment.
Columns Columns
}
// Entry represents a single entry in the table with a key and value.
type Entry struct {
Key string
Value string
}
// Columns defines the structure of the table columns.
type Columns struct {
// CustomWidth allows setting specific widths for columns.
CustomWidth map[string]string
// CustomAlign allows setting specific alignments for columns.
CustomAlign map[string]string
}
func (a Action) HTML(tmpl *htmltemplate.Template) (string, error) {
var buf bytes.Buffer
err := tmpl.ExecuteTemplate(&buf, "button", a)
if err != nil {
return "", err
}
return buf.String(), nil
}
func (a Action) PlainText() (string, error) {
return a.Text + " (" + a.Link + ")", nil
}
func (l Line) HTML(tmpl *htmltemplate.Template) (string, error) {
var buf bytes.Buffer
err := tmpl.ExecuteTemplate(&buf, "line", l)
if err != nil {
return "", err
}
return buf.String(), nil
}
func (l Line) PlainText() (string, error) {
return l.Text, nil
}
func (t Table) HTML(tmpl *htmltemplate.Template) (string, error) {
var buf bytes.Buffer
err := tmpl.ExecuteTemplate(&buf, "table", t)
if err != nil {
return "", err
}
return buf.String(), nil
}
func (t Table) PlainText() (string, error) {
if len(t.Data) == 0 || len(t.Data[0]) == 0 {
return "", nil
}
// Extract column order from first row
columnNames := make([]string, 0, len(t.Data[0]))
for _, entry := range t.Data[0] {
columnNames = append(columnNames, entry.Key)
}
// Calculate column widths
colWidths := make(map[string]int)
for _, col := range columnNames {
colWidths[col] = len(col)
if wStr, ok := t.Columns.CustomWidth[col]; ok {
if w, err := strconv.Atoi(wStr); err == nil {
colWidths[col] = w
}
}
}
// If no custom width, compute max width from data
for _, row := range t.Data {
for _, entry := range row {
width := len(entry.Value)
if width > colWidths[entry.Key] {
colWidths[entry.Key] = width
}
}
}
var sb strings.Builder
t.writeHeader(&sb, columnNames, colWidths)
t.writeData(&sb, t.Data, columnNames, colWidths)
return sb.String(), nil
}
func (t Table) writeHeader(sb *strings.Builder, columnNames []string, colWidths map[string]int) {
// Header row
for i, col := range columnNames {
sb.WriteString(t.padString(t.capitalize(col), colWidths[col], t.Columns.CustomAlign[col]))
if i < len(columnNames)-1 {
sb.WriteString(" | ")
}
}
sb.WriteString("\n")
// Separator row
for i, col := range columnNames {
sb.WriteString(strings.Repeat("-", colWidths[col]))
if i < len(columnNames)-1 {
sb.WriteString("-+-")
}
}
sb.WriteString("\n")
}
func (t Table) writeData(sb *strings.Builder, data [][]Entry, columnNames []string, colWidths map[string]int) {
for _, row := range data {
entryMap := make(map[string]string)
for _, e := range row {
entryMap[e.Key] = e.Value
}
for i, col := range columnNames {
val := entryMap[col]
sb.WriteString(t.padString(val, colWidths[col], t.Columns.CustomAlign[col]))
if i < len(columnNames)-1 {
sb.WriteString(" | ")
}
}
sb.WriteString("\n")
}
}
func (t Table) padString(s string, width int, align string) string {
switch align {
case "right":
return fmt.Sprintf("%*s", width, s)
case "center":
pad := width - len(s)
left := pad / 2 //nolint:mnd // integer division
right := pad - left
return strings.Repeat(" ", left) + s + strings.Repeat(" ", right)
default: // left
return fmt.Sprintf("%-*s", width, s)
}
}
func (t Table) capitalize(s string) string {
if s == "" {
return ""
}
runes := []rune(s)
runes[0] = unicode.ToUpper(runes[0])
return string(runes)
}