-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtemplate.go
101 lines (84 loc) · 2.52 KB
/
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
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
package main
import (
"github.com/andrew-d/go-termutil"
"github.com/fatih/color"
"os"
"strconv"
"text/template"
"time"
)
const MergeRequestListTemplate string = `
{{ blue "#" }}{{ itoa .Iid | yellow }} {{ .Title | green | bold }}
{{ green .SourceBranch }} -> {{ red .TargetBranch }}
{{ .Description }}
`
const MergeRequestCheckoutListTemplate string = `{{ green .Title }}
`
const FeedTitleTemplate string = `
{{ .Title | bold }}
`
const FeedTemplate string = `
{{ magenta "[" | bold }}{{ .Updated | shortDate }}{{ magenta "]" | bold }} {{ .Title }}
`
type formatFunc func(string, ...interface{}) string
// Map of colored template funcs: true, and non-colored: false
var templateFuncs map[bool]template.FuncMap
func init() {
templateFuncs = make(map[bool]template.FuncMap)
// Setup color functions for text/template
colorFuncs := map[string]formatFunc{
"green": color.GreenString,
"red": color.RedString,
"yellow": color.YellowString,
"white": color.WhiteString,
"cyan": color.CyanString,
"black": color.BlackString,
"blue": color.BlueString,
"magenta": color.MagentaString,
}
colorFuncMap := template.FuncMap{
"bold": func(input string) string {
return color.New(color.Bold).SprintFunc()(input)
},
}
for c, fun := range colorFuncs {
colorFuncMap[c] = func(finner formatFunc) func(string) string {
return func(input string) string {
return finner(input)
}
}(fun)
}
templateFuncs[true] = colorFuncMap
monochromeFuncs := make(template.FuncMap)
stringIdentity := func(input string) string {
return input
}
for name, _ := range colorFuncMap {
monochromeFuncs[name] = stringIdentity
}
templateFuncs[false] = monochromeFuncs
// Shared functions
for _, b := range []bool{true, false} {
templateFuncs[b]["itoa"] = strconv.Itoa
templateFuncs[b]["shortDate"] = func(t time.Time) string {
return t.Format("2006-01-02 15:04")
}
}
}
/// Determine from tty output, whether we should do colors
func doColors(output *os.File) bool {
return termutil.Isatty(output.Fd())
}
/// Get new template for colored output
func newColorTemplate(name, format string) (*template.Template, error) {
return newTemplate(name, format, true)
}
/// Get new template with monochrome version of color functions
func newMonochromeTemplate(name, format string) (*template.Template, error) {
return newTemplate(name, format, false)
}
func newTemplate(name, format string, color bool) (*template.Template, error) {
tmpl := template.New(name)
tmpl.Funcs(templateFuncs[color])
return tmpl.Parse(format)
}