Skip to content

Commit 41b1e95

Browse files
committed
jc: add completions and README
1 parent ba9a4c1 commit 41b1e95

File tree

2 files changed

+79
-17
lines changed

2 files changed

+79
-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+
source ~/path/to/jc/mod.rs
34+
```

modules/jc/mod.nu

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,56 @@
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" [] {
2+
if $env.__NU_COMPLETE_JC? != null {
3+
return $env.__NU_COMPLETE_JC
4+
}
5+
6+
let options = try {
7+
let options = ^jc --help
8+
| collect
9+
| parse "{_}Parsers:\n{_}\n\nOptions:\n{inherent}\n\nSlice:{_}"
10+
| get 0
11+
12+
let parsers = ^jc --about
13+
| from json
14+
| get parsers
15+
| select argument description
16+
| rename value description
17+
18+
let inherent = $options.inherent
19+
| lines
20+
| parse " {short}, {long} {description}"
21+
| update description { str trim }
22+
| each {|record|
23+
[[value, description];
24+
[$record.short, $record.description],
25+
[$record.long, $record.description]]
26+
}
27+
| flatten
28+
29+
$parsers ++ $inherent
30+
} catch {
31+
[]
32+
}
33+
34+
$env.__NU_COMPLETE_JC = $options
35+
36+
$options
37+
}
38+
39+
# Run `jc` (JSON Converter).
40+
export def --wrapped jc [...arguments: string@"nu-complete jc"]: [any -> table, any -> record, any -> string] {
41+
let run = ^jc ...$arguments | complete
1442

1543
if $run.exit_code != 0 {
1644
error make {
17-
msg: $run.stderr,
45+
msg: "jc exection failed"
1846
label: {
19-
text: "jc execution failed",
20-
span: (metadata $args).span
47+
text: ($run.stderr | str replace "jc:" "" | str replace "Error -" "" | str trim)
48+
span: (metadata $arguments).span
2149
}
2250
}
2351
}
2452

25-
if '--help' in $args or '-h' in $args {
53+
if "--help" in $arguments or "-h" in $arguments {
2654
$run.stdout
2755
} else {
2856
$run.stdout | from json

0 commit comments

Comments
 (0)