-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.go
More file actions
129 lines (110 loc) · 2.67 KB
/
Copy pathrender.go
File metadata and controls
129 lines (110 loc) · 2.67 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
package main
import (
"encoding/base64"
"fmt"
"reflect"
"strings"
"unicode/utf8"
"github.com/fatih/color"
"github.com/hackebrot/turtle"
)
// render will render the reply from redis into a human friendly format with RT latency
func render(reply interface{}) string {
defer func() {
if r := recover(); r != nil {
logger.Error("Faced panic while render %v - Panic %v", reply, r)
color.Red("Panic while rendering: %v", r)
}
}()
if reply == nil {
return color.HiGreenString("<nil>")
}
switch val := reply.(type) {
case byte, int8, int16, int32, int64, int:
return color.GreenString("(integer) %d", val)
case float32, float64:
return color.GreenString("(float) %f", val)
case bool:
return color.GreenString("(boolean) %t", val)
case string:
return color.GreenString("(string) %s", val)
case []byte:
if s := string(val); utf8.ValidString(s) {
return color.GreenString("(string) %s", s)
}
b64 := base64.RawStdEncoding.EncodeToString(val)
return color.GreenString("(binary) %s", b64)
}
return renderComplex(reply)
}
// renderComplex renders complex data type like Slice and Map
func renderComplex(reply interface{}) string {
switch val := reflect.ValueOf(reply); val.Kind() {
case reflect.Slice:
slc := reply.([]interface{})
return renderSlice(slc)
case reflect.Map:
mp := reply.(map[interface{}]interface{})
return renderMap(mp)
default:
return color.HiMagentaString("%T <", val.Interface()) +
color.GreenString("%v", val.Interface()) +
color.HiMagentaString(" >")
}
}
func renderSlice(slc []interface{}) string {
var sb strings.Builder
var newline bool
if len(slc) > 20 {
newline = true
}
sb.WriteString(color.HiMagentaString("[ "))
for i, x := range slc {
var delim string
if i < len(slc)-1 {
if newline {
delim = "\n"
} else {
delim = ", "
}
}
sb.WriteString(render(x))
sb.WriteString(color.RedString(delim))
}
sb.WriteString(color.HiMagentaString(" ]"))
return sb.String()
}
func renderMap(mp map[interface{}]interface{}) string {
var delim string
if len(mp) > 10 {
delim = "\n"
} else {
delim = ","
}
var kvPairs []string
for k, v := range mp {
kv := color.GreenString(render(k)) +
color.HiMagentaString("->") +
color.GreenString(render(v))
kvPairs = append(kvPairs, kv)
}
return color.HiMagentaString("{") +
strings.Join(kvPairs, delim) +
color.HiMagentaString(" }")
}
func printable(bytes []byte) bool {
if len(bytes) > 2048 {
return false
}
return utf8.Valid(bytes)
}
func printWithEmoji(em string, data interface{}) {
emoji := turtle.Search(em)
var emChar string
if len(emoji) == 0 {
emChar = em
} else {
emChar = emoji[0].Char
}
fmt.Printf("%s %v\n", emChar, data)
}