From 648f6ec70fd84a0e0fd999c2d430fdbf2bcb0c26 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sat, 19 Jul 2025 18:42:15 +0300 Subject: [PATCH] nushell: reorder config and make zoxide work --- modules/common/nushell/config.nu | 232 +++++++++++++++---------------- 1 file changed, 116 insertions(+), 116 deletions(-) diff --git a/modules/common/nushell/config.nu b/modules/common/nushell/config.nu index e80e59e..e260773 100644 --- a/modules/common/nushell/config.nu +++ b/modules/common/nushell/config.nu @@ -1,121 +1,6 @@ use std-rfc/clip use std null_device -source ~/.config/nushell/zoxide.nu - -$env.CARAPACE_BRIDGES = "inshellisense,carapace,zsh,fish,bash" -source ~/.config/nushell/carapace.nu - -# Retrieve the output of the last command. -def _ []: nothing -> any { - $env.last? -} - -# Create a directory and cd into it. -def --env mc [path: path]: nothing -> nothing { - mkdir $path - cd $path -} - -# Create a directory, cd into it and initialize version control. -def --env mcg [path: path]: nothing -> nothing { - mkdir $path - cd $path - jj git init --colocate -} - -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). -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: "jc exection failed" - label: { - text: ($run.stderr | str replace "jc:" "" | str replace "Error -" "" | str trim) - span: (metadata $arguments).span - } - } - } - - if "--help" in $arguments or "-h" in $arguments { - $run.stdout - } else { - $run.stdout | from json - } -} - -# `nu-highlight` with default colors -# -# Custom themes can produce a lot more ansi color codes and make the output -# exceed discord's character limits -def nu-highlight-default [] { - let input = $in - $env.config.color_config = {} - $input | nu-highlight -} - $env.config.history.file_format = "sqlite" $env.config.history.isolation = false $env.config.history.max_size = 10_000_000 @@ -210,10 +95,20 @@ $env.config.hooks.display_output = {|| $env.config.hooks.command_not_found = [] +# `nu-highlight` with default colors +# +# Custom themes can produce a lot more ansi color codes and make the output +# exceed discord's character limits +def nu-highlight-default [] { + let input = $in + $env.config.color_config = {} + $input | nu-highlight +} + # Copy the current commandline, add syntax highlighting, wrap it in a # markdown code block, copy that to the system clipboard. # -# Perfect for sharing code snippets on discord +# Perfect for sharing code snippets on Discord. def "nu-keybind commandline-copy" []: nothing -> nothing { commandline | nu-highlight-default @@ -541,3 +436,108 @@ module dump { } use dump + +# Retrieve the output of the last command. +def _ []: nothing -> any { + $env.last? +} + +# Create a directory and cd into it. +def --env mc [path: path]: nothing -> nothing { + mkdir $path + cd $path +} + +# Create a directory, cd into it and initialize version control. +def --env mcg [path: path]: nothing -> nothing { + mkdir $path + cd $path + jj git init --colocate +} + +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). +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: "jc exection failed" + label: { + text: ($run.stderr | str replace "jc:" "" | str replace "Error -" "" | str trim) + span: (metadata $arguments).span + } + } + } + + if "--help" in $arguments or "-h" in $arguments { + $run.stdout + } else { + $run.stdout | from json + } +} + +source ~/.config/nushell/zoxide.nu + +$env.CARAPACE_BRIDGES = "inshellisense,carapace,zsh,fish,bash" +source ~/.config/nushell/carapace.nu