journal/tool/utils.jq

121 lines
3.7 KiB
Plaintext
Raw Normal View History

2024-11-07 08:58:38 +00:00
def debugLog($target; $value):
if
($ENV["JQ_DEBUG"] == $target)
or
($target == "*")
or
($ENV["JQ_DEBUG"] | split(",") | any(. as $debugTarget | $target | startswith($debugTarget | split("*")[0])))
then
debug({$target, $value})
end;
def assert(cond; $msg): if cond then . else error($msg) end;
def assert($loc; cond; $msg): assert(cond; "\($loc.file):\($loc.line): " + $msg);
def titleCase:
[splits("\\b") | select(length>0)]
| map((.[:1]|ascii_upcase) + (.[1:] |ascii_downcase))
| join("");
def lpad(string;len;fill):
if len == 0 then string else (fill * len)[0:len] + string end;
def rpad(string;len;fill):
if len == 0 then string else string + (fill * len)[0:len] end;
def firstN($n): .[:$n];
# only use when // does not suffice
def ifNullDefault($value; $default):
if $value != null then
$value
else
$default
end;
def orderedUnique:
(reduce .[] as $value ([];
if
(. | any(index($value)) | not)
then . += [$value]
else . end
));
def printPrettyTable($title; $columnTitles; $rows):
if [$rows[] | length] | unique | length > 1 then error("non-even number of columns") end |
$title | if . == null then "" end | . as $title |
" | " as $columnSeperator |
([$rows[] | length] | unique[0] // 0) as $columns |
[$rows[] as $columns | [$columns[] | length]] as $columnLengths |
(reduce $columnLengths[] as $lengths ([range($columns) | 0];
[range($columns) as $columnNumber | [.[$columnNumber], $lengths[$columnNumber]] | max]
)) as $maxColumnLengths |
(reduce $columnLengths[] as $lengths ($maxColumnLengths;
[range($columns) as $columnNumber | [.[$columnNumber], ($columnTitles[$columnNumber] | length)] | max]
)) as $maxColumnLengths |
(reduce range($columns) as $columnNumber ([];
$columnTitles[$columnNumber] as $title |
. += [
if $columnNumber == 0 then
lpad($title; $maxColumnLengths[$columnNumber] - ($title | length) ; " ")
else
rpad($title; $maxColumnLengths[$columnNumber] - ($title | length) ; " ")
end
]
)) | join($columnSeperator) as $columnTitleRow |
(reduce $rows[] as $row ([];
. += [(reduce range($columns) as $columnNumber ([]; . += [
$row[$columnNumber] as $column |
if $columnNumber == 0 then
lpad($column; $maxColumnLengths[$columnNumber] - ($column | length) ; " ")
else
rpad($column; $maxColumnLengths[$columnNumber] - ($column | length) ; " ")
end
]))]
)) as $rows |
$title | length as $titleLength |
((($columns - 1) * ($columnSeperator | length)) + ($maxColumnLengths | add)) as $maxRowLength |
[
$maxRowLength,
$titleLength
] | max as $maxLength |
# Pad title to be centered to maximum row length
$title | if $titleLength < $maxRowLength then
lpad($title; ($maxLength - $titleLength) / 2; " ")
end | . as $title |
# if title is longer than maximum row length, pad the first row the difference between them
$rows | if $titleLength > $maxRowLength then
(reduce .[] as $row ([];
. += [
[
lpad($row[0]; $titleLength - $maxRowLength; " ")
] + $row[1:]
]
))
end | . as $rows |
($rows | map(join($columnSeperator))) as $rows |
if $title != null then
[
$title,
([range($maxLength) | "-"] | join("")),
$columnTitleRow,
([range($maxLength) | "-"] | join("")),
$rows
] | flatten | join("\n") + "\n"
else
[
$columnTitleRow,
$rows
] | flatten | join("\n") + "\n"
end;