Skip to content

Commit 6553fd6

Browse files
refactor!: Move color to its own package (backport #736) (#739)
Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
1 parent 5501309 commit 6553fd6

3 files changed

Lines changed: 70 additions & 65 deletions

File tree

internal/color/colors.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// TODO: Can we delete this package. Honestly pretty insane this is in our database.
2+
package color
3+
4+
import (
5+
"fmt"
6+
"os"
7+
"strings"
8+
)
9+
10+
const (
11+
ANSIReset = "\x1b[0m"
12+
ANSIBright = "\x1b[1m"
13+
14+
ANSIFgGreen = "\x1b[32m"
15+
ANSIFgBlue = "\x1b[34m"
16+
ANSIFgCyan = "\x1b[36m"
17+
)
18+
19+
// color the string s with color 'color'
20+
// unless s is already colored
21+
func treat(s string, color string) string {
22+
if len(s) > 2 && s[:2] == "\x1b[" {
23+
return s
24+
}
25+
return color + s + ANSIReset
26+
}
27+
28+
func treatAll(color string, args ...interface{}) string {
29+
parts := make([]string, 0, len(args))
30+
for _, arg := range args {
31+
parts = append(parts, treat(fmt.Sprintf("%v", arg), color))
32+
}
33+
return strings.Join(parts, "")
34+
}
35+
36+
func Green(args ...interface{}) string {
37+
return treatAll(ANSIFgGreen, args...)
38+
}
39+
40+
func Blue(args ...interface{}) string {
41+
return treatAll(ANSIFgBlue, args...)
42+
}
43+
44+
func Cyan(args ...interface{}) string {
45+
return treatAll(ANSIFgCyan, args...)
46+
}
47+
48+
// ColoredBytes takes in the byte that you would like to show as a string and byte
49+
// and will display them in a human readable format.
50+
// If the environment variable TENDERMINT_IAVL_COLORS_ON is set to a non-empty string then different colors will be used for bytes and strings.
51+
func ColoredBytes(data []byte, textColor, bytesColor func(...interface{}) string) string {
52+
colors := os.Getenv("TENDERMINT_IAVL_COLORS_ON")
53+
if colors == "" {
54+
for _, b := range data {
55+
return string(b)
56+
}
57+
}
58+
s := ""
59+
for _, b := range data {
60+
if 0x21 <= b && b < 0x7F {
61+
s += textColor(string(b))
62+
} else {
63+
s += bytesColor(fmt.Sprintf("%02X", b))
64+
}
65+
}
66+
return s
67+
}

node.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/cosmos/iavl/cache"
1616

17+
"github.com/cosmos/iavl/internal/color"
1718
"github.com/cosmos/iavl/internal/encoding"
1819
)
1920

@@ -167,8 +168,8 @@ func (node *Node) String() string {
167168
child += fmt.Sprintf("{right %v}", node.rightNode.nodeKey)
168169
}
169170
return fmt.Sprintf("Node{%s:%s@ %v:%v-%v %d-%d}#%s\n",
170-
ColoredBytes(node.key, Green, Blue),
171-
ColoredBytes(node.value, Cyan, Blue),
171+
color.ColoredBytes(node.key, color.Green, color.Blue),
172+
color.ColoredBytes(node.value, color.Cyan, color.Blue),
172173
node.nodeKey, node.leftNodeKey, node.rightNodeKey,
173174
node.size, node.subtreeHeight, child)
174175
}

util.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package iavl
22

33
import (
44
"fmt"
5-
"os"
6-
"strings"
75
)
86

97
// PrintTree prints the whole tree in an indented form.
@@ -66,64 +64,3 @@ func maxInt8(a, b int8) int8 {
6664
}
6765
return b
6866
}
69-
70-
// Colors: ------------------------------------------------
71-
72-
const (
73-
ANSIReset = "\x1b[0m"
74-
ANSIBright = "\x1b[1m"
75-
76-
ANSIFgGreen = "\x1b[32m"
77-
ANSIFgBlue = "\x1b[34m"
78-
ANSIFgCyan = "\x1b[36m"
79-
)
80-
81-
// color the string s with color 'color'
82-
// unless s is already colored
83-
func treat(s string, color string) string {
84-
if len(s) > 2 && s[:2] == "\x1b[" {
85-
return s
86-
}
87-
return color + s + ANSIReset
88-
}
89-
90-
func treatAll(color string, args ...interface{}) string {
91-
parts := make([]string, 0, len(args))
92-
for _, arg := range args {
93-
parts = append(parts, treat(fmt.Sprintf("%v", arg), color))
94-
}
95-
return strings.Join(parts, "")
96-
}
97-
98-
func Green(args ...interface{}) string {
99-
return treatAll(ANSIFgGreen, args...)
100-
}
101-
102-
func Blue(args ...interface{}) string {
103-
return treatAll(ANSIFgBlue, args...)
104-
}
105-
106-
func Cyan(args ...interface{}) string {
107-
return treatAll(ANSIFgCyan, args...)
108-
}
109-
110-
// ColoredBytes takes in the byte that you would like to show as a string and byte
111-
// and will display them in a human readable format.
112-
// If the environment variable TENDERMINT_IAVL_COLORS_ON is set to a non-empty string then different colors will be used for bytes and strings.
113-
func ColoredBytes(data []byte, textColor, bytesColor func(...interface{}) string) string {
114-
colors := os.Getenv("TENDERMINT_IAVL_COLORS_ON")
115-
if colors == "" {
116-
for _, b := range data {
117-
return string(b)
118-
}
119-
}
120-
s := ""
121-
for _, b := range data {
122-
if 0x21 <= b && b < 0x7F {
123-
s += textColor(string(b))
124-
} else {
125-
s += bytesColor(fmt.Sprintf("%02X", b))
126-
}
127-
}
128-
return s
129-
}

0 commit comments

Comments
 (0)