From 41b1e9510b3b1b76ed7830736ee10e4e30ceed8a Mon Sep 17 00:00:00 2001 From: RGBCube Date: Fri, 18 Jul 2025 22:28:37 +0300 Subject: [PATCH 1/6] jc: add completions and README --- modules/jc/README.md | 34 ++++++++++++++++++++++++ modules/jc/mod.nu | 62 ++++++++++++++++++++++++++++++++------------ 2 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 modules/jc/README.md diff --git a/modules/jc/README.md b/modules/jc/README.md new file mode 100644 index 000000000..a26c4425c --- /dev/null +++ b/modules/jc/README.md @@ -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: + + +2. Source this module in your `config.nu`: + ```nu + source ~/path/to/jc/mod.rs + ``` diff --git a/modules/jc/mod.nu b/modules/jc/mod.nu index f83e52228..5a78f13ee 100644 --- a/modules/jc/mod.nu +++ b/modules/jc/mod.nu @@ -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 From 9482d0325a6395a3e24da96600bff3c22b75449b Mon Sep 17 00:00:00 2001 From: RGBCube Date: Fri, 18 Jul 2025 23:54:23 +0300 Subject: [PATCH 2/6] jc: add magic command completions and clean up parsing --- modules/jc/mod.nu | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/modules/jc/mod.nu b/modules/jc/mod.nu index 5a78f13ee..2f5618724 100644 --- a/modules/jc/mod.nu +++ b/modules/jc/mod.nu @@ -4,29 +4,37 @@ def --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 + 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 = $options.inherent + let inherent = ^jc --help | lines - | parse " {short}, {long} {description}" + | 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]] + [$record.long, $record.description], + ] } | flatten - $parsers ++ $inherent + $options ++ $inherent ++ $magic } catch { [] } From 2b2a17d54ba3157374a4b93fbe24823e562e7107 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Fri, 18 Jul 2025 23:59:42 +0300 Subject: [PATCH 3/6] jc: fix instructions and make it a module --- modules/jc/README.md | 2 +- modules/jc/mod.nu | 12 ++---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/modules/jc/README.md b/modules/jc/README.md index a26c4425c..64914a997 100644 --- a/modules/jc/README.md +++ b/modules/jc/README.md @@ -30,5 +30,5 @@ automatically parses its output into a structured data format. 2. Source this module in your `config.nu`: ```nu - source ~/path/to/jc/mod.rs + use modules/jc ``` diff --git a/modules/jc/mod.nu b/modules/jc/mod.nu index 2f5618724..4e8a49797 100644 --- a/modules/jc/mod.nu +++ b/modules/jc/mod.nu @@ -1,9 +1,5 @@ def --env "nu-complete jc" [] { - if $env.__NU_COMPLETE_JC? != null { - return $env.__NU_COMPLETE_JC - } - - let options = try { + try { let about = ^jc --about | from json @@ -38,14 +34,10 @@ def --env "nu-complete jc" [] { } 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] { +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 { From 0a36da8326ffe440b0d7e983cd0cac1267809ce3 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sat, 19 Jul 2025 00:06:31 +0300 Subject: [PATCH 4/6] jc: only show --options if commandline ends with - --- modules/jc/mod.nu | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/jc/mod.nu b/modules/jc/mod.nu index 4e8a49797..2a1241aa7 100644 --- a/modules/jc/mod.nu +++ b/modules/jc/mod.nu @@ -1,4 +1,4 @@ -def --env "nu-complete jc" [] { +def --env "nu-complete jc" [commandline: string] { try { let about = ^jc --about | from json @@ -30,7 +30,11 @@ def --env "nu-complete jc" [] { } | flatten - $options ++ $inherent ++ $magic + $magic ++ if ($commandline | str ends-with "-") { + $options ++ $inherent + } else { + [] + } } catch { [] } From 6ce9922b982498fef891dbba20a74dcdc6770837 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sat, 19 Jul 2025 00:19:59 +0300 Subject: [PATCH 5/6] jc: cache completions --- modules/jc/mod.nu | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/modules/jc/mod.nu b/modules/jc/mod.nu index 2a1241aa7..878c6000a 100644 --- a/modules/jc/mod.nu +++ b/modules/jc/mod.nu @@ -1,5 +1,21 @@ def --env "nu-complete jc" [commandline: string] { - try { + let stor = stor open + + if $stor.jc_completions? == null { + stor create --table-name jc_completions --columns { value: str, description: str } + } + + if $stor.jc_completions_ran? == null { + stor create --table-name jc_completions_ran --columns { _: bool } + } + + if $stor.jc_completions_ran != [] { + return $stor.jc_completions + } else { + stor insert --table-name jc_completions_ran --data-record { _: true } + } + + let completions = try { let about = ^jc --about | from json @@ -38,6 +54,12 @@ def --env "nu-complete jc" [commandline: string] { } catch { [] } + + for entry in $completions { + stor insert --table-name jc_completions --data-record $entry + } + + $completions } # Run `jc` (JSON Converter). From d8b8b2cc80ffdec440e36501d005db2f2dc2133e Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sat, 19 Jul 2025 00:34:29 +0300 Subject: [PATCH 6/6] jc: fix completions for flags --- modules/jc/mod.nu | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/modules/jc/mod.nu b/modules/jc/mod.nu index 878c6000a..c50bf082f 100644 --- a/modules/jc/mod.nu +++ b/modules/jc/mod.nu @@ -2,20 +2,14 @@ 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 } + 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 != [] { - return $stor.jc_completions - } else { - stor insert --table-name jc_completions_ran --data-record { _: true } - } - - let completions = try { + if $stor.jc_completions_ran == [] { try { let about = ^jc --about | from json @@ -46,20 +40,23 @@ def --env "nu-complete jc" [commandline: string] { } | flatten - $magic ++ if ($commandline | str ends-with "-") { - $options ++ $inherent - } else { - [] + for entry in $magic { + stor insert --table-name jc_completions --data-record ($entry | insert is_flag false) } - } catch { - [] - } - for entry in $completions { - stor insert --table-name jc_completions --data-record $entry - } + for entry in ($options ++ $inherent) { + stor insert --table-name jc_completions --data-record ($entry | insert is_flag true) + } - $completions + 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).