mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-07-31 14:17:45 +00:00
jc: add completions and README
This commit is contained in:
parent
ba9a4c1cac
commit
41b1e9510b
2 changed files with 79 additions and 17 deletions
34
modules/jc/README.md
Normal file
34
modules/jc/README.md
Normal file
|
@ -0,0 +1,34 @@
|
|||
# jc (JSON converter)
|
||||
|
||||
jc converts the output of many commands, file-types, and strings to JSON or YAML
|
||||
|
||||
This module provides a wrapper around the `jc` command line tool and
|
||||
automatically parses its output into a structured data format.
|
||||
|
||||
## Example
|
||||
|
||||
```nu
|
||||
> df | jc --df
|
||||
|
||||
┌─#─┬───filesystem───┬─1k_blocks─┬───used────┬─available─┬─────────mounted_on─────────┬─use_percent─┬─capacity_percent─┐
|
||||
│ 0 │ /dev/disk3s1s1 │ 482797652 │ 368026060 │ 114771592 │ / │ 77 │ 24 │
|
||||
│ 1 │ /dev/disk3s6 │ 482797652 │ 368026060 │ 114771592 │ /System/Volumes/VM │ 77 │ 24 │
|
||||
│ 2 │ /dev/disk3s2 │ 482797652 │ 368026060 │ 114771592 │ /System/Volumes/Preboot │ 77 │ 24 │
|
||||
│ 3 │ /dev/disk3s4 │ 482797652 │ 368026060 │ 114771592 │ /System/Volumes/Update │ 77 │ 24 │
|
||||
│ 4 │ /dev/disk1s2 │ 512000 │ 23052 │ 488948 │ /System/Volumes/xarts │ 5 │ 96 │
|
||||
│ 5 │ /dev/disk1s1 │ 512000 │ 23052 │ 488948 │ /System/Volumes/iSCPreboot │ 5 │ 96 │
|
||||
│ 6 │ /dev/disk1s3 │ 512000 │ 23052 │ 488948 │ /System/Volumes/Hardware │ 5 │ 96 │
|
||||
│ 7 │ /dev/disk3s7 │ 482797652 │ 368026060 │ 114771592 │ /nix │ 77 │ 24 │
|
||||
│ 8 │ /dev/disk4 │ 524288 │ 12316 │ 511972 │ /private/var/run/agenix.d │ 3 │ 98 │
|
||||
└───┴────────────────┴───────────┴───────────┴───────────┴────────────────────────────┴─────────────┴──────────────────┘
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
1. Install the `jc` command line:
|
||||
<https://kellyjonbrazil.github.io/jc/#installation>
|
||||
|
||||
2. Source this module in your `config.nu`:
|
||||
```nu
|
||||
source ~/path/to/jc/mod.rs
|
||||
```
|
|
@ -1,28 +1,56 @@
|
|||
# Run `jc` (Json Converter)
|
||||
#
|
||||
# This module provides a wrapper around the `jc` command line tool and automatically
|
||||
# parses its output into a structured data format.
|
||||
#
|
||||
# Dependencies:
|
||||
# * `jc`
|
||||
#
|
||||
# Installation:
|
||||
# 1. Install the `jc` command line: https://kellyjonbrazil.github.io/jc/#installation
|
||||
# 2. Import this module in your `config.nu`: `import ~/.local/share/nu_scripts/modules/jc/`
|
||||
export def --wrapped main [...args]: [any -> table, any -> record, any -> string] {
|
||||
let run = (^jc ...$args | complete)
|
||||
def --env "nu-complete jc" [] {
|
||||
if $env.__NU_COMPLETE_JC? != null {
|
||||
return $env.__NU_COMPLETE_JC
|
||||
}
|
||||
|
||||
let options = try {
|
||||
let options = ^jc --help
|
||||
| collect
|
||||
| parse "{_}Parsers:\n{_}\n\nOptions:\n{inherent}\n\nSlice:{_}"
|
||||
| get 0
|
||||
|
||||
let parsers = ^jc --about
|
||||
| from json
|
||||
| get parsers
|
||||
| select argument description
|
||||
| rename value description
|
||||
|
||||
let inherent = $options.inherent
|
||||
| lines
|
||||
| parse " {short}, {long} {description}"
|
||||
| update description { str trim }
|
||||
| each {|record|
|
||||
[[value, description];
|
||||
[$record.short, $record.description],
|
||||
[$record.long, $record.description]]
|
||||
}
|
||||
| flatten
|
||||
|
||||
$parsers ++ $inherent
|
||||
} catch {
|
||||
[]
|
||||
}
|
||||
|
||||
$env.__NU_COMPLETE_JC = $options
|
||||
|
||||
$options
|
||||
}
|
||||
|
||||
# Run `jc` (JSON Converter).
|
||||
export def --wrapped jc [...arguments: string@"nu-complete jc"]: [any -> table, any -> record, any -> string] {
|
||||
let run = ^jc ...$arguments | complete
|
||||
|
||||
if $run.exit_code != 0 {
|
||||
error make {
|
||||
msg: $run.stderr,
|
||||
msg: "jc exection failed"
|
||||
label: {
|
||||
text: "jc execution failed",
|
||||
span: (metadata $args).span
|
||||
text: ($run.stderr | str replace "jc:" "" | str replace "Error -" "" | str trim)
|
||||
span: (metadata $arguments).span
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if '--help' in $args or '-h' in $args {
|
||||
if "--help" in $arguments or "-h" in $arguments {
|
||||
$run.stdout
|
||||
} else {
|
||||
$run.stdout | from json
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue