-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.go
113 lines (86 loc) · 2.81 KB
/
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
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
package print
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/fatih/color"
)
func Struct(strct interface{}) {
argValue := reflect.ValueOf(strct)
if argValue.Kind() != reflect.Struct {
Error("Error: Argument is not a struct")
return
}
jsonBytes, err := json.MarshalIndent(strct, "", " ")
if err != nil {
fmt.Println("Error:", err)
}
// Convert the indented JSON bytes to a string and replace tabs with spaces
indentedJSON := strings.ReplaceAll(string(jsonBytes), "\t", " ")
fmt.Println(indentedJSON)
}
func Structc(strct interface{}) {
argValue := reflect.ValueOf(strct)
if argValue.Kind() != reflect.Struct {
Error("Error: Argument is not a struct")
return
}
jsonBytes, err := json.MarshalIndent(strct, "", " ")
if err != nil {
fmt.Println("Error:", err)
}
// Convert the indented JSON bytes to a string and replace tabs with spaces
indentedJSON := strings.ReplaceAll(string(jsonBytes), "\t", " ")
// Create color functions
red := color.New(color.FgRed).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()
// Colorize the keys and values in JSON
colorized := colorizeJSON(indentedJSON, red, green)
// Print the colorized JSON
fmt.Println(colorized)
}
func Slice[T any](s []T) {
// Convert the slice values to strings
strValues := make([]string, 0)
for _, value := range s {
strValues = append(strValues, fmt.Sprint(value))
}
typeOfSlice := fmt.Sprintf("%T", s)
// Concatenate the values with a space separator and add brackets
result := typeOfSlice + "{ " + strings.Join(strValues, ", ") + " }"
// Print the concatenated string
fmt.Println(result)
}
func colorizeJSON(input string, keyColorFunc, valueColorFunc func(a ...interface{}) string) string {
// Split the input into lines
lines := strings.Split(input, "\n")
// Iterate through each line
for i, line := range lines {
// Find the index of the colon character
colonIndex := strings.Index(line, ":")
// If colon exists and it's not the last character
if colonIndex > 0 && colonIndex < len(line)-1 {
// Colorize the key before the colon and the value after the colon
// Colorize the key using the keyColorFunc
colorizedKey := keyColorFunc(line[:colonIndex]) + line[colonIndex:]
// Find the colon index in the colorized key
colonIndex = strings.Index(colorizedKey, ":")
// Colorize the value using the valueColorFunc
colorizedValue := colorizedKey[:colonIndex+1] + valueColorFunc(colorizedKey[colonIndex+1:])
// Update the line with the colorized value
lines[i] = colorizedValue
}
}
// Join the lines back into a string and return
return strings.Join(lines, "\n")
}
func Error(error string) {
redColor := "\033[31m"
resetColor := "\033[0m"
if CheckString(error) {
fmt.Println(redColor + error + resetColor)
} else {
fmt.Println("Argument is not a string")
}
}