Last active
March 30, 2025 16:46
-
-
Save MrPandir/bf46b9041cf2704233dfc65048acf24b to your computer and use it in GitHub Desktop.
My rewritten carapace configuration file for nushell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std "path add" | |
def cache [] { | |
let func = $in | |
let version = version | get version | |
let path = $nu.temp-path | path join "carapace-cache" $version | |
try { | |
open $path | |
} catch { | |
mkdir ($nu.temp-path | path join "carapace-cache") | |
do $func o> $path | |
open $path | |
} | |
} | |
def delete_pipe [spans: list<string>]: nothing -> list { | |
if $spans.0 == "|" {$spans | skip 1} else {$spans} | |
} | |
def expand_alias [spans: list<string>]: nothing -> list { | |
let expanded_alias = ( | |
scope aliases | |
| where name == $spans.0 | |
| get 0?.expansion? | |
) | |
if ($expanded_alias | is-empty) { | |
return $spans | |
} | |
$spans | |
| skip 1 | |
| prepend ($expanded_alias | split words | first) | |
} | |
def get_carapace_complite [spans: list<string>] { | |
carapace $spans.0 nushell ...$spans | from json | default null | |
} | |
def ignore_error [] { | |
let input_data = $in | |
if ($input_data | is-empty) { | |
return null | |
} | |
if not ( | |
$input_data | |
| get description? | |
| to text | |
| str contains "unknown shorthand flag" | |
) {$input_data} | |
} | |
def carapace_completer [] {{|spans| | |
let spans = delete_pipe $spans | |
let spans = expand_alias $spans | |
if ($spans | is-not-empty) { | |
get_carapace_complite $spans | ignore_error | |
} | |
}} | |
def get_config_dir []: nothing -> path { | |
$env.XDG_CONFIG_HOME? | default ($env.HOME | path join ".config") | |
} | |
export-env { | |
path add (get_config_dir | path join "carapace/bin") | |
if ($env.config?.completions?.external?.enable? | is-empty) { | |
$env.config.completions.external.enable = true | |
} | |
$env.config.completions.external.completer = (carapace_completer) | |
} | |
export def --env get-env [name] { $env | get $name } | |
export def --env set-env [name, value] { load-env { $name: $value } } | |
export def --env unset-env [name] { hide-env $name } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Different from the official compliter that is provided in the
carapace _carapace
command:XDG_CONFIG_HOME
variable (orHOME
+.config
) instead of the harcoded path.nushell
commands are ignored.env <name command>
.unknown shorthand flag
error is ignored.Examples
Use
Warning
This script is written for version
0.103.0
and above.NU_LIB_DIRS
in theenv.nu
file so that you can import using only the file nameconfig.nu
configuration