Skip to content

Commit c8c912c

Browse files
authored
jc: add completions and README (#1144)
<img width="631" height="600" alt="image" src="https://github.com/user-attachments/assets/a96ed6f7-eab1-4265-93c0-8bd478c13ba4" /> Errors: <img width="632" height="210" alt="image" src="https://github.com/user-attachments/assets/04b9ac02-6c2b-4f7f-9da3-929fc97efecb" />
1 parent ba9a4c1 commit c8c912c

File tree

2 files changed

+102
-17
lines changed

2 files changed

+102
-17
lines changed

modules/jc/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# jc (JSON converter)
2+
3+
jc converts the output of many commands, file-types, and strings to JSON or YAML
4+
5+
This module provides a wrapper around the `jc` command line tool and
6+
automatically parses its output into a structured data format.
7+
8+
## Example
9+
10+
```nu
11+
> df | jc --df
12+
13+
┌─#─┬───filesystem───┬─1k_blocks─┬───used────┬─available─┬─────────mounted_on─────────┬─use_percent─┬─capacity_percent─┐
14+
│ 0 │ /dev/disk3s1s1 │ 482797652 │ 368026060 │ 114771592 │ / │ 77 │ 24 │
15+
│ 1 │ /dev/disk3s6 │ 482797652 │ 368026060 │ 114771592 │ /System/Volumes/VM │ 77 │ 24 │
16+
│ 2 │ /dev/disk3s2 │ 482797652 │ 368026060 │ 114771592 │ /System/Volumes/Preboot │ 77 │ 24 │
17+
│ 3 │ /dev/disk3s4 │ 482797652 │ 368026060 │ 114771592 │ /System/Volumes/Update │ 77 │ 24 │
18+
│ 4 │ /dev/disk1s2 │ 512000 │ 23052 │ 488948 │ /System/Volumes/xarts │ 5 │ 96 │
19+
│ 5 │ /dev/disk1s1 │ 512000 │ 23052 │ 488948 │ /System/Volumes/iSCPreboot │ 5 │ 96 │
20+
│ 6 │ /dev/disk1s3 │ 512000 │ 23052 │ 488948 │ /System/Volumes/Hardware │ 5 │ 96 │
21+
│ 7 │ /dev/disk3s7 │ 482797652 │ 368026060 │ 114771592 │ /nix │ 77 │ 24 │
22+
│ 8 │ /dev/disk4 │ 524288 │ 12316 │ 511972 │ /private/var/run/agenix.d │ 3 │ 98 │
23+
└───┴────────────────┴───────────┴───────────┴───────────┴────────────────────────────┴─────────────┴──────────────────┘
24+
```
25+
26+
## Installation
27+
28+
1. Install the `jc` command line:
29+
<https://kellyjonbrazil.github.io/jc/#installation>
30+
31+
2. Source this module in your `config.nu`:
32+
```nu
33+
use modules/jc
34+
```

modules/jc/mod.nu

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,79 @@
1-
# Run `jc` (Json Converter)
2-
#
3-
# This module provides a wrapper around the `jc` command line tool and automatically
4-
# parses its output into a structured data format.
5-
#
6-
# Dependencies:
7-
# * `jc`
8-
#
9-
# Installation:
10-
# 1. Install the `jc` command line: https://kellyjonbrazil.github.io/jc/#installation
11-
# 2. Import this module in your `config.nu`: `import ~/.local/share/nu_scripts/modules/jc/`
12-
export def --wrapped main [...args]: [any -> table, any -> record, any -> string] {
13-
let run = (^jc ...$args | complete)
1+
def --env "nu-complete jc" [commandline: string] {
2+
let stor = stor open
3+
4+
if $stor.jc_completions? == null {
5+
stor create --table-name jc_completions --columns { value: str, description: str, is_flag: bool }
6+
}
7+
8+
if $stor.jc_completions_ran? == null {
9+
stor create --table-name jc_completions_ran --columns { _: bool }
10+
}
11+
12+
if $stor.jc_completions_ran == [] { try {
13+
let about = ^jc --about
14+
| from json
15+
16+
let magic = $about
17+
| get parsers
18+
| each { { value: $in.magic_commands?, description: $in.description } }
19+
| where value != null
20+
| flatten
21+
22+
let options = $about
23+
| get parsers
24+
| select argument description
25+
| rename value description
26+
27+
let inherent = ^jc --help
28+
| lines
29+
| split list "" # Group with empty lines as boundary.
30+
| where { $in.0? == "Options:" } | get 0 # Get the first section that starts with "Options:"
31+
| skip 1 # Remove header
32+
| each { str trim }
33+
| parse "{short}, {long} {description}"
34+
| update description { str trim }
35+
| each {|record|
36+
[[value, description];
37+
[$record.short, $record.description],
38+
[$record.long, $record.description],
39+
]
40+
}
41+
| flatten
42+
43+
for entry in $magic {
44+
stor insert --table-name jc_completions --data-record ($entry | insert is_flag false)
45+
}
46+
47+
for entry in ($options ++ $inherent) {
48+
stor insert --table-name jc_completions --data-record ($entry | insert is_flag true)
49+
}
50+
51+
stor insert --table-name jc_completions_ran --data-record { _: true }
52+
} }
53+
54+
if ($commandline | str contains "-") {
55+
$stor.jc_completions
56+
} else {
57+
$stor.jc_completions
58+
| where is_flag == 0
59+
} | select value description
60+
}
61+
62+
# Run `jc` (JSON Converter).
63+
export def --wrapped main [...arguments: string@"nu-complete jc"]: [any -> table, any -> record, any -> string] {
64+
let run = ^jc ...$arguments | complete
1465

1566
if $run.exit_code != 0 {
1667
error make {
17-
msg: $run.stderr,
68+
msg: "jc exection failed"
1869
label: {
19-
text: "jc execution failed",
20-
span: (metadata $args).span
70+
text: ($run.stderr | str replace "jc:" "" | str replace "Error -" "" | str trim)
71+
span: (metadata $arguments).span
2172
}
2273
}
2374
}
2475

25-
if '--help' in $args or '-h' in $args {
76+
if "--help" in $arguments or "-h" in $arguments {
2677
$run.stdout
2778
} else {
2879
$run.stdout | from json

0 commit comments

Comments
 (0)