Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions modules/jc/README.md
Original file line number Diff line number Diff line change
@@ -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
use modules/jc
```
85 changes: 68 additions & 17 deletions modules/jc/mod.nu
Original file line number Diff line number Diff line change
@@ -1,28 +1,79 @@
# 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" [commandline: string] {
let stor = stor open

if $stor.jc_completions? == null {
stor create --table-name jc_completions --columns { value: str, description: str, is_flag: bool }
}

if $stor.jc_completions_ran? == null {
stor create --table-name jc_completions_ran --columns { _: bool }
}

if $stor.jc_completions_ran == [] { try {
let about = ^jc --about
| from json

let magic = $about
| get parsers
| each { { value: $in.magic_commands?, description: $in.description } }
| where value != null
| flatten

let options = $about
| get parsers
| select argument description
| rename value description

let inherent = ^jc --help
| lines
| split list "" # Group with empty lines as boundary.
| where { $in.0? == "Options:" } | get 0 # Get the first section that starts with "Options:"
| skip 1 # Remove header
| each { str trim }
| parse "{short}, {long} {description}"
| update description { str trim }
| each {|record|
[[value, description];
[$record.short, $record.description],
[$record.long, $record.description],
]
}
| flatten

for entry in $magic {
stor insert --table-name jc_completions --data-record ($entry | insert is_flag false)
}

for entry in ($options ++ $inherent) {
stor insert --table-name jc_completions --data-record ($entry | insert is_flag true)
}

stor insert --table-name jc_completions_ran --data-record { _: true }
} }

if ($commandline | str contains "-") {
$stor.jc_completions
} else {
$stor.jc_completions
| where is_flag == 0
} | select value description
}

# Run `jc` (JSON Converter).
export def --wrapped main [...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
Expand Down