/
helpers.go
45 lines (37 loc) · 930 Bytes
/
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
package rx
import (
"fmt"
"strings"
"github.com/fatih/color"
"github.com/gobuffalo/plush/v4"
)
const (
SUCCESS = "\u2713"
ERROR = "\u2718"
WARNING = "\u26A0"
)
type Helpers struct{}
func (Helpers) Warning(help plush.HelperContext) (string, error) {
return colorize(color.YellowString, help, WARNING)
}
func (Helpers) Error(help plush.HelperContext) (string, error) {
return colorize(color.RedString, help, ERROR)
}
func (Helpers) Success(help plush.HelperContext) (string, error) {
return colorize(color.GreenString, help, SUCCESS)
}
func colorize(fn func(s string, i ...interface{}) string, help plush.HelperContext, mark string) (string, error) {
if !help.HasBlock() {
return "", fmt.Errorf("no block given")
}
x, err := help.Block()
if err != nil {
return "", err
}
x = strings.TrimSpace(x)
if len(mark) > 0 {
x = fmt.Sprintf("%s %s", mark, x)
}
x = strings.TrimSpace(fn(x))
return x, nil
}