/
helpers.go
63 lines (53 loc) · 1.24 KB
/
helpers.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
package templates
import (
"bytes"
"fmt"
"strings"
"text/template"
"github.com/manifoldco/promptui"
money "github.com/rhymond/go-money"
)
func funcMap() template.FuncMap {
fn := promptui.FuncMap
fn["price"] = price
fn["title"] = title
return fn
}
func price(price int) string {
if price == 0 {
return "Free"
}
return money.New(int64(price), "USD").Display() + "/month"
}
func title(v interface{}) string {
val := fmt.Sprintf("%v", v)
return strings.Title(val)
}
func SelectSuccess(tpls *promptui.SelectTemplates, data interface{}) string {
tpl, err := template.New("").Funcs(funcMap()).Parse(tpls.Selected)
if err != nil {
return fmt.Sprintf("%+v", data)
}
var buf bytes.Buffer
err = tpl.Execute(&buf, data)
if err != nil {
return fmt.Sprintf("%+v", data)
}
return buf.String()
}
func PromptSuccess(label, value string) string {
data := input{Label: label, Value: value}
return render(tplSuccess, data)
}
func PromptFailure(label, value string) string {
data := input{Label: label, Value: value}
return render(tplFailure, data)
}
func render(tpl *template.Template, data interface{}) string {
var buf bytes.Buffer
err := tpl.Execute(&buf, data)
if err != nil {
return fmt.Sprintf("%+v", data)
}
return buf.String()
}