-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
48 lines (42 loc) · 902 Bytes
/
util.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
package formatter
import (
"fmt"
"strings"
)
const (
ColorBlack = iota + 30
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorMagenta
ColorCyan
ColorWhite
ColorBold = 1
ColorDarkGray = 90
)
func Colorize(s any, c int, enabled bool) string {
if enabled {
return fmt.Sprintf("\x1b[%dm%v\x1b[0m", c, s)
}
return fmt.Sprintf("%s", s)
}
func Boldrize(s any, c int, enabled bool) string {
return Colorize(Colorize(s, c, enabled), ColorBold, enabled)
}
func kvJoin(slice ...string) string {
var pairs []string
for i := 0; i < len(slice); i += 2 {
pairs = append(pairs, slice[i]+slice[i+1])
}
return strings.Join(pairs, string(' '))
}
// needsQuote returns true when the string s should be quoted in output.
func needsQuote(s string) bool {
for i := range s {
if s[i] < 0x20 || s[i] > 0x7e || s[i] == ' ' || s[i] == '\\' || s[i] == '"' {
return true
}
}
return false
}