Skip to content

Commit da0fdb2

Browse files
authored
Update nushell init script (ajeetdsouza#966)
1 parent 3fe42e9 commit da0fdb2

File tree

12 files changed

+60
-36
lines changed

12 files changed

+60
-36
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,13 @@ When calling `zoxide init`, the following flags are available:
392392
- `--cmd cd` would replace the `cd` command.
393393
- `--hook <HOOK>`
394394
- Changes how often zoxide increments a directory's score:
395+
395396
| Hook | Description |
396397
| --------------- | --------------------------------- |
397398
| `none` | Never |
398399
| `prompt` | At every shell prompt |
399400
| `pwd` (default) | Whenever the directory is changed |
401+
400402
- `--no-cmd`
401403
- Prevents zoxide from defining the `z` and `zi` commands.
402404
- These functions will still be available in your shell as `__zoxide_z` and
@@ -410,22 +412,26 @@ Environment variables[^2] can be used for configuration. They must be set before
410412
- `_ZO_DATA_DIR`
411413
- Specifies the directory in which the database is stored.
412414
- The default value varies across OSes:
415+
413416
| OS | Path | Example |
414417
| ----------- | ---------------------------------------- | ------------------------------------------ |
415418
| Linux / BSD | `$XDG_DATA_HOME` or `$HOME/.local/share` | `/home/alice/.local/share` |
416419
| macOS | `$HOME/Library/Application Support` | `/Users/Alice/Library/Application Support` |
417420
| Windows | `%LOCALAPPDATA%` | `C:\Users\Alice\AppData\Local` |
421+
418422
- `_ZO_ECHO`
419423
- When set to 1, `z` will print the matched directory before navigating to
420424
it.
421425
- `_ZO_EXCLUDE_DIRS`
422426
- Excludes the specified directories from the database.
423427
- This is provided as a list of [globs][glob], separated by OS-specific
424428
characters:
429+
425430
| OS | Separator | Example |
426431
| ------------------- | --------- | ----------------------- |
427432
| Linux / macOS / BSD | `:` | `$HOME:$HOME/private/*` |
428433
| Windows | `;` | `$HOME;$HOME/private/*` |
434+
429435
- By default, this is set to `"$HOME"`.
430436
- `_ZO_FZF_OPTS`
431437
- Custom options to pass to [fzf] during interactive selection. See

rustfmt.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ use_field_init_shorthand = true
55
use_small_heuristics = "Max"
66
use_try_shorthand = true
77
wrap_comments = true
8-
version = "Two"
8+
style_edition = "2024"

shell.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
let
22
pkgs = import (builtins.fetchTarball
3-
"https://github.com/NixOS/nixpkgs/archive/4d513ab5f170d66afa3387bdd718d41aa936ee9f.tar.gz") {
3+
"https://github.com/NixOS/nixpkgs/archive/056faf24027e12f0ba6edebe299ed136e030d29a.tar.gz") {
44
overlays = [ rust ];
55
};
66
rust = import (builtins.fetchTarball
7-
"https://github.com/oxalica/rust-overlay/archive/ab150c7412db7bea5879ce2776718f53fba37aa2.tar.gz");
7+
"https://github.com/oxalica/rust-overlay/archive/f61820fa2c3844d6940cce269a6afdec30aa2e6c.tar.gz");
88

99
rust-nightly =
1010
pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.minimal);

src/cmd/add.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::Path;
22

3-
use anyhow::{bail, Result};
3+
use anyhow::{Result, bail};
44

55
use crate::cmd::{Add, Run};
66
use crate::db::Database;

src/cmd/import.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fs;
22

3-
use anyhow::{bail, Context, Result};
3+
use anyhow::{Context, Result, bail};
44

55
use crate::cmd::{Import, ImportFrom, Run};
66
use crate::db::Database;

src/cmd/remove.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{bail, Result};
1+
use anyhow::{Result, bail};
22

33
use crate::cmd::{Remove, Run};
44
use crate::db::Database;

src/config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22
use std::ffi::OsString;
33
use std::path::PathBuf;
44

5-
use anyhow::{ensure, Context, Result};
5+
use anyhow::{Context, Result, ensure};
66
use glob::Pattern;
77

88
use crate::db::Rank;
@@ -20,7 +20,7 @@ pub fn data_dir() -> Result<PathBuf> {
2020
}
2121

2222
pub fn echo() -> bool {
23-
env::var_os("_ZO_ECHO").map_or(false, |var| var == "1")
23+
env::var_os("_ZO_ECHO").is_some_and(|var| var == "1")
2424
}
2525

