import "stringLib" as stringLib; 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 $numColumnseperator | def padRow($numColumns; $maxColumnLengths): . as $row | [ range($numColumns) as $columnNumber | $row[$columnNumber] as $content | if $columnNumber == 0 then stringLib::lpad($content; $maxColumnLengths[$columnNumber] - ($content | length) ; " ") else stringLib::rpad($content; $maxColumnLengths[$columnNumber] - ($content | length) ; " ") end ]; ([$rows[] | length] | unique[0]) as $numColumns | ([$columnTitles | length] | unique[0]) as $numColumnTitles | if $numColumns != $numColumnTitles then error("unequal title rows and columns") end | [$columnTitles[] | length] as $maxColumnLengths | (reduce $rows[] as $column ($maxColumnLengths; [ range($numColumns) as $columnNumber | [ .[$columnNumber], ($column[$columnNumber] | length) ] | max ] )) as $maxColumnLengths | $columnTitles | padRow($numColumns; $maxColumnLengths) | join($numColumnseperator) as $columnTitleRow | [$rows[] | padRow($numColumns; $maxColumnLengths)] as $rows | $title | length as $titleLength | ((($numColumns - 1) * ($numColumnseperator | length)) + ($maxColumnLengths | add)) as $maxRowLength | [ $maxRowLength, $titleLength ] | max as $maxLength | # Pad title to be centered to maximum row length $title | if $titleLength < $maxRowLength then stringLib::lpad($title; ($maxLength - $titleLength) / 2; " ") end | . as $title | # if title is longer than maximum row length, pad the first row the difference between them if $titleLength > $maxRowLength then [ $rows[] as $row | [ stringLib::lpad($row[0]; $titleLength - $maxRowLength; " ") ] + $row[1:] ] else $rows end | . as $rows | ($rows | map(join($numColumnseperator))) as $rows | if $title != "" then [ $title, ([range($maxLength) | "-"] | join("")), $columnTitleRow, ([range($maxLength) | "-"] | join("")), $rows ] | flatten | join("\n") + "\n" else [ $columnTitleRow, $rows ] | flatten | join("\n") + "\n" end;