-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt.go
93 lines (86 loc) · 1.28 KB
/
fmt.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
package zlog
import (
"bytes"
"strconv"
)
// Base attributes
const (
FmtReset int = iota
FmtBold
FmtFaint
FmtItalic
FmtUnderline
FmtBlinkSlow
FmtBlinkRapid
FmtReverseVideo
FmtConcealed
FmtCrossedOut
)
// Foreground colors
const (
FgBlack int = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
)
// Foreground Hi-Intensity colors
const (
FgHiBlack int = iota + 90
FgHiRed
FgHiGreen
FgHiYellow
FgHiBlue
FgHiMagenta
FgHiCyan
FgHiWhite
)
// Background colors
const (
BgBlack int = iota + 40
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
)
// Background Hi-Intensity colors
const (
BgHiBlack int = iota + 100
BgHiRed
BgHiGreen
BgHiYellow
BgHiBlue
BgHiMagenta
BgHiCyan
BgHiWhite
)
// Fmt formats a string with the provided attributes and colors and returns the formatted string.
//
// for example :
// zlog.Fmt("text", zlog.FmtBold, zlog.FgRed)
//
// output :
// \x1b[1;31mtext\x1b[0m
func Fmt(s string, attrs ...int) string {
if attrLen := len(attrs); attrLen > 0 {
b := bytes.Buffer{}
b.WriteString("\x1b[")
for i, attr := range attrs {
b.WriteString(strconv.Itoa(attr))
if i < attrLen-1 {
b.WriteRune(';')
}
}
b.WriteRune('m')
b.WriteString(s)
b.WriteString("\x1b[0m")
return b.String()
}
return s
}