mirror of
https://github.com/Open-Argon/argon-v3.git
synced 2025-12-06 00:46:07 +00:00
colourise the terminal
This commit is contained in:
@@ -7,34 +7,68 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func anyToArgon(x any, quote bool, simplify bool, depth int, indent int) string {
|
||||
func anyToArgon(x any, quote bool, simplify bool, depth int, indent int, color bool, plain int) string {
|
||||
output := []string{}
|
||||
maybenewline := ""
|
||||
if plain == 1 {
|
||||
maybenewline = "\n"
|
||||
}
|
||||
if depth == 0 {
|
||||
return "(...)"
|
||||
if color {
|
||||
output = append(output, "\x1b[38;5;240m")
|
||||
}
|
||||
output = append(output, "(...)")
|
||||
if color {
|
||||
output = append(output, "\x1b[0m")
|
||||
}
|
||||
return strings.Join(output, "")
|
||||
}
|
||||
switch x := x.(type) {
|
||||
case string:
|
||||
if !quote {
|
||||
return x
|
||||
output = append(output, x)
|
||||
break
|
||||
}
|
||||
if color {
|
||||
output = append(output, "\x1b[33;5;240m")
|
||||
}
|
||||
output = append(output, strconv.Quote(x))
|
||||
if color {
|
||||
output = append(output, "\x1b[0m")
|
||||
}
|
||||
return strconv.Quote(x)
|
||||
case number:
|
||||
if color {
|
||||
output = append(output, "\x1b[34;5;240m")
|
||||
}
|
||||
num, _ := x.Float64()
|
||||
if math.IsNaN(num) {
|
||||
return "NaN"
|
||||
output = append(output, "NaN")
|
||||
} else if math.IsInf(num, 1) {
|
||||
return "infinity"
|
||||
output = append(output, "infinity")
|
||||
} else if math.IsInf(num, -1) {
|
||||
return "-infinity"
|
||||
output = append(output, "-infinity")
|
||||
} else {
|
||||
if simplify {
|
||||
return numberToString(x, 0, true)
|
||||
}
|
||||
return numberToString(x, 0, false)
|
||||
output = append(output, numberToString(x, 0, simplify))
|
||||
}
|
||||
if color {
|
||||
output = append(output, "\x1b[0m")
|
||||
}
|
||||
case bool:
|
||||
return strconv.FormatBool(x)
|
||||
if color {
|
||||
output = append(output, "\x1b[35;5;240m")
|
||||
}
|
||||
output = append(output, strconv.FormatBool(x))
|
||||
if color {
|
||||
output = append(output, "\x1b[0m")
|
||||
}
|
||||
case nil:
|
||||
return "null"
|
||||
if color {
|
||||
output = append(output, "\x1b[31;5;240m")
|
||||
}
|
||||
output = append(output, "null")
|
||||
if color {
|
||||
output = append(output, "\x1b[0m")
|
||||
}
|
||||
case ArMap:
|
||||
keys := make([]any, len(x))
|
||||
|
||||
@@ -45,16 +79,29 @@ func anyToArgon(x any, quote bool, simplify bool, depth int, indent int) string
|
||||
}
|
||||
output := []string{}
|
||||
for _, key := range keys {
|
||||
output = append(output, anyToArgon(key, true, true, depth, indent+1)+": "+anyToArgon(x[key], true, true, depth-1, indent+1))
|
||||
output = append(output, anyToArgon(key, true, true, depth, (indent+1)*plain, color, plain)+": "+anyToArgon(x[key], true, true, depth-1, indent+1, color, plain))
|
||||
}
|
||||
return "{\n" + (strings.Repeat(" ", indent+1)) + strings.Join(output, ",\n"+(strings.Repeat(" ", indent+1))) + "\n" + (strings.Repeat(" ", indent)) + "}"
|
||||
return "{" + maybenewline + (strings.Repeat(" ", (indent+1)*plain)) + strings.Join(output, ","+maybenewline+(strings.Repeat(" ", (indent+1)*plain))) + maybenewline + (strings.Repeat(" ", indent*plain)) + "}"
|
||||
case builtinFunc:
|
||||
return "<builtin function " + x.name + ">"
|
||||
if color {
|
||||
output = append(output, "\x1b[38;5;240m")
|
||||
}
|
||||
output = append(output, "<builtin function "+x.name+">")
|
||||
if color {
|
||||
output = append(output, "\x1b[0m")
|
||||
}
|
||||
case Callable:
|
||||
return "<function " + x.name + ">"
|
||||
if color {
|
||||
output = append(output, "\x1b[38;5;240m")
|
||||
}
|
||||
output = append(output, "<function "+x.name+">")
|
||||
if color {
|
||||
output = append(output, "\x1b[0m")
|
||||
}
|
||||
case ArClass:
|
||||
return anyToArgon(x.value, false, true, depth-1, indent+1)
|
||||
return anyToArgon(x.value, quote, simplify, depth, indent, color, plain)
|
||||
default:
|
||||
return fmt.Sprint(x)
|
||||
}
|
||||
return strings.Join(output, "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user