feat: jq just became a 1M times cooler!
Some checks failed
Test action / kaas (push) Failing after 1s
Some checks failed
Test action / kaas (push) Failing after 1s
This commit is contained in:
parent
c16cb15c10
commit
0fa3b79bd9
9 changed files with 568 additions and 9 deletions
59
.jq/table.jq
Normal file
59
.jq/table.jq
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import "format" as _ {search:"./"};
|
||||
|
||||
def n_max(limit):
|
||||
if . > limit then limit else . end;
|
||||
|
||||
def n_min(limit):
|
||||
if . < limit then limit else . end;
|
||||
|
||||
def pad_right(width):
|
||||
(. | tostring) as $s
|
||||
| ($s | length) as $l
|
||||
| ((width - $l) | n_min(0)) as $w
|
||||
| ($s + (" " * $w));
|
||||
|
||||
def to_cells(sizes; fn):
|
||||
to_entries
|
||||
| map(
|
||||
(sizes[.key]) as $size
|
||||
| (" " + .value)
|
||||
| pad_right($size + 2)
|
||||
| fn // .
|
||||
);
|
||||
|
||||
def to_cells(sizes): to_cells(sizes; null);
|
||||
|
||||
def to_line(left; joiner; right):
|
||||
[left, .[1], (.[1:] | map([joiner, .]) ), right] | flatten | join("");
|
||||
|
||||
def to_table(data; header_callback; cell_callback):
|
||||
(data[0] | to_entries | map(.key)) as $keys
|
||||
| ([$keys]) as $header
|
||||
| (data | map(to_entries | map(.value))) as $rows
|
||||
| ($header + $rows) as $cells
|
||||
| (
|
||||
$keys # Use keys so that we have an array of the correct size
|
||||
| to_entries
|
||||
| map(
|
||||
(.key) as $i
|
||||
| $cells
|
||||
| map(.[$i] | length)
|
||||
| max
|
||||
)
|
||||
) as $column_sizes
|
||||
| (
|
||||
[
|
||||
($column_sizes | map("═" * (. + 2)) | to_line("╔"; "╤"; "╗")),
|
||||
($keys | to_cells($column_sizes; header_callback) | to_line("║"; "│"; "║")),
|
||||
($rows | map([
|
||||
($column_sizes | map("─" * (. + 2)) | to_line("╟"; "┼"; "╢")),
|
||||
(. | to_cells($column_sizes; cell_callback) | to_line("║"; "│"; "║"))
|
||||
])),
|
||||
($column_sizes | map("═" * (. + 2)) | to_line("╚"; "╧"; "╝"))
|
||||
]
|
||||
| flatten
|
||||
| join("\n")
|
||||
);
|
||||
|
||||
def to_table(data; header_callback): to_table(data; header_callback; null);
|
||||
def to_table(data): to_table(data; _::style(_::BOLD); null);
|
||||
Loading…
Add table
Add a link
Reference in a new issue