diff --git a/CHANGELOG.md b/CHANGELOG.md index a0fe6f1..6969fef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Added + +- Support for [Nushell](https://www.nushell.sh/). + ### Fixed - Removed backtraces on Rust nightly. @@ -69,7 +73,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Support for PowerShell. +- Support for powershell. ### Removed diff --git a/README.md b/README.md index 37cceb9..e4393e7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ - - # zoxide [![crates.io](https://img.shields.io/crates/v/zoxide)](https://crates.io/crates/zoxide) @@ -7,16 +5,11 @@ A faster way to navigate your filesystem - - ## Table of contents - [Introduction](#introduction) - [Examples](#examples) - [Getting started](#getting-started) - - [Step 1: Install zoxide](#step-1-install-zoxide) - - [Step 2: Install fzf (optional)](#step-2-install-fzf-optional) - - [Step 3: Add zoxide to your shell](#step-3-add-zoxide-to-your-shell) - [Configuration](#configuration) ## Introduction @@ -51,8 +44,6 @@ curl --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/ajeetdsou If you would rather not run a script, you can download the binary from the [Releases](https://github.com/ajeetdsouza/zoxide/releases) page and add it anywhere in your `$PATH`. - - #### On Linux | Distribution | Repository | Instructions | @@ -68,8 +59,6 @@ If you would rather not run a script, you can download the binary from the [Rele | Parrot OS | | `apt install zoxide` | | Void Linux | [Void Linux Packages] | `xbps-install -S zoxide` | - - #### On macOS | Repository | Instructions | @@ -78,8 +67,6 @@ If you would rather not run a script, you can download the binary from the [Rele | [Homebrew] | `brew install zoxide` | | [MacPorts] | `port install zoxide` | - - #### On Windows | Repository | Instructions | @@ -87,8 +74,6 @@ If you would rather not run a script, you can download the binary from the [Rele | [crates.io] | `cargo install zoxide` | | [Scoop] | `scoop install zoxide` | - - #### On BSD | Distribution | Repository | Instructions | @@ -98,8 +83,6 @@ If you would rather not run a script, you can download the binary from the [Rele | FreeBSD | [FreshPorts] | `pkg install zoxide` | | NetBSD | [pkgsrc] | `pkgin install zoxide` | - - #### On Android | Repository | Instructions | @@ -118,7 +101,7 @@ If you currently use `z`, `z.lua`, or `zsh-z`, you may want to first import your existing entries into `zoxide`: ```sh -zoxide import /path/to/db +zoxide import --from z /path/to/db ``` Alternatively, for `autojump`: @@ -127,8 +110,6 @@ Alternatively, for `autojump`: zoxide import --from autojump /path/to/db ``` - - #### bash Add the following line to your `~/.bashrc`: @@ -137,8 +118,6 @@ Add the following line to your `~/.bashrc`: eval "$(zoxide init bash)" ``` - - #### fish Add the following line to your `~/.config/fish/config.fish`: @@ -147,9 +126,22 @@ Add the following line to your `~/.config/fish/config.fish`: zoxide init fish | source ``` - +#### nushell -#### PowerShell +Initialize zoxide's Nushell script: + +```sh +zoxide init nushell --hook prompt | save ~/.zoxide.nu +``` + +Then, in your Nushell configuration file: + +- Prepend `__zoxide_hook|` to your `prompt` variable. +- Add the following two lines to your `startup` variable: + - `zoxide init nushell --hook prompt | save ~/.zoxide.nu` + - `source ~/.zoxide.nu` + +#### powershell Add the following line to your profile: @@ -160,18 +152,14 @@ Invoke-Expression (& { }) ``` - - #### xonsh Add the following line to your profile (usually `~/.xonshrc`): -```xonsh +```python execx($(zoxide init xonsh), 'exec', __xonsh__.ctx, filename='zoxide') ``` - - #### zsh Add the following line to your `~/.zshrc`: @@ -180,8 +168,6 @@ Add the following line to your `~/.zshrc`: eval "$(zoxide init zsh)" ``` - - #### Any POSIX shell Add the following line to your shell's configuration file: @@ -205,6 +191,8 @@ eval "$(zoxide init posix --hook prompt)" ### Environment variables +Be sure to set these before calling `zoxide init`. + - `_ZO_DATA_DIR` - Specifies the directory in which zoxide should store its database. - The default value varies across OSes: diff --git a/src/cmd/init.rs b/src/cmd/init.rs index b0e24d4..d425a7c 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -51,8 +51,9 @@ impl Cmd for Init { let source = match self.shell { Shell::Bash => shell::Bash(opts).render(), Shell::Fish => shell::Fish(opts).render(), + Shell::Nushell => shell::Nushell(opts).render(), Shell::Posix => shell::Posix(opts).render(), - Shell::Powershell => shell::PowerShell(opts).render(), + Shell::Powershell => shell::Powershell(opts).render(), Shell::Xonsh => shell::Xonsh(opts).render(), Shell::Zsh => shell::Zsh(opts).render(), } @@ -65,6 +66,7 @@ impl Cmd for Init { enum Shell { Bash, Fish, + Nushell, Posix, Powershell, Xonsh, diff --git a/src/shell.rs b/src/shell.rs index 0636187..b643c66 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -25,8 +25,9 @@ macro_rules! make_template { make_template!(Bash, "bash.txt"); make_template!(Fish, "fish.txt"); +make_template!(Nushell, "nushell.txt"); make_template!(Posix, "posix.txt"); -make_template!(PowerShell, "powershell.txt"); +make_template!(Powershell, "powershell.txt"); make_template!(Xonsh, "xonsh.txt"); make_template!(Zsh, "zsh.txt"); @@ -152,6 +153,21 @@ mod tests { // TODO: fishindent + #[test] + fn nushell_nushell_#i() { + let opts = dbg!(&opts()[i]); + let source = Nushell(opts).render().unwrap(); + let assert = Command::new("nu") + .args(&["--commands", &source]) + .assert() + .success() + .stderr(""); + + if opts.hook != Hook::Pwd { + assert.stdout(""); + } + } + #[test] fn posix_bashposix_#i() { let opts = dbg!(&opts()[i]); @@ -214,7 +230,7 @@ mod tests { #[test] fn powershell_pwsh_#i() { let opts = dbg!(&opts()[i]); - let source = PowerShell(opts).render().unwrap(); + let source = Powershell(opts).render().unwrap(); Command::new("pwsh") .args(&["-Command", &source, "-NoLogo", "-NonInteractive", "-NoProfile"]) .assert() diff --git a/templates/nushell.txt b/templates/nushell.txt new file mode 100644 index 0000000..5fac436 --- /dev/null +++ b/templates/nushell.txt @@ -0,0 +1,100 @@ +{%- let SECTION = "# =============================================================================\n#" -%} +{%- let NOT_CONFIGURED = "# -- not configured --" -%} + +{{ SECTION }} +# Hook configuration for zoxide. +# + +# Hook to add new entries to the database. +{%- match hook %} +{%- when Hook::None %} +def __zoxide_hook [] {} + +{%- when Hook::Prompt %} +def __zoxide_hook [] { + zoxide add "$(pwd)" +} + +{%- when Hook::Pwd %} +def __zoxide_hook [] {} + +printf "zoxide: PWD hooks are not supported on Nushell.\n Use 'zoxide init nushell --hook prompt' instead.\n" + +def __zoxide_hook [] { + zoxide add "$(pwd)" +} + +{%- endmatch %} + +{{ SECTION }} +# When using zoxide with --no-aliases, alias these internal functions as +# desired. +# + +# Jump to a directory using only keywords. +def __zoxide_z [...rest:string] { + if $(echo $rest | count) == 1 { + cd ~ + } { + let args = $(echo $rest | skip 1); + if $(echo $args | count) == 1 { + let arg0 = $(echo $args | first 1); + if $arg0 == '-' { + cd - + } { + if $(echo $arg0 | path exists) { + cd $arg0 + } { + cd $(zoxide query -- $args | str trim) + } + } + } { + cd $(zoxide query -- $args | str trim) + } + } + {%- if echo %} + echo $(pwd) + {%- endif %} +} + +# Jump to a directory using interactive search. +def __zoxide_zi [...rest:string] { + let args = $(echo $rest | skip 1) + cd $(zoxide query -i -- $args | str trim) + {%- if echo %} + echo $(pwd) + {%- endif %} +} + +{{ SECTION }} +# Convenient aliases for zoxide. Disable these using --no-aliases. +# + +{%- match cmd %} +{%- when Some with (cmd) %} + +alias {{cmd}} = __zoxide_z '' +alias {{cmd}}i = __zoxide_zi '' + +{%- when None %} +{{ NOT_CONFIGURED }} + +{%- endmatch %} + +{{ SECTION }} +# To initialize zoxide with Nushell: +# +# Step 1: Initialize zoxide's config file: +# +# touch ~/.zoxide.nu +# +# Step 2: In your Nushell configuration file, prepend this to the `prompt` +# variable: +# +# "__zoxide_hook | " +# +# Step 3: In your Nushell configuration file, add the following lines to the +# `startup` variable: +# +# "zoxide init nushell --hook prompt | save ~/.zoxide.nu" +# "source ~/.zoxide.nu" diff --git a/templates/powershell.txt b/templates/powershell.txt index e766d48..9e72391 100644 --- a/templates/powershell.txt +++ b/templates/powershell.txt @@ -46,7 +46,7 @@ if ($__zoxide_hooked -ne '1') { } } else { Write-Error ("`n" + - "zoxide: PWD hooks are not supported below PowerShell 6.`n" + + "zoxide: PWD hooks are not supported below powershell 6.`n" + " Use 'zoxide init powershell --hook prompt' instead.") } {%- endmatch %} @@ -100,7 +100,7 @@ Set-Alias {{cmd}}i __zoxide_zi {%- endmatch %} {{ SECTION }} -# To initialize zoxide with PowerShell, add the following line to your -# PowerShell configuration file (the location is stored in $profile): +# To initialize zoxide with powershell, add the following line to your +# powershell configuration file (the location is stored in $profile): # # Invoke-Expression (& { $hook = if ($PSVersionTable.PSVersion.Major -ge 6) { 'pwd' } else { 'prompt' } (zoxide init powershell --hook $hook) -join "`n" })