|
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 |
14 | 65 |
|
15 | 66 | if $run.exit_code != 0 { |
16 | 67 | error make { |
17 | | - msg: $run.stderr, |
| 68 | + msg: "jc exection failed" |
18 | 69 | 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 |
21 | 72 | } |
22 | 73 | } |
23 | 74 | } |
24 | 75 |
|
25 | | - if '--help' in $args or '-h' in $args { |
| 76 | + if "--help" in $arguments or "-h" in $arguments { |
26 | 77 | $run.stdout |
27 | 78 | } else { |
28 | 79 | $run.stdout | from json |
|
0 commit comments