From 18965c177a8a123d5185c5ebbf927dce1693bf9e Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Tue, 8 Mar 2022 04:06:37 +0530 Subject: [PATCH] Upgrade Nushell support for engine-q --- README.md | 2 +- man/man1/zoxide-init.1 | 2 +- templates/nushell.txt | 93 +++++++++++++++++------------------------- 3 files changed, 40 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 94e67e4..2f15f84 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ Add this to your configuration (find it by running `config path` in Nushell): startup = ["zoxide init nushell --hook prompt | save ~/.zoxide.nu", "source ~/.zoxide.nu"] ``` -Note: zoxide only supports Nushell v0.37.0 and above. +Note: zoxide only supports Nushell v0.59.0 and above. diff --git a/man/man1/zoxide-init.1 b/man/man1/zoxide-init.1 index 69a3a9e..ee96d17 100644 --- a/man/man1/zoxide-init.1 +++ b/man/man1/zoxide-init.1 @@ -37,7 +37,7 @@ Nushell): \fBstartup = ["zoxide init nushell --hook prompt | save ~/.zoxide.nu", "source ~/.zoxide.nu"]\fR .fi .sp -Note: zoxide only supports Nushell v0.37.0 and above. +Note: zoxide only supports Nushell v0.59.0 and above. .TP .B powershell Add this to your configuration (find it by running \fBecho $profile\fR in diff --git a/templates/nushell.txt b/templates/nushell.txt index 9874a31..ed5814b 100644 --- a/templates/nushell.txt +++ b/templates/nushell.txt @@ -3,45 +3,40 @@ # Code generated by zoxide. DO NOT EDIT. -{{ section }} -# Utility functions for zoxide. -# - -# Default prompt for Nushell. -def __zoxide_prompt [] { - let git = $'(do -i {git rev-parse --abbrev-ref HEAD} | str trim -rc (char newline))' - let git = (if ($git | str length) == 0 { '' } { - build-string (char lparen) (ansi cb) $git (ansi reset) (char rparen) - }) - build-string (ansi gb) (pwd) (ansi reset) $git '> ' -} - {{ section }} # Hook configuration for zoxide. # - -# Hook to add new entries to the database. -{%- match hook %} +{% match hook %} {%- when InitHook::None %} {{ not_configured }} {%- when InitHook::Prompt %} +# Default prompt for Nushell. +let-env __zoxide_oldprompt = (if '__zoxide_oldprompt' in (env).name { + $env.__zoxide_oldprompt +} else { + if 'PROMPT_COMMAND' in (env).name { + $env.PROMPT_COMMAND + } else { +{# An error causes Nushell to silently print the default prompt. #} + __zoxide_undefined + } +}) + +# Hook to add new entries to the database. def __zoxide_hook [] { - shells | where active == $true && name == filesystem | get path | each { - zoxide add -- $it - } + zoxide add -- $nu.cwd } # Initialize hook. -let-env PROMPT_COMMAND = ( - let prompt = (if ($nu.env | select PROMPT_COMMAND | empty?) { - if ($nu.config | select prompt | empty?) { '__zoxide_prompt' } { $nu.config.prompt } - } { $nu.env.PROMPT_COMMAND }); - if ($prompt | str contains '__zoxide_hook') { $prompt } { $'__zoxide_hook;($prompt)' } -) +let-env PROMPT_COMMAND = { + __zoxide_hook + do $env.__zoxide_oldprompt +} {%- when InitHook::Pwd %} -$'zoxide: PWD hooks are not supported on Nushell.(char nl)Use (char sq)zoxide init nushell --hook prompt(char sq) instead.(char nl)' +echo $'zoxide: PWD hooks are not supported on Nushell.(char newline)Use `zoxide init nushell --hook prompt` instead.' + {%- endmatch %} {{ section }} @@ -50,39 +45,26 @@ $'zoxide: PWD hooks are not supported on Nushell.(char nl)Use (char sq)zoxide in # # Jump to a directory using only keywords. -def __zoxide_z [...rest:string] { - if (shells | where active == $true | get name) != filesystem { - if ($rest | length) > 1 { - $'zoxide: can only jump directories on filesystem(char nl)' - } { - cd $rest +def-env __zoxide_z [...rest:string] { + # `z -` doesn't work yet, see https://github.com/nushell/nushell/issues/4769 + let arg0 = ($rest | append '~').0 + let path = if ($rest | length) <= 1 && ($arg0 | path expand | path type) == dir { + $arg0 + } else { + (zoxide query --exclude $nu.cwd -- $rest | str trim -r -c (char newline)) + } + cd $path {%- if echo %} - pwd + echo $nu.cwd {%- endif %} - } - } { - let arg0 = ($rest | append '~' | first 1); - if ($rest | length) <= 1 && ($arg0 == '-' || ($arg0 | path expand | path exists)) { - cd $arg0 - } { - cd $'(zoxide query --exclude (pwd) -- $rest | str trim -rc (char newline))' - } -{%- if echo %} - pwd -{%- endif %} - } } # Jump to a directory using interactive search. -def __zoxide_zi [...rest:string] { - if (shells | where active == $true | get name) != filesystem { - $'zoxide: can only jump directories on filesystem(char nl)' - } { - cd $'(zoxide query -i -- $rest | str trim -rc (char newline))' +def-env __zoxide_zi [...rest:string] { + cd $'(zoxide query -i -- $rest | str trim -r -c (char newline))' {%- if echo %} - pwd + echo $nu.cwd {%- endif %} - } } {{ section }} @@ -103,8 +85,9 @@ alias {{cmd}}i = __zoxide_zi {{ section }} # To initialize zoxide, add this to your configuration (find it by running -# `config path` in Nushell): +# `$nu.config-path` in Nushell): # -# startup = ['zoxide init nushell --hook prompt | save ~/.zoxide.nu', 'source ~/.zoxide.nu'] +# zoxide init nushell --hook prompt | save ~/.zoxide.nu +# source ~/.zoxide.nu # -# Note: zoxide only supports Nushell v0.37.0 and above. +# Note: zoxide only supports Nushell v0.59.0 and above.