diff --git a/dot_config/nushell/completions/zoxide.nu b/dot_config/nushell/completions/zoxide.nu index ebbddc7..7c22060 100644 --- a/dot_config/nushell/completions/zoxide.nu +++ b/dot_config/nushell/completions/zoxide.nu @@ -6,15 +6,23 @@ # # Initialize hook to add new entries to the database. -if (not ($env | default false __zoxide_hooked | get __zoxide_hooked)) { - $env.__zoxide_hooked = 1 - $env.config = ($env | default {} config).config - $env.config = ($env.config | default {} hooks) - $env.config = ($env.config | update hooks ($env.config.hooks | default {} env_change)) - $env.config = ($env.config | update hooks.env_change ($env.config.hooks.env_change | default [] PWD)) - $env.config = ($env.config | update hooks.env_change.PWD ($env.config.hooks.env_change.PWD | append {|_, dir| - zoxide add -- $dir - })) +export-env { + $env.config = ( + $env.config? + | default {} + | upsert hooks { default {} } + | upsert hooks.env_change { default {} } + | upsert hooks.env_change.PWD { default [] } + ) + let __zoxide_hooked = ( + $env.config.hooks.env_change.PWD | any { try { get __zoxide_hook } catch { false } } + ) + if not $__zoxide_hooked { + $env.config.hooks.env_change.PWD = ($env.config.hooks.env_change.PWD | append { + __zoxide_hook: true, + code: {|_, dir| zoxide add -- $dir} + }) + } } # ============================================================================= @@ -23,18 +31,20 @@ if (not ($env | default false __zoxide_hooked | get __zoxide_hooked)) { # # Jump to a directory using only keywords. -def --env __zoxide_z [...rest:string] { - let arg0 = ($rest | append '~').0 - let path = if (($rest | length) <= 1) and ($arg0 == '-' or ($arg0 | path expand | path type) == dir) { - $arg0 - } else { - (zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n") +def --env --wrapped __zoxide_z [...rest: string] { + let path = match $rest { + [] => {'~'}, + [ '-' ] => {'-'}, + [ $arg ] if ($arg | path type) == 'dir' => {$arg} + _ => { + zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n" + } } cd $path } # Jump to a directory using interactive search. -def --env __zoxide_zi [...rest:string] { +def --env --wrapped __zoxide_zi [...rest:string] { cd $'(zoxide query --interactive -- ...$rest | str trim -r -c "\n")' } @@ -43,8 +53,8 @@ def --env __zoxide_zi [...rest:string] { # Commands for zoxide. Disable these using --no-cmd. # -alias cd = __zoxide_z -alias cdi = __zoxide_zi +alias z = __zoxide_z +alias zi = __zoxide_zi # ============================================================================= #