2626
pub fn exclude_dirs() -> Result<Vec<Pattern>> {
@@ -58,5 +58,5 @@ pub fn maxage() -> Result<Rank> {
5858
}
5959

6060
pub fn resolve_symlinks() -> bool {
61-
env::var_os("_ZO_RESOLVE_SYMLINKS").map_or(false, |var| var == "1")
61+
env::var_os("_ZO_RESOLVE_SYMLINKS").is_some_and(|var| var == "1")
6262
}

src/db/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod stream;
44
use std::path::{Path, PathBuf};
55
use std::{fs, io};
66

7-
use anyhow::{bail, Context, Result};
7+
use anyhow::{Context, Result, bail};
88
use bincode::Options;
99
use ouroboros::self_referencing;
1010

src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::{self, Display, Formatter};
22
use std::io;
33

4-
use anyhow::{bail, Context, Result};
4+
use anyhow::{Context, Result, bail};
55

66
/// Custom error type for early exit.
77
#[derive(Debug)]

src/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{env, mem};
88

99
#[cfg(windows)]
1010
use anyhow::anyhow;
11-
use anyhow::{bail, Context, Result};
11+
use anyhow::{Context, Result, bail};
1212

1313
use crate::db::{Dir, Epoch};
1414
use crate::error::SilentExit;

templates/nushell.txt

+40-22
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,40 @@
1212

1313
{%- else -%}
1414
# Initialize hook to add new entries to the database.
15-
if (not ($env | default false __zoxide_hooked | get __zoxide_hooked)) {
16-
$env.__zoxide_hooked = true
15+
export-env {
1716
{%- if hook == InitHook::Prompt %}
18-
$env.config = ($env | default {} config).config
19-
$env.config = ($env.config | default {} hooks)
20-
$env.config = ($env.config | update hooks ($env.config.hooks | default [] pre_prompt))
21-
$env.config = ($env.config | update hooks.pre_prompt ($env.config.hooks.pre_prompt | append { ||
22-
zoxide add -- $env.PWD
23-
}))
17+
$env.config = (
18+
$env.config?
19+
| default {}
20+
| upsert hooks { default {} }
21+
| upsert hooks.pre_prompt { default [] }
22+
)
23+
let __zoxide_hooked = (
24+
$env.config.hooks.pre_prompt | any { try { get __zoxide_hook } catch { false } }
25+
)
26+
if not $__zoxide_hooked {
27+
$env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | append {
28+
__zoxide_hook: true,
29+
code: {|| zoxide add -- $env.PWD}
30+
})
31+
}
2432
{%- else if hook == InitHook::Pwd %}
25-
$env.config = ($env | default {} config).config
26-
$env.config = ($env.config | default {} hooks)
27-
$env.config = ($env.config | update hooks ($env.config.hooks | default {} env_change))
28-
$env.config = ($env.config | update hooks.env_change ($env.config.hooks.env_change | default [] PWD))
29-
$env.config = ($env.config | update hooks.env_change.PWD ($env.config.hooks.env_change.PWD | append {|_, dir|
30-
zoxide add -- $dir
31-
}))
33+
$env.config = (
34+
$env.config?
35+
| default {}
36+
| upsert hooks { default {} }
37+
| upsert hooks.env_change { default {} }
38+
| upsert hooks.env_change.PWD { default [] }
39+
)
40+
let __zoxide_hooked = (
41+
$env.config.hooks.env_change.PWD | any { try { get __zoxide_hook } catch { false } }
42+
)
43+
if not $__zoxide_hooked {
44+
$env.config.hooks.env_change.PWD = ($env.config.hooks.env_change.PWD | append {
45+
__zoxide_hook: true,
46+
code: {|_, dir| zoxide add -- $dir}
47+
})
48+
}
3249
{%- endif %}
3350
}
3451

@@ -39,13 +56,14 @@ if (not ($env | default false __zoxide_hooked | get __zoxide_hooked)) {
3956
#
4057

4158
# Jump to a directory using only keywords.
42-
def --env --wrapped __zoxide_z [...rest:string] {
43-
let arg0 = ($rest | append '~').0
44-
let arg0_is_dir = (try {$arg0 | path expand | path type}) == 'dir'
45-
let path = if (($rest | length) <= 1) and ($arg0 == '-' or $arg0_is_dir) {
46-
$arg0
47-
} else {
48-
(zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n")
59+
def --env --wrapped __zoxide_z [...rest: string] {
60+
let path = match $rest {
61+
[] => {'~'},
62+
[ '-' ] => {'-'},
63+
[ $arg ] if ($arg | path type) == 'dir' => {$arg}
64+
_ => {
65+
zoxide query --exclude $env.PWD -- ...$rest | str trim -r -c "\n"
66+
}
4967
}
5068
cd $path
5169
{%- if echo %}

templates/xonsh.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ def __zoxide_pwd() -> str:
4343
return pwd
4444

4545

46-
def __zoxide_cd(path: typing.Optional[typing.AnyStr] = None) -> None:
46+
def __zoxide_cd(path: str | bytes | None = None) -> None:
4747
"""cd + custom logic based on the value of _ZO_ECHO."""
4848
if path is None:
4949
args = []
5050
elif isinstance(path, bytes):
5151
args = [path.decode("utf-8")]
52-
elif isinstance(path, str):
52+
else:
5353
args = [path]
5454
_, exc, _ = xonsh.dirstack.cd(args)
5555
if exc is not None:

0 commit comments

Comments
 (0)