| 1 |
package templates |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"os/exec" |
| 9 |
"path/filepath" |
| 10 |
"regexp" |
| 11 |
"strings" |
| 12 |
"text/template" |
| 13 |
"time" |
| 14 |
|
| 15 |
"git.sr.ht/~rjarry/aerc/lib/format" |
| 16 |
"git.sr.ht/~rjarry/aerc/lib/parse" |
| 17 |
"git.sr.ht/~rjarry/aerc/models" |
| 18 |
"github.com/emersion/go-message/mail" |
| 19 |
) |
| 20 |
|
| 21 |
var version string |
| 22 |
|
| 23 |
// SetVersion initializes the aerc version displayed in template functions |
| 24 |
func SetVersion(v string) { |
| 25 |
version = v |
| 26 |
} |
| 27 |
|
| 28 |
var execPath string |
| 29 |
|
| 30 |
func SetExecPath(dirs []string) { |
| 31 |
// prepend aerc filters dirs to the default exec path |
| 32 |
paths := make([]string, 0, len(dirs)+1) |
| 33 |
for _, d := range dirs { |
| 34 |
paths = append(paths, filepath.Join(d, "filters")) |
| 35 |
} |
| 36 |
paths = append(paths, os.Getenv("PATH")) |
| 37 |
execPath = strings.Join(paths, ":") |
| 38 |
} |
| 39 |
|
| 40 |
// wrap allows to chain wrapText |
| 41 |
func wrap(lineWidth int, text string) string { |
| 42 |
return wrapText(text, lineWidth) |
| 43 |
} |
| 44 |
|
| 45 |
func wrapLine(text string, lineWidth int) string { |
| 46 |
words := strings.Fields(text) |
| 47 |
if len(words) == 0 { |
| 48 |
return text |
| 49 |
} |
| 50 |
var wrapped strings.Builder |
| 51 |
wrapped.WriteString(words[0]) |
| 52 |
spaceLeft := lineWidth - wrapped.Len() |
| 53 |
for _, word := range words[1:] { |
| 54 |
if len(word)+1 > spaceLeft { |
| 55 |
wrapped.WriteRune('\n') |
| 56 |
wrapped.WriteString(word) |
| 57 |
spaceLeft = lineWidth - len(word) |
| 58 |
} else { |
| 59 |
wrapped.WriteRune(' ') |
| 60 |
wrapped.WriteString(word) |
| 61 |
spaceLeft -= 1 + len(word) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return wrapped.String() |
| 66 |
} |
| 67 |
|
| 68 |
func wrapText(text string, lineWidth int) string { |
| 69 |
text = strings.ReplaceAll(text, "\r\n", "\n") |
| 70 |
text = strings.TrimRight(text, "\n") |
| 71 |
lines := strings.Split(text, "\n") |
| 72 |
var wrapped strings.Builder |
| 73 |
|
| 74 |
for _, line := range lines { |
| 75 |
switch { |
| 76 |
case line == "": |
| 77 |
// deliberately left blank |
| 78 |
case line[0] == '>': |
| 79 |
// leave quoted text alone |
| 80 |
wrapped.WriteString(line) |
| 81 |
default: |
| 82 |
wrapped.WriteString(wrapLine(line, lineWidth)) |
| 83 |
} |
| 84 |
wrapped.WriteRune('\n') |
| 85 |
} |
| 86 |
return wrapped.String() |
| 87 |
} |
| 88 |
|
| 89 |
// quote prepends "> " in front of every line in text |
| 90 |
func quote(text string) string { |
| 91 |
text = strings.ReplaceAll(text, "\r\n", "\n") |
| 92 |
text = strings.TrimRight(text, "\n") |
| 93 |
lines := strings.Split(text, "\n") |
| 94 |
var quoted strings.Builder |
| 95 |
for _, line := range lines { |
| 96 |
if line == "" { |
| 97 |
quoted.WriteString(">\n") |
| 98 |
continue |
| 99 |
} |
| 100 |
if strings.HasPrefix(line, ">") { |
| 101 |
quoted.WriteString(">") |
| 102 |
} else { |
| 103 |
quoted.WriteString("> ") |
| 104 |
} |
| 105 |
quoted.WriteString(line) |
| 106 |
quoted.WriteRune('\n') |
| 107 |
} |
| 108 |
|
| 109 |
return quoted.String() |
| 110 |
} |
| 111 |
|
| 112 |
// cmd allow to parse reply by shell command |
| 113 |
// text have to be passed by cmd param |
| 114 |
// if there is error, original string is returned |
| 115 |
func cmd(cmd, text string) string { |
| 116 |
var out bytes.Buffer |
| 117 |
c := exec.Command("sh", "-c", cmd) |
| 118 |
c.Env = append(os.Environ(), "PATH="+execPath) |
| 119 |
c.Stdin = strings.NewReader(text) |
| 120 |
c.Stdout = &out |
| 121 |
err := c.Run() |
| 122 |
if err != nil { |
| 123 |
return text |
| 124 |
} |
| 125 |
return out.String() |
| 126 |
} |
| 127 |
|
| 128 |
func toLocal(t time.Time) time.Time { |
| 129 |
return time.Time.In(t, time.Local) |
| 130 |
} |
| 131 |
|
| 132 |
func rearrangeNameWithComma(name string) string { |
| 133 |
parts := strings.SplitN(name, ",", 3) |
| 134 |
if len(parts) == 2 { |
| 135 |
return fmt.Sprintf("%s %s", strings.TrimSpace(parts[1]), strings.TrimSpace(parts[0])) |
| 136 |
} |
| 137 |
return strings.TrimSpace(name) |
| 138 |
} |
| 139 |
|
| 140 |
func names(addresses []*mail.Address) []string { |
| 141 |
n := make([]string, len(addresses)) |
| 142 |
for i, addr := range addresses { |
| 143 |
name := rearrangeNameWithComma(addr.Name) |
| 144 |
if name == "" { |
| 145 |
parts := strings.SplitN(addr.Address, "@", 2) |
| 146 |
name = parts[0] |
| 147 |
} |
| 148 |
n[i] = name |
| 149 |
} |
| 150 |
return n |
| 151 |
} |
| 152 |
|
| 153 |
func firstnames(addresses []*mail.Address) []string { |
| 154 |
n := make([]string, len(addresses)) |
| 155 |
for i, addr := range addresses { |
| 156 |
var name string |
| 157 |
if addr.Name == "" { |
| 158 |
parts := strings.SplitN(addr.Address, "@", 2) |
| 159 |
parts = strings.SplitN(parts[0], ".", 2) |
| 160 |
name = parts[0] |
| 161 |
} else { |
| 162 |
name = rearrangeNameWithComma(addr.Name) |
| 163 |
name = strings.SplitN(name, " ", 2)[0] // split by spaces and get the first word |
| 164 |
name = strings.SplitN(name, ",", 2)[0] // split by commas and get the first word |
| 165 |
} |
| 166 |
n[i] = name |
| 167 |
} |
| 168 |
return n |
| 169 |
} |
| 170 |
|
| 171 |
func initials(addresses []*mail.Address) []string { |
| 172 |
n := names(addresses) |
| 173 |
ret := make([]string, len(addresses)) |
| 174 |
for i, name := range n { |
| 175 |
split := strings.Split(name, " ") |
| 176 |
var initial strings.Builder |
| 177 |
for _, s := range split { |
| 178 |
initial.WriteString(string([]rune(s)[0:1])) |
| 179 |
} |
| 180 |
ret[i] = initial.String() |
| 181 |
} |
| 182 |
return ret |
| 183 |
} |
| 184 |
|
| 185 |
func emails(addresses []*mail.Address) []string { |
| 186 |
e := make([]string, len(addresses)) |
| 187 |
for i, addr := range addresses { |
| 188 |
e[i] = addr.Address |
| 189 |
} |
| 190 |
return e |
| 191 |
} |
| 192 |
|
| 193 |
func mboxes(addresses []*mail.Address) []string { |
| 194 |
e := make([]string, len(addresses)) |
| 195 |
for i, addr := range addresses { |
| 196 |
parts := strings.SplitN(addr.Address, "@", 2) |
| 197 |
e[i] = parts[0] |
| 198 |
} |
| 199 |
return e |
| 200 |
} |
| 201 |
|
| 202 |
func shortmboxes(addresses []*mail.Address) []string { |
| 203 |
e := make([]string, len(addresses)) |
| 204 |
for i, addr := range addresses { |
| 205 |
parts := strings.SplitN(addr.Address, "@", 2) |
| 206 |
parts = strings.SplitN(parts[0], ".", 2) |
| 207 |
e[i] = parts[0] |
| 208 |
} |
| 209 |
return e |
| 210 |
} |
| 211 |
|
| 212 |
func persons(addresses []*mail.Address) []string { |
| 213 |
e := make([]string, len(addresses)) |
| 214 |
for i, addr := range addresses { |
| 215 |
e[i] = format.AddressForHumans(addr) |
| 216 |
} |
| 217 |
return e |
| 218 |
} |
| 219 |
|
| 220 |
var units = []string{"K", "M", "G", "T"} |
| 221 |
|
| 222 |
func humanReadable(value int) string { |
| 223 |
sign := "" |
| 224 |
if value < 0 { |
| 225 |
sign = "-" |
| 226 |
value = -value |
| 227 |
} |
| 228 |
if value < 1000 { |
| 229 |
return fmt.Sprintf("%s%d", sign, value) |
| 230 |
} |
| 231 |
val := float64(value) |
| 232 |
unit := "" |
| 233 |
for i := 0; val >= 1000 && i < len(units); i++ { |
| 234 |
unit = units[i] |
| 235 |
val /= 1000.0 |
| 236 |
} |
| 237 |
if val < 100.0 { |
| 238 |
return fmt.Sprintf("%s%.1f%s", sign, val, unit) |
| 239 |
} |
| 240 |
return fmt.Sprintf("%s%.0f%s", sign, val, unit) |
| 241 |
} |
| 242 |
|
| 243 |
func cwd() string { |
| 244 |
path, err := os.Getwd() |
| 245 |
if err != nil { |
| 246 |
return err.Error() |
| 247 |
} |
| 248 |
home, err := os.UserHomeDir() |
| 249 |
if err != nil { |
| 250 |
return err.Error() |
| 251 |
} |
| 252 |
if strings.HasPrefix(path, home) { |
| 253 |
path = strings.Replace(path, home, "~", 1) |
| 254 |
} |
| 255 |
return path |
| 256 |
} |
| 257 |
|
| 258 |
func join(sep string, elems []string) string { |
| 259 |
return strings.Join(elems, sep) |
| 260 |
} |
| 261 |
|
| 262 |
func split(sep string, s string) []string { |
| 263 |
return strings.Split(s, sep) |
| 264 |
} |
| 265 |
|
| 266 |
// removes a signature from the piped in message |
| 267 |
func trimSignature(message string) string { |
| 268 |
var res strings.Builder |
| 269 |
|
| 270 |
input := bufio.NewScanner(strings.NewReader(message)) |
| 271 |
|
| 272 |
for input.Scan() { |
| 273 |
line := input.Text() |
| 274 |
if line == "-- " { |
| 275 |
break |
| 276 |
} |
| 277 |
res.WriteString(line) |
| 278 |
res.WriteRune('\n') |
| 279 |
} |
| 280 |
return res.String() |
| 281 |
} |
| 282 |
|
| 283 |
func compactDir(path string) string { |
| 284 |
return format.CompactPath(path, os.PathSeparator) |
| 285 |
} |
| 286 |
|
| 287 |
type ( |
| 288 |
Case struct{ expr, value string } |
| 289 |
Default struct{ value string } |
| 290 |
Exclude struct{ expr string } |
| 291 |
) |
| 292 |
|
| 293 |
func (c *Case) Matches(s string) bool { return parse.MatchCache(s, c.expr) } |
| 294 |
func (c *Case) Value() string { return c.value } |
| 295 |
func (c *Case) Skip() bool { return false } |
| 296 |
func (d *Default) Matches(s string) bool { return true } |
| 297 |
func (d *Default) Value() string { return d.value } |
| 298 |
func (d *Default) Skip() bool { return false } |
| 299 |
func (e *Exclude) Matches(s string) bool { return parse.MatchCache(s, e.expr) } |
| 300 |
func (e *Exclude) Value() string { return "" } |
| 301 |
func (e *Exclude) Skip() bool { return true } |
| 302 |
|
| 303 |
func switch_(value string, cases ...models.Case) string { |
| 304 |
for _, c := range cases { |
| 305 |
if c.Matches(value) { |
| 306 |
return c.Value() |
| 307 |
} |
| 308 |
} |
| 309 |
return "" |
| 310 |
} |
| 311 |
|
| 312 |
func case_(expr, value string) models.Case { |
| 313 |
return &Case{expr: expr, value: value} |
| 314 |
} |
| 315 |
|
| 316 |
func default_(value string) models.Case { |
| 317 |
return &Default{value: value} |
| 318 |
} |
| 319 |
|
| 320 |
func exclude(expr string) models.Case { |
| 321 |
return &Exclude{expr: expr} |
| 322 |
} |
| 323 |
|
| 324 |
func map_(elements []string, cases ...models.Case) []string { |
| 325 |
mapped := make([]string, 0, len(elements)) |
| 326 |
top: |
| 327 |
for _, e := range elements { |
| 328 |
for _, c := range cases { |
| 329 |
if c.Matches(e) { |
| 330 |
if c.Skip() { |
| 331 |
continue top |
| 332 |
} |
| 333 |
e = c.Value() |
| 334 |
break |
| 335 |
} |
| 336 |
} |
| 337 |
mapped = append(mapped, e) |
| 338 |
} |
| 339 |
return mapped |
| 340 |
} |
| 341 |
|
| 342 |
func replace(pattern, subst, value string) string { |
| 343 |
re := regexp.MustCompile(pattern) |
| 344 |
return re.ReplaceAllString(value, subst) |
| 345 |
} |
| 346 |
|
| 347 |
func contains(substring, s string) bool { |
| 348 |
return strings.Contains(s, substring) |
| 349 |
} |
| 350 |
|
| 351 |
func hasPrefix(prefix, s string) bool { |
| 352 |
return strings.HasPrefix(s, prefix) |
| 353 |
} |
| 354 |
|
| 355 |
func head(n uint, s string) string { |
| 356 |
r := []rune(s) |
| 357 |
length := uint(len(r)) |
| 358 |
if length >= n { |
| 359 |
return string(r[:n]) |
| 360 |
} |
| 361 |
return s |
| 362 |
} |
| 363 |
|
| 364 |
func tail(n uint, s string) string { |
| 365 |
r := []rune(s) |
| 366 |
length := uint(len(r)) |
| 367 |
if length >= n { |
| 368 |
return string(r[length-n:]) |
| 369 |
} |
| 370 |
return s |
| 371 |
} |
| 372 |
|
| 373 |
var templateFuncs = template.FuncMap{ |
| 374 |
"quote": quote, |
| 375 |
"wrapText": wrapText, |
| 376 |
"wrap": wrap, |
| 377 |
"now": time.Now, |
| 378 |
"dateFormat": time.Time.Format, |
| 379 |
"toLocal": toLocal, |
| 380 |
"exec": cmd, |
| 381 |
"version": func() string { return version }, |
| 382 |
"names": names, |
| 383 |
"firstnames": firstnames, |
| 384 |
"initials": initials, |
| 385 |
"emails": emails, |
| 386 |
"mboxes": mboxes, |
| 387 |
"shortmboxes": shortmboxes, |
| 388 |
"persons": persons, |
| 389 |
"humanReadable": humanReadable, |
| 390 |
"cwd": cwd, |
| 391 |
"join": join, |
| 392 |
"split": split, |
| 393 |
"trimSignature": trimSignature, |
| 394 |
"compactDir": compactDir, |
| 395 |
"match": parse.MatchCache, |
| 396 |
"switch": switch_, |
| 397 |
"case": case_, |
| 398 |
"default": default_, |
| 399 |
"map": map_, |
| 400 |
"exclude": exclude, |
| 401 |
"contains": contains, |
| 402 |
"hasPrefix": hasPrefix, |
| 403 |
"toLower": strings.ToLower, |
| 404 |
"toUpper": strings.ToUpper, |
| 405 |
"replace": replace, |
| 406 |
"head": head, |
| 407 |
"tail": tail, |
| 408 |
} |