-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
41 lines (35 loc) · 1013 Bytes
/
template.go
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
package boa
import (
"fmt"
"io"
"strings"
"text/template"
"unicode"
)
var templateFuncs = template.FuncMap{
"trim": strings.TrimSpace,
"trimRightSpace": trimRightSpace,
"trimTrailingWhitespaces": trimRightSpace,
"rpad": rpad,
"sliceToCsv": sliceToCsv,
}
// trimRightSpace trims any trailing whitespace
func trimRightSpace(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
}
// rpad adds padding to the right of a string.
func rpad(s string, padding int) string {
template := fmt.Sprintf("%%-%ds", padding)
return fmt.Sprintf(template, s)
}
// sliceToCsv converts a string slice to a string csv
func sliceToCsv(args []string) string {
return strings.Join(args, ", ")
}
// tmpl executes the given template text on data, writing the result to w.
func tmpl(w io.Writer, text string, data interface{}) error {
t := template.New("tmpl")
t.Funcs(templateFuncs)
template.Must(t.Parse(text))
return t.Execute(w, data)
}