/
color_print.go
65 lines (55 loc) · 1.26 KB
/
color_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
// Copyright (c) 2019 Chen Lei <my@mysq.to>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package log
import (
"fmt"
)
type color int
// ANSI color for terminal
const (
colorBlack color = iota + 30
colorRed
colorGreen
colorYellow
colorBlue
colorMagenta
colorCyan
colorWhite
)
func colorSeq(color color) string {
return fmt.Sprintf("\033[%dm", int(color))
}
func colorSeqBold(color color) string {
return fmt.Sprintf("\033[%d;1m", int(color))
}
var (
colors = []string{
FATAL: colorSeq(colorMagenta),
ERROR: colorSeq(colorRed),
WARN: colorSeq(colorYellow),
INFO: colorSeq(colorGreen),
DEBUG: colorSeq(colorCyan),
none: colorSeq(colorWhite),
}
boldColors = []string{
FATAL: colorSeqBold(colorMagenta),
ERROR: colorSeqBold(colorRed),
WARN: colorSeqBold(colorYellow),
INFO: colorSeqBold(colorGreen),
DEBUG: colorSeqBold(colorCyan),
none: colorSeqBold(colorWhite),
}
colorReset = "\033[0m"
)
// colorfultty writes the message with ANSI colors under windows
func (r *Record) colorfultty(backend Backend, bold bool) {
var col string
if bold {
col = boldColors[r.level]
} else {
col = colors[r.level]
}
r.color = col
_ = backend.write(r.msgBuf())
}