Add support for Nushell

This commit is contained in:
Ajeet D'Souza 2021-03-27 21:34:47 +05:30
parent 06062e92ca
commit e64dc4c8c3
6 changed files with 148 additions and 38 deletions

View File

@ -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

View File

@ -1,5 +1,3 @@
<!-- omit in toc -->
# 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
<!-- omit in toc -->
## 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`.
<!-- omit in toc -->
#### 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` |
<!-- omit in toc -->
#### 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` |
<!-- omit in toc -->
#### 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` |
<!-- omit in toc -->
#### 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` |
<!-- omit in toc -->
#### 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
```
<!-- omit in toc -->
#### bash
Add the following line to your `~/.bashrc`:
@ -137,8 +118,6 @@ Add the following line to your `~/.bashrc`:
eval "$(zoxide init bash)"
```
<!-- omit in toc -->
#### 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
```
<!-- omit in toc -->
#### 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 (& {
})
```
<!-- omit in toc -->
#### xonsh
Add the following line to your profile (usually `~/.xonshrc`):
```xonsh
```python
execx($(zoxide init xonsh), 'exec', __xonsh__.ctx, filename='zoxide')
```
<!-- omit in toc -->
#### zsh
Add the following line to your `~/.zshrc`:
@ -180,8 +168,6 @@ Add the following line to your `~/.zshrc`:
eval "$(zoxide init zsh)"
```
<!-- omit in toc -->
#### 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:

View File

@ -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,

View File

@ -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()

100
templates/nushell.txt Normal file
View File

@ -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"

View File

@ -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" })