-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcolorizer.go
182 lines (161 loc) · 4.15 KB
/
colorizer.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"fmt"
"io"
"strings"
"github.com/mitchellh/colorstring"
"github.com/ujiro99/logcatf/logcat"
)
const (
defaultColorsKey = 0
defaultColorsValue = 1
prefix = "LOGCATF_"
)
var defaultColors = [][]string{
// Default foreground/background colors
{"default", "39"},
{"_default_", "49"},
// Foreground colors
{"black", "30"},
{"red", "31"},
{"green", "32"},
{"yellow", "33"},
{"blue", "34"},
{"magenta", "35"},
{"cyan", "36"},
{"light_gray", "37"},
{"dark_gray", "90"},
{"light_red", "91"},
{"light_green", "92"},
{"light_yellow", "93"},
{"light_blue", "94"},
{"light_magenta", "95"},
{"light_cyan", "96"},
{"white", "97"},
// Background colors
{"_black_", "40"},
{"_red_", "41"},
{"_green_", "42"},
{"_yellow_", "43"},
{"_blue_", "44"},
{"_magenta_", "45"},
{"_cyan_", "46"},
{"_light_gray_", "47"},
{"_dark_gray_", "100"},
{"_light_red_", "101"},
{"_light_green_", "102"},
{"_light_yellow_", "103"},
{"_light_blue_", "104"},
{"_light_magenta_", "105"},
{"_light_cyan_", "106"},
{"_white_", "107"},
// Attributes
{"bold", "1"},
{"dim", "2"},
{"underline", "4"},
{"blink_slow", "5"},
{"blink_fast", "6"},
{"invert", "7"},
{"hidden", "8"},
// Reset to reset everything to their defaults
{"reset", "0"},
{"reset_bold", "21"},
}
var colorMap = map[string]string{
// Default foreground/background colors
prefix + "default": "39",
prefix + "_default_": "49",
// Foreground colors
prefix + "black": "30",
prefix + "red": "31",
prefix + "green": "32",
prefix + "yellow": "33",
prefix + "blue": "34",
prefix + "magenta": "35",
prefix + "cyan": "36",
prefix + "light_gray": "37",
prefix + "dark_gray": "90",
prefix + "light_red": "91",
prefix + "light_green": "92",
prefix + "light_yellow": "93",
prefix + "light_blue": "94",
prefix + "light_magenta": "95",
prefix + "light_cyan": "96",
prefix + "white": "97",
// Background colors
prefix + "_black_": "40",
prefix + "_red_": "41",
prefix + "_green_": "42",
prefix + "_yellow_": "43",
prefix + "_blue_": "44",
prefix + "_magenta_": "45",
prefix + "_cyan_": "46",
prefix + "_light_gray_": "47",
prefix + "_dark_gray_": "100",
prefix + "_light_red_": "101",
prefix + "_light_green_": "102",
prefix + "_light_yellow_": "103",
prefix + "_light_blue_": "104",
prefix + "_light_magenta_": "105",
prefix + "_light_cyan_": "106",
prefix + "_white_": "107",
// Attributes
prefix + "bold": "1",
prefix + "dim": "2",
prefix + "underline": "4",
prefix + "blink_slow": "5",
prefix + "blink_fast": "6",
prefix + "invert": "7",
prefix + "hidden": "8",
// Reset to reset everything to their defaults
prefix + "reset": "0",
prefix + "reset_bold": "21",
}
// priorityColors represents default color of priority.
var priorityColors = map[string]string{
"V": "default",
"D": "blue",
"I": "default",
"W": "yellow",
"E": "red",
"F": "red",
}
// Colorizer enables to print strings colored.
type Colorizer struct {
context colorstring.Colorize
}
// ColorConfig has configuration for colorize.
type ColorConfig map[string]string
// Fprintln prints string according to color settings.
func (f *Colorizer) Fprintln(writer io.Writer, str string, item logcat.Entry) {
color, ok := priorityColors[item["priority"]]
if ok {
str = fmt.Sprintf("[%s%s]%s", prefix, color, str)
}
fmt.Fprintln(writer, f.context.Color(str))
}
// SetUp color settings.
func (f *Colorizer) Init(colorEnable bool, config ColorConfig) {
for k, v := range config {
if v == "" {
continue
}
priorityColors[k] = v
colorEnable = true
}
c := colorstring.Colorize{
Colors: colorMap,
Disable: !colorEnable,
Reset: true,
}
f.context = c
}
// ReplaceColorCode replaces colorCode in format, to be safety.
func (f *Colorizer) ReplaceColorCode(format string) string {
for _, pair := range defaultColors {
oldKey := "[" + pair[defaultColorsKey] + "]"
newKey := "[" + prefix + pair[defaultColorsKey] + "]"
format = strings.Replace(format, oldKey, newKey, flagAll)
}
return format
}