-
Notifications
You must be signed in to change notification settings - Fork 1
/
print.go
103 lines (93 loc) · 2.24 KB
/
print.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
102
103
package utils
import (
"fmt"
"github.com/gookit/color"
"sort"
"strings"
)
var themes = map[string]*color.Theme{
"info": color.Info,
"note": color.Note,
"light": color.Light,
"error": color.Error,
"debug": color.Debug,
"danger": color.Danger,
"notice": color.Notice,
"success": color.Success,
"comment": color.Comment,
"primary": color.Primary,
"warning": color.Warn,
"question": color.Question,
"secondary": color.Secondary,
}
func Info(format string, args ...interface{}) {
color.Info.Prompt(format, args...)
}
func Warn(format string, args ...interface{}) {
color.Warn.Prompt(format, args...)
}
func Error(format string, args ...interface{}) {
color.Error.Prompt(format, args...)
}
func Println(format string, args ...interface{}) {
str := fmt.Sprintf(format, args...)
color.Print(fmt.Sprintf("%s\n", str))
}
func Print(format string, args ...interface{}) {
str := fmt.Sprintf(format, args...)
color.Print(str)
}
func Block(theme string, format string, args ...interface{}) {
colorTheme := themes[theme]
colorTheme.Block(format, args...)
}
func stringSplitter(str string) string {
lines := strings.Split(str, "\n")
if len(lines) <= 1 {
return str
}
newLines := []string{
" |-",
}
for _, line := range lines {
newLines = append(newLines, fmt.Sprintf(" %s", line))
}
return strings.Join(newLines, "\n")
}
func MapToYaml(val map[string]string) string {
var list []string
for key, val := range val {
if val != "" {
list = append(list, fmt.Sprintf(" <red>%s:</> %s", key, stringSplitter(val)))
}
}
sort.Strings(list)
return strings.Join(list, "\n")
}
func MapArrayToYaml(ml []map[string]string) string {
var output []string
for _, m := range ml {
var list []string
for key, val := range m {
if val != "" {
list = append(list, fmt.Sprintf("<red>%s:</> %s", key, stringSplitter(val)))
}
}
sort.Strings(list)
for i, line := range list {
if i == 0 {
output = append(output, fmt.Sprintf("- %s", line))
} else {
output = append(output, fmt.Sprintf(" %s", line))
}
}
}
return strings.Join(output, "\n")
}
func MapFlatten(m map[string]string) string {
var list []string
for key, val := range m {
list = append(list, fmt.Sprintf("%s=%s", key, val))
}
return strings.Join(list, ";")
}