summaryrefslogtreecommitdiffstats
path: root/internal/utils/output.go
diff options
context:
space:
mode:
authors <[email protected]>2025-11-07 11:50:47 -0500
committers <[email protected]>2025-11-07 11:50:47 -0500
commit7d63867f365163f149db5d768c71f518f9eaf711 (patch)
treef3f4856cf82f57286dee5bec23fae0c5f70bd1ee /internal/utils/output.go
parent5821aaa32e7ff2a2b2935bee712c2684907a3451 (diff)
downloaddborg-7d63867f365163f149db5d768c71f518f9eaf711.tar.gz
dborg-7d63867f365163f149db5d768c71f518f9eaf711.zip
refactor: extract json output formatting to utils.PrintJSON and add colorized json output with new dns tld command
Diffstat (limited to 'internal/utils/output.go')
-rw-r--r--internal/utils/output.go113
1 files changed, 112 insertions, 1 deletions
diff --git a/internal/utils/output.go b/internal/utils/output.go
index 3f2347c..e8e97bc 100644
--- a/internal/utils/output.go
+++ b/internal/utils/output.go
@@ -1,18 +1,129 @@
package utils
import (
+ "bytes"
"encoding/json"
"fmt"
"os"
"text/tabwriter"
)
+const (
+ colorReset = "\033[0m"
+ colorKey = "\033[36m"
+ colorString = "\033[32m"
+ colorNumber = "\033[33m"
+ colorBool = "\033[35m"
+ colorNull = "\033[90m"
+ colorBrace = "\033[37m"
+)
+
+func colorizeJSON(data []byte) string {
+ if !isTerminal() {
+ return string(data)
+ }
+
+ var result bytes.Buffer
+ var inString bool
+ var isKey bool
+ var depth int
+
+ for i := 0; i < len(data); i++ {
+ c := data[i]
+
+ switch c {
+ case '"':
+ if i > 0 && data[i-1] != '\\' {
+ inString = !inString
+ if inString {
+ if i > 0 && (data[i-1] == '{' || data[i-1] == ',' || data[i-1] == '[' || data[i-1] == '\n' || data[i-1] == ' ') {
+ isKey = true
+ result.WriteString(colorKey)
+ } else {
+ result.WriteString(colorString)
+ }
+ } else {
+ result.WriteByte(c)
+ result.WriteString(colorReset)
+ if isKey && i+1 < len(data) && data[i+1] == ':' {
+ isKey = false
+ }
+ continue
+ }
+ }
+ result.WriteByte(c)
+
+ case '{', '}', '[', ']':
+ if !inString {
+ result.WriteString(colorBrace)
+ result.WriteByte(c)
+ result.WriteString(colorReset)
+ if c == '{' || c == '[' {
+ depth++
+ } else {
+ depth--
+ }
+ } else {
+ result.WriteByte(c)
+ }
+
+ case 't', 'f':
+ if !inString && i+3 < len(data) {
+ word := string(data[i:min(i+5, len(data))])
+ if word[:4] == "true" || word[:5] == "false" {
+ result.WriteString(colorBool)
+ if word[:4] == "true" {
+ result.WriteString("true")
+ i += 3
+ } else {
+ result.WriteString("false")
+ i += 4
+ }
+ result.WriteString(colorReset)
+ } else {
+ result.WriteByte(c)
+ }
+ } else {
+ result.WriteByte(c)
+ }
+
+ case 'n':
+ if !inString && i+3 < len(data) && string(data[i:i+4]) == "null" {
+ result.WriteString(colorNull)
+ result.WriteString("null")
+ result.WriteString(colorReset)
+ i += 3
+ } else {
+ result.WriteByte(c)
+ }
+
+ case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
+ if !inString {
+ result.WriteString(colorNumber)
+ for i < len(data) && (data[i] >= '0' && data[i] <= '9' || data[i] == '.' || data[i] == '-' || data[i] == 'e' || data[i] == 'E' || data[i] == '+') {
+ result.WriteByte(data[i])
+ i++
+ }
+ i--
+ result.WriteString(colorReset)
+ } else {
+ result.WriteByte(c)
+ }
+
+ default:
+ result.WriteByte(c)
+ }
+ }
+
+ return result.String()
+}
+
func PrintJSON(data any) error {
output, err := json.MarshalIndent(data, "", " ")
if err != nil {
return fmt.Errorf("failed to format JSON: %w", err)
}
- fmt.Println(string(output))
+ fmt.Println(colorizeJSON(output))
return nil
}