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
34
.jq/format.jq
Normal file
34
.jq/format.jq
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
def RESET: "0";
|
||||
def BOLD: "1";
|
||||
def DIM: "2";
|
||||
def ITALIC: "3";
|
||||
def UNDERLINE: "4";
|
||||
def BLINKING: "5";
|
||||
def INVERSE: "7";
|
||||
def HIDDEN: "8";
|
||||
def STRIKETHROUGH: "9";
|
||||
def RESET_FONT: "22";
|
||||
|
||||
def BLACK: 0;
|
||||
def RED: 1;
|
||||
def GREEN: 2;
|
||||
def YELLOW: 3;
|
||||
def BLUE: 4;
|
||||
def MAGENTA: 5;
|
||||
def CYAN: 6;
|
||||
def WHITE: 7;
|
||||
def DEFAULT: 9;
|
||||
|
||||
def foreground(color): 30 + color;
|
||||
def background(color): 40 + color;
|
||||
def bright(color): 60 + color;
|
||||
|
||||
def escape(options):
|
||||
(if ((options|type) == "array") then options else [options] end) as $o
|
||||
| "\u001b[\($o | map(tostring) | join(";"))m";
|
||||
|
||||
def style(options): escape(options) + . + escape([RESET]);
|
||||
|
||||
def to_title:
|
||||
(.|ascii_upcase) as $str
|
||||
| escape([BOLD, foreground(BLACK), background(WHITE)]) + " " + $str + " " + escape([RESET]);
|
||||
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