summaryrefslogtreecommitdiff
path: root/cmd/rr/utils/cprint.go
blob: f6f828f8ee459a48e4180f72efb734c7623fd38e (plain)
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
package utils

import (
	"fmt"
	"gopkg.in/AlecAivazis/survey.v1/core"
	"regexp"
	"strings"
)

// Printf works identically to fmt.Print but adds `<white+hb>color formatting support for CLI</reset>`.
func Printf(format string, args ...interface{}) {
	fmt.Print(Sprintf(format, args...))
}

// Sprintf works identically to fmt.Sprintf but adds `<white+hb>color formatting support for CLI</reset>`.
func Sprintf(format string, args ...interface{}) string {
	r, err := regexp.Compile(`<([^>]+)>`)
	if err != nil {
		panic(err)
	}

	format = r.ReplaceAllStringFunc(format, func(s string) string {
		return fmt.Sprintf(`{{color "%s"}}`, strings.Trim(s, "<>/"))
	})

	out, err := core.RunTemplate(fmt.Sprintf(format, args...), nil)
	if err != nil {
		panic(err)
	}

	return out
}