forked from fydemy/console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.go
More file actions
240 lines (208 loc) · 5.72 KB
/
console.go
File metadata and controls
240 lines (208 loc) · 5.72 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package console
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Purple = "\033[35m"
Cyan = "\033[36m"
Gray = "\033[37m"
Bold = "\033[1m"
)
func Log(data interface{}) {
switch v := data.(type) {
case nil:
fmt.Printf("%s%snull%s\n", Gray, Bold, Reset)
case string:
fmt.Printf("%s\"%s\"%s\n", Green, v, Reset)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
fmt.Printf("%s%v%s\n", Blue, v, Reset)
case float32, float64:
fmt.Printf("%s%v%s\n", Blue, v, Reset)
case bool:
if v {
fmt.Printf("%s%strue%s\n", Yellow, Bold, Reset)
} else {
fmt.Printf("%s%sfalse%s\n", Yellow, Bold, Reset)
}
case time.Time:
fmt.Printf("%s%s%s\n", Purple, v.Format("2006-01-02 15:04:05"), Reset)
default:
jsonBytes, err := json.MarshalIndent(v, "", " ")
if err != nil {
fmt.Printf("%sError formatting data: %v%s\n", Red, err, Reset)
return
}
output := string(jsonBytes)
output = colorizeJSON(output)
fmt.Println(output)
}
}
func Table(data interface{}) {
v := reflect.ValueOf(data)
if v.Kind() != reflect.Slice {
fmt.Printf("%sError: Table() requires a slice%s\n", Red, Reset)
return
}
if v.Len() == 0 {
fmt.Printf("%sEmpty slice%s\n", Gray, Reset)
return
}
firstElem := v.Index(0)
if firstElem.Kind() != reflect.Struct {
printSimpleTable(v)
return
}
printStructTable(v)
}
func Info(message interface{}) {
timestamp := time.Now().Format("15:04:05")
fmt.Printf("%s[%s]%s %sINFO%s %v\n",
Gray, timestamp, Reset,
Cyan, Reset, message)
}
func Warn(message interface{}) {
timestamp := time.Now().Format("15:04:05")
fmt.Printf("%s[%s]%s %s%sWARN%s %v\n",
Gray, timestamp, Reset,
Yellow, Bold, Reset, message)
}
func Error(message interface{}) {
timestamp := time.Now().Format("15:04:05")
fmt.Printf("%s[%s]%s %s%sERROR%s %v\n",
Gray, timestamp, Reset,
Red, Bold, Reset, message)
}
func Success(message interface{}) {
timestamp := time.Now().Format("15:04:05")
fmt.Printf("%s[%s]%s %s%sSUCCESS%s %v\n",
Gray, timestamp, Reset,
Green, Bold, Reset, message)
}
func Debug(label string, data interface{}) {
timestamp := time.Now().Format("15:04:05.000")
fmt.Printf("%s[%s]%s %s%sDEBUG%s %s%s:%s\n",
Gray, timestamp, Reset,
Purple, Bold, Reset,
Bold, label, Reset)
Log(data)
fmt.Println()
}
func colorizeJSON(jsonStr string) string {
lines := strings.Split(jsonStr, "\n")
var colorized []string
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "\"") && strings.Contains(trimmed, ":") {
parts := strings.SplitN(trimmed, ":", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
value = strings.TrimSuffix(value, ",")
comma := ""
if strings.HasSuffix(strings.TrimSpace(parts[1]), ",") {
comma = ","
}
colorized = append(colorized, fmt.Sprintf("%s %s%s%s: %s%s%s",
strings.Repeat(" ", len(line)-len(trimmed)),
Cyan, key, Reset,
getValueColor(value), value, Reset)+comma)
continue
}
}
colorized = append(colorized, line)
}
return strings.Join(colorized, "\n")
}
func getValueColor(value string) string {
value = strings.TrimSpace(value)
if value == "null" {
return Gray + Bold
}
if value == "true" || value == "false" {
return Yellow + Bold
}
if strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") {
return Green
}
if _, err := strconv.ParseFloat(value, 64); err == nil {
return Blue
}
return Reset
}
func printSimpleTable(v reflect.Value) {
fmt.Printf("%s┌─────┬─────────┐%s\n", Gray, Reset)
fmt.Printf("%s│%s %s%sIdx%s %s│%s %s%sValue%s %s│%s\n",
Gray, Reset, Bold, Cyan, Reset, Gray, Reset, Bold, Cyan, Reset, Gray, Reset)
fmt.Printf("%s├─────┼─────────┤%s\n", Gray, Reset)
for i := 0; i < v.Len(); i++ {
elem := v.Index(i)
fmt.Printf("%s│%s %s%-3d%s %s│%s %-7v %s│%s\n",
Gray, Reset, Blue, i, Reset, Gray, Reset, elem.Interface(), Gray, Reset)
}
fmt.Printf("%s└─────┴─────────┘%s\n", Gray, Reset)
}
func printStructTable(v reflect.Value) {
firstElem := v.Index(0)
elemType := firstElem.Type()
var fieldNames []string
for i := 0; i < elemType.NumField(); i++ {
fieldNames = append(fieldNames, elemType.Field(i).Name)
}
colWidths := make([]int, len(fieldNames))
for i, name := range fieldNames {
colWidths[i] = len(name)
}
var rows [][]string
for i := 0; i < v.Len(); i++ {
elem := v.Index(i)
var row []string
for j := 0; j < elem.NumField(); j++ {
fieldValue := elem.Field(j).Interface()
valueStr := fmt.Sprintf("%v", fieldValue)
row = append(row, valueStr)
if len(valueStr) > colWidths[j] {
colWidths[j] = len(valueStr)
}
}
rows = append(rows, row)
}
printTableSeparator(colWidths, "┌", "┬", "┐")
printTableRow(fieldNames, colWidths, true)
printTableSeparator(colWidths, "├", "┼", "┤")
for _, row := range rows {
printTableRow(row, colWidths, false)
}
printTableSeparator(colWidths, "└", "┴", "┘")
}
func printTableSeparator(colWidths []int, left, middle, right string) {
fmt.Printf("%s%s", Gray, left)
for i, width := range colWidths {
fmt.Print(strings.Repeat("─", width+2))
if i < len(colWidths)-1 {
fmt.Print(middle)
}
}
fmt.Printf("%s%s\n", right, Reset)
}
func printTableRow(row []string, colWidths []int, isHeader bool) {
fmt.Printf("%s│%s", Gray, Reset)
for i, cell := range row {
color := Reset
if isHeader {
color = Bold + Cyan
}
fmt.Printf(" %s%-*s%s ", color, colWidths[i], cell, Reset)
fmt.Printf("%s│%s", Gray, Reset)
}
fmt.Println()
}