|
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 |
14 | 42 |
|
15 | 43 | if $run.exit_code != 0 { |
16 | 44 | error make { |
17 | | - msg: $run.stderr, |
| 45 | + msg: "jc exection failed" |
18 | 46 | 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 |
21 | 49 | } |
22 | 50 | } |
23 | 51 | } |
24 | 52 |
|
25 | | - if '--help' in $args or '-h' in $args { |
| 53 | + if "--help" in $arguments or "-h" in $arguments { |
26 | 54 | $run.stdout |
27 | 55 | } else { |
28 | 56 | $run.stdout | from json |
|
0 commit comments