update
This commit is contained in:
parent
cd06938720
commit
ed0450c914
|
@ -1,30 +1,30 @@
|
|||
import "ansiLib" as ansiLib;
|
||||
|
||||
def colorEscapes:
|
||||
def colourEscapes:
|
||||
def parseJQColours:
|
||||
(. | split(":")) |
|
||||
split(":") |
|
||||
{
|
||||
"null": .[0],
|
||||
"false": .[1],
|
||||
"true": .[2],
|
||||
"number": .[3],
|
||||
"string": .[4],
|
||||
"array": .[5],
|
||||
"object": .[6],
|
||||
"objectKey": .[7],
|
||||
"null": (.[0] // "0"),
|
||||
"false": (.[1] // "0"),
|
||||
"true": (.[2] // "0"),
|
||||
"number": (.[3] // "0"),
|
||||
"string": (.[4] // "0"),
|
||||
"array": (.[5] // "0"),
|
||||
"object": (.[6] // "0"),
|
||||
"objectKey": (.[7] // "0"),
|
||||
};
|
||||
|
||||
def parseGOJQColours:
|
||||
(. | split(":")) |
|
||||
split(":") |
|
||||
{
|
||||
"null": .[0],
|
||||
"false": .[1],
|
||||
"true": .[2],
|
||||
"number": .[3],
|
||||
"string": .[4],
|
||||
"objectKey": .[5],
|
||||
"array": .[6],
|
||||
"object": .[7],
|
||||
"null": (.[0] // "0"),
|
||||
"false": (.[1] // "0"),
|
||||
"true": (.[2] // "0"),
|
||||
"number": (.[3] // "0"),
|
||||
"string": (.[4] // "0"),
|
||||
"objectKey": (.[5] // "0"),
|
||||
"array": (.[6] // "0"),
|
||||
"object": (.[7] // "0"),
|
||||
};
|
||||
def defaultColours:
|
||||
if ($ENV["JQ_FLAVOUR"] | tostring | startswith("gojq")) then
|
||||
|
@ -45,66 +45,191 @@ def colorEscapes:
|
|||
defaultColours
|
||||
);
|
||||
|
||||
def encodeJSONValue($withColour; $escapes; $indent; $currentDepth):
|
||||
def colourText($kind; $escapes):
|
||||
. as $text |
|
||||
ansiLib::CSR + $escapes[$kind] + "m" + $text + ansiLib::CSR + "0m";
|
||||
|
||||
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;
|
||||
def _encodeJSONValuePlainPretty($indent; $currentDepth):
|
||||
(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
|
||||
"null" | maybeColourText($withColour; "null"; $escapes)
|
||||
"null"
|
||||
elif $valueType == "boolean" then
|
||||
if $value then
|
||||
"true" | maybeColourText($withColour; "true"; $escapes)
|
||||
else
|
||||
"false" | maybeColourText($withColour; "false"; $escapes)
|
||||
end
|
||||
if $value then "true" else "false" end
|
||||
elif $valueType == "number" then
|
||||
$value | tostring | maybeColourText($withColour; "number"; $escapes)
|
||||
$value | tostring
|
||||
elif $valueType == "string" then
|
||||
$value | tojson | maybeColourText($withColour; "string"; $escapes)
|
||||
$value | tojson
|
||||
elif $valueType == "array" then
|
||||
($value | length) as $numValues |
|
||||
("[" | maybeColourText($withColour; "array"; $escapes)) +
|
||||
if $numValues > 0 then maybeNewline($indent) else "" end +
|
||||
([
|
||||
$value[] |
|
||||
maybeIndent($indent; $currentDepth) +
|
||||
encodeJSONValue($withColour; $escapes; $indent; $currentDepth + 1)
|
||||
] | join("," + maybeNewline($indent))) +
|
||||
if $numValues > 0 then maybeNewline($indent) else "" end +
|
||||
if $numValues > 0 then maybeIndent($indent; $currentDepth-1) else "" end +
|
||||
("]" | maybeColourText($withColour; "array"; $escapes))
|
||||
|
||||
if $numValues > 0 then
|
||||
"[" +
|
||||
$currentIndentDepthNewline +
|
||||
([
|
||||
$value[] |
|
||||
$nextDepthIndent +
|
||||
_encodeJSONValuePlainPretty($indent; $currentDepth + 1)
|
||||
] | join("," + $currentIndentDepthNewline)) +
|
||||
$currentIndentDepthNewline +
|
||||
$currentIndentDepthIndent +
|
||||
"]"
|
||||
else
|
||||
"[]"
|
||||
end
|
||||
elif $valueType == "object" then
|
||||
($value | keys | length) as $numKeys |
|
||||
("{" | maybeColourText($withColour; "object"; $escapes)) +
|
||||
if $numKeys > 0 then maybeNewline($indent) else "" end +
|
||||
([
|
||||
$value | keys | sort[] |
|
||||
. as $objectKey |
|
||||
$value[$objectKey] as $objectValue |
|
||||
maybeIndent($indent; $currentDepth) +
|
||||
($objectKey | tojson | maybeColourText($withColour; "objectKey"; $escapes)) +
|
||||
":" + maybeSpace($indent) +
|
||||
($objectValue | encodeJSONValue($withColour; $escapes; $indent; $currentDepth + 1))
|
||||
] | join("," + maybeNewline($indent))) +
|
||||
if $numKeys > 0 then maybeNewline($indent) else "" end +
|
||||
if $numKeys > 0 then maybeIndent($indent; $currentDepth-1) else "" end +
|
||||
("}" | maybeColourText($withColour; "object"; $escapes))
|
||||
|
||||
if $numKeys > 0 then
|
||||
"{" +
|
||||
$currentIndentDepthNewline +
|
||||
([
|
||||
$value | keys | sort[] |
|
||||
. as $objectKey |
|
||||
$value[$objectKey] as $objectValue |
|
||||
|
||||
$nextDepthIndent +
|
||||
($objectKey | tojson) +
|
||||
":" + $currentIndentDepthSpace +
|
||||
($objectValue | _encodeJSONValuePlainPretty($indent; $currentDepth + 1))
|
||||
] | join("," + $currentIndentDepthNewline)) +
|
||||
$currentIndentDepthNewline +
|
||||
$currentIndentDepthIndent +
|
||||
"}"
|
||||
else
|
||||
"{}"
|
||||
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):
|
||||
. | 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;
|
||||
|
||||
|
||||
|
Loading…
Reference in a new issue