From ab464ce6f1dc2cdc84e6448630f13577697619fd Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sat, 19 Jul 2025 00:57:12 +0300 Subject: [PATCH] jc: improve suggestions --- modules/common/nushell/config.nu | 59 ++++++++++++++++++++++---------- rebuild.nu | 7 +--- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/modules/common/nushell/config.nu b/modules/common/nushell/config.nu index 8036d48..1420601 100644 --- a/modules/common/nushell/config.nu +++ b/modules/common/nushell/config.nu @@ -24,42 +24,65 @@ def --env mcg [path: path]: nothing -> nothing { jj git init --colocate } -def --env "nu-complete jc" [] { - if $env.__NU_COMPLETE_JC? != null { - return $env.__NU_COMPLETE_JC +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 } } - let options = try { - let options = ^jc --help - | collect - | parse "{_}Parsers:\n{_}\n\nOptions:\n{inherent}\n\nSlice:{_}" - | get 0 + if $stor.jc_completions_ran? == null { + stor create --table-name jc_completions_ran --columns { _: bool } + } - let parsers = ^jc --about + 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 = $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 - } catch { - [] - } + for entry in $magic { + stor insert --table-name jc_completions --data-record ($entry | insert is_flag false) + } - $env.__NU_COMPLETE_JC = $options + for entry in ($options ++ $inherent) { + stor insert --table-name jc_completions --data-record ($entry | insert is_flag true) + } - $options + 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). diff --git a/rebuild.nu b/rebuild.nu index a8599e1..1578e88 100755 --- a/rebuild.nu +++ b/rebuild.nu @@ -123,13 +123,8 @@ def darwin-shadow-xcode-popup [] { def darwin-set-zshrc [] { print "setting zshrc..." - let nu_command = $"let usr_bin_index = $env.PATH -| enumerate -| where item == /usr/bin -| get 0.index; - + let nu_command = $"let usr_bin_index = $env.PATH | enumerate | where item == /usr/bin | get 0.index; $env.PATH = $env.PATH | insert $usr_bin_index ($shadow_path | path expand); - $env.SHELL = which nu | get 0.path" | str replace --all "\n" "" let zshrc = $"exec nu --execute '($nu_command)'"