This commit is contained in:
chaos 2024-11-10 16:25:21 +00:00
parent cd06938720
commit ed0450c914

View file

@ -1,30 +1,30 @@
import "ansiLib" as ansiLib; import "ansiLib" as ansiLib;
def colorEscapes: def colourEscapes:
def parseJQColours: def parseJQColours:
(. | split(":")) | split(":") |
{ {
"null": .[0], "null": (.[0] // "0"),
"false": .[1], "false": (.[1] // "0"),
"true": .[2], "true": (.[2] // "0"),
"number": .[3], "number": (.[3] // "0"),
"string": .[4], "string": (.[4] // "0"),
"array": .[5], "array": (.[5] // "0"),
"object": .[6], "object": (.[6] // "0"),
"objectKey": .[7], "objectKey": (.[7] // "0"),
}; };
def parseGOJQColours: def parseGOJQColours:
(. | split(":")) | split(":") |
{ {
"null": .[0], "null": (.[0] // "0"),
"false": .[1], "false": (.[1] // "0"),
"true": .[2], "true": (.[2] // "0"),
"number": .[3], "number": (.[3] // "0"),
"string": .[4], "string": (.[4] // "0"),
"objectKey": .[5], "objectKey": (.[5] // "0"),
"array": .[6], "array": (.[6] // "0"),
"object": .[7], "object": (.[7] // "0"),
}; };
def defaultColours: def defaultColours:
if ($ENV["JQ_FLAVOUR"] | tostring | startswith("gojq")) then if ($ENV["JQ_FLAVOUR"] | tostring | startswith("gojq")) then
@ -45,66 +45,191 @@ def colorEscapes:
defaultColours defaultColours
); );
def encodeJSONValue($withColour; $escapes; $indent; $currentDepth): def _encodeJSONValuePlainPretty($indent; $currentDepth):
def colourText($kind; $escapes): (if $indent > 0 then [range($indent * $currentDepth) | " "] | join("") else "" end) as $currentIndentDepthIndent |
. as $text | (if $indent > 0 then [range($indent * ($currentDepth + 1)) | " "] | join("") else "" end) as $nextDepthIndent |
ansiLib::CSR + $escapes[$kind] + "m" + $text + ansiLib::CSR + "0m"; (if $indent > 0 then "\n" else "" end) as $currentIndentDepthNewline |
(if $indent > 0 then " " else "" end) as $currentIndentDepthSpace |
def maybeColourText($withColour; $kind; $escapes): if $withColour then colourText($kind; $escapes) end;
def maybeIndent($indent; $currentDepth):
if $indent > 0 then [range($indent * $currentDepth) | " "] | join("") else "" end;
def maybeNewline($indent):
if $indent > 0 then "\n" else "" end;
def maybeSpace($indent):
if $indent > 0 then " " else "" end;
. as $value | . as $value |
(. | type) as $valueType | (. | type) as $valueType |
if $valueType == "null" then if $valueType == "null" then
"null" | maybeColourText($withColour; "null"; $escapes) "null"
elif $valueType == "boolean" then elif $valueType == "boolean" then
if $value then if $value then "true" else "false" end
"true" | maybeColourText($withColour; "true"; $escapes)
else
"false" | maybeColourText($withColour; "false"; $escapes)
end
elif $valueType == "number" then elif $valueType == "number" then
$value | tostring | maybeColourText($withColour; "number"; $escapes) $value | tostring
elif $valueType == "string" then elif $valueType == "string" then
$value | tojson | maybeColourText($withColour; "string"; $escapes) $value | tojson
elif $valueType == "array" then elif $valueType == "array" then
($value | length) as $numValues | ($value | length) as $numValues |
("[" | maybeColourText($withColour; "array"; $escapes)) +
if $numValues > 0 then maybeNewline($indent) else "" end + if $numValues > 0 then
"[" +
$currentIndentDepthNewline +
([ ([
$value[] | $value[] |
maybeIndent($indent; $currentDepth) + $nextDepthIndent +
encodeJSONValue($withColour; $escapes; $indent; $currentDepth + 1) _encodeJSONValuePlainPretty($indent; $currentDepth + 1)
] | join("," + maybeNewline($indent))) + ] | join("," + $currentIndentDepthNewline)) +
if $numValues > 0 then maybeNewline($indent) else "" end + $currentIndentDepthNewline +
if $numValues > 0 then maybeIndent($indent; $currentDepth-1) else "" end + $currentIndentDepthIndent +
("]" | maybeColourText($withColour; "array"; $escapes)) "]"
else
"[]"
end
elif $valueType == "object" then elif $valueType == "object" then
($value | keys | length) as $numKeys | ($value | keys | length) as $numKeys |
("{" | maybeColourText($withColour; "object"; $escapes)) +
if $numKeys > 0 then maybeNewline($indent) else "" end + if $numKeys > 0 then
"{" +
$currentIndentDepthNewline +
([ ([
$value | keys | sort[] | $value | keys | sort[] |
. as $objectKey | . as $objectKey |
$value[$objectKey] as $objectValue | $value[$objectKey] as $objectValue |
maybeIndent($indent; $currentDepth) +
($objectKey | tojson | maybeColourText($withColour; "objectKey"; $escapes)) + $nextDepthIndent +
":" + maybeSpace($indent) + ($objectKey | tojson) +
($objectValue | encodeJSONValue($withColour; $escapes; $indent; $currentDepth + 1)) ":" + $currentIndentDepthSpace +
] | join("," + maybeNewline($indent))) + ($objectValue | _encodeJSONValuePlainPretty($indent; $currentDepth + 1))
if $numKeys > 0 then maybeNewline($indent) else "" end + ] | join("," + $currentIndentDepthNewline)) +
if $numKeys > 0 then maybeIndent($indent; $currentDepth-1) else "" end + $currentIndentDepthNewline +
("}" | maybeColourText($withColour; "object"; $escapes)) $currentIndentDepthIndent +
"}"
else
"{}"
end
end; end;
def _encodeJSONValueColourCompact:
colourEscapes as $escapes |
def colourText($text; $kind):
ansiLib::CSR + $escapes[$kind] + "m" + $text + ansiLib::CSR + "0m";
. as $value |
(. | type) as $valueType |
if $valueType == "null" then
colourText("null"; "null")
elif $valueType == "boolean" then
($value | tostring) as $boolStr |
colourText($boolStr; $boolStr)
elif $valueType == "number" then
colourText($value | tostring; "number")
elif $valueType == "string" then
colourText($value | tojson; "string")
elif $valueType == "array" then
($value | length) as $numValues |
if $numValues > 0 then
colourText("["; "array") +
([
$value[] |
_encodeJSONValueColourCompact
] | join(",")) +
colourText("]"; "array")
else
colourText("[]"; "array")
end
elif $valueType == "object" then
($value | keys | length) as $numKeys |
if $numKeys > 0 then
colourText("{"; "object") +
([
$value | keys | sort[] |
. as $objectKey |
$value[$objectKey] as $objectValue |
colourText($objectKey | tojson; "objectKey") +
":" +
($objectValue | _encodeJSONValueColourCompact)
] | join(",")) +
colourText("}"; "object")
else
colourText("{}"; "object")
end
end;
def _encodeJSONValueColourPretty($indent; $currentDepth):
colourEscapes as $escapes |
def colourText($text; $kind):
ansiLib::CSR + $escapes[$kind] + "m" + $text + ansiLib::CSR + "0m";
(if $indent > 0 then [range($indent * $currentDepth) | " "] | join("") else "" end) as $currentIndentDepthIndent |
(if $indent > 0 then [range($indent * ($currentDepth + 1)) | " "] | join("") else "" end) as $nextDepthIndent |
(if $indent > 0 then "\n" else "" end) as $currentIndentDepthNewline |
(if $indent > 0 then " " else "" end) as $currentIndentDepthSpace |
. as $value |
(. | type) as $valueType |
if $valueType == "null" then
colourText("null"; "null")
elif $valueType == "boolean" then
($value | tostring) as $boolStr |
colourText($boolStr; $boolStr)
elif $valueType == "number" then
colourText($value | tostring; "number")
elif $valueType == "string" then
colourText($value | tojson; "string")
elif $valueType == "array" then
($value | length) as $numValues |
if $numValues > 0 then
colourText("["; "array") +
$currentIndentDepthNewline +
([
$value[] |
$nextDepthIndent +
_encodeJSONValueColourPretty($indent; $currentDepth + 1)
] | join("," + $currentIndentDepthNewline)) +
$currentIndentDepthNewline +
$currentIndentDepthIndent +
colourText("]"; "array")
else
colourText("[]"; "array")
end
elif $valueType == "object" then
($value | keys | length) as $numKeys |
if $numKeys > 0 then
colourText("{"; "object") +
$currentIndentDepthNewline +
([
$value | keys | sort[] |
. as $objectKey |
$value[$objectKey] as $objectValue |
$nextDepthIndent +
colourText($objectKey | tojson; "objectKey") +
":" + $currentIndentDepthSpace +
($objectValue | _encodeJSONValueColourPretty($indent; $currentDepth + 1))
] | join("," + $currentIndentDepthNewline)) +
$currentIndentDepthNewline +
$currentIndentDepthIndent +
colourText("}"; "object")
else
colourText("{}"; "object")
end
end;
def encodeJSON($withColour; $indent): def encodeJSON($withColour; $indent):
. | encodeJSONValue($withColour; colorEscapes; $indent // 0; 1); if ($withColour | not) and ($indent == 0) then
tojson
else
if $withColour then
if $indent > 0 then
_encodeJSONValueColourPretty($indent; 0)
else
_encodeJSONValueColourCompact
end
else
_encodeJSONValuePlainPretty($indent; 0)
end
end;