journal/tool/lib/JSONLib.jq

235 lines
6.6 KiB
Plaintext
Raw Normal View History

2024-11-10 13:00:43 +00:00
import "ansiLib" as ansiLib;
2024-11-10 16:25:21 +00:00
def colourEscapes:
2024-11-10 13:00:43 +00:00
def parseJQColours:
2024-11-10 16:25:21 +00:00
split(":") |
2024-11-10 13:00:43 +00:00
{
2024-11-10 16:25:21 +00:00
"null": (.[0] // "0"),
"false": (.[1] // "0"),
"true": (.[2] // "0"),
"number": (.[3] // "0"),
"string": (.[4] // "0"),
"array": (.[5] // "0"),
"object": (.[6] // "0"),
"objectKey": (.[7] // "0"),
2024-11-10 13:00:43 +00:00
};
def parseGOJQColours:
2024-11-10 16:25:21 +00:00
split(":") |
2024-11-10 13:00:43 +00:00
{
2024-11-10 16:25:21 +00:00
"null": (.[0] // "0"),
"false": (.[1] // "0"),
"true": (.[2] // "0"),
"number": (.[3] // "0"),
"string": (.[4] // "0"),
"objectKey": (.[5] // "0"),
"array": (.[6] // "0"),
"object": (.[7] // "0"),
2024-11-10 13:00:43 +00:00
};
def defaultColours:
if ($ENV["JQ_FLAVOUR"] | tostring | startswith("gojq")) then
"90:33:33:36:32:34;1" | parseGOJQColours
else
"0;90:0;39:0;39:0;39:0;32:1;39:1;39:1;34" | parseJQColours
end;
try (
if $ENV["JQ_COLORS"] != null then
$ENV["JQ_COLORS"] | parseJQColours
elif $ENV["GOJQ_COLORS"] != null then
$ENV["GOJQ_COLORS"] | parseGOJQColours
else
defaultColours
end
) catch (
defaultColours
);
2024-11-10 16:25:21 +00:00
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"
elif $valueType == "boolean" then
if $value then "true" else "false" end
elif $valueType == "number" then
$value | tostring
elif $valueType == "string" then
$value | tojson
elif $valueType == "array" then
($value | length) as $numValues |
2024-11-10 13:00:43 +00:00
2024-11-10 16:25:21 +00:00
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 |
2024-11-10 13:00:43 +00:00
2024-11-10 16:25:21 +00:00
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";
2024-11-10 13:00:43 +00:00
. as $value |
(. | type) as $valueType |
if $valueType == "null" then
2024-11-10 16:25:21 +00:00
colourText("null"; "null")
2024-11-10 13:00:43 +00:00
elif $valueType == "boolean" then
2024-11-10 16:25:21 +00:00
($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")
2024-11-10 13:00:43 +00:00
else
2024-11-10 16:25:21 +00:00
colourText("{}"; "object")
2024-11-10 13:00:43 +00:00
end
2024-11-10 16:25:21 +00:00
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)
2024-11-10 13:00:43 +00:00
elif $valueType == "number" then
2024-11-10 16:25:21 +00:00
colourText($value | tostring; "number")
2024-11-10 13:00:43 +00:00
elif $valueType == "string" then
2024-11-10 16:25:21 +00:00
colourText($value | tojson; "string")
2024-11-10 13:00:43 +00:00
elif $valueType == "array" then
($value | length) as $numValues |
2024-11-10 16:25:21 +00:00
if $numValues > 0 then
colourText("["; "array") +
$currentIndentDepthNewline +
([
$value[] |
$nextDepthIndent +
_encodeJSONValueColourPretty($indent; $currentDepth + 1)
] | join("," + $currentIndentDepthNewline)) +
$currentIndentDepthNewline +
$currentIndentDepthIndent +
colourText("]"; "array")
else
colourText("[]"; "array")
end
2024-11-10 13:00:43 +00:00
elif $valueType == "object" then
($value | keys | length) as $numKeys |
2024-11-10 16:25:21 +00:00
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
2024-11-10 13:00:43 +00:00
end;
2024-11-10 16:25:21 +00:00
2024-11-10 13:00:43 +00:00
def encodeJSON($withColour; $indent):
2024-11-10 16:25:51 +00:00
if ($withColour == false) and ($indent == 0) then
2024-11-10 16:25:21 +00:00
tojson
else
if $withColour then
if $indent > 0 then
_encodeJSONValueColourPretty($indent; 0)
else
_encodeJSONValueColourCompact
end
else
_encodeJSONValuePlainPretty($indent; 0)
end
end;
2024-11-10 13:00:43 +00:00