diff --git a/README.md b/README.md index 2f0140f..130f741 100644 --- a/README.md +++ b/README.md @@ -392,11 +392,13 @@ When calling `zoxide init`, the following flags are available: - `--cmd cd` would replace the `cd` command. - `--hook ` - Changes how often zoxide increments a directory's score: + | Hook | Description | | --------------- | --------------------------------- | | `none` | Never | | `prompt` | At every shell prompt | | `pwd` (default) | Whenever the directory is changed | + - `--no-cmd` - Prevents zoxide from defining the `z` and `zi` commands. - These functions will still be available in your shell as `__zoxide_z` and @@ -410,11 +412,13 @@ Environment variables[^2] can be used for configuration. They must be set before - `_ZO_DATA_DIR` - Specifies the directory in which the database is stored. - The default value varies across OSes: + | OS | Path | Example | | ----------- | ---------------------------------------- | ------------------------------------------ | | Linux / BSD | `$XDG_DATA_HOME` or `$HOME/.local/share` | `/home/alice/.local/share` | | macOS | `$HOME/Library/Application Support` | `/Users/Alice/Library/Application Support` | | Windows | `%LOCALAPPDATA%` | `C:\Users\Alice\AppData\Local` | + - `_ZO_ECHO` - When set to 1, `z` will print the matched directory before navigating to it. @@ -422,10 +426,12 @@ Environment variables[^2] can be used for configuration. They must be set before - Excludes the specified directories from the database. - This is provided as a list of [globs][glob], separated by OS-specific characters: + | OS | Separator | Example | | ------------------- | --------- | ----------------------- | | Linux / macOS / BSD | `:` | `$HOME:$HOME/private/*` | | Windows | `;` | `$HOME;$HOME/private/*` | + - By default, this is set to `"$HOME"`. - `_ZO_FZF_OPTS` - Custom options to pass to [fzf] during interactive selection. See diff --git a/rustfmt.toml b/rustfmt.toml index 024f400..72efef7 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -5,4 +5,4 @@ use_field_init_shorthand = true use_small_heuristics = "Max" use_try_shorthand = true wrap_comments = true -version = "Two" +style_edition = "2024" diff --git a/shell.nix b/shell.nix index 10bed4b..b63d44c 100644 --- a/shell.nix +++ b/shell.nix @@ -1,10 +1,10 @@ let pkgs = import (builtins.fetchTarball - "https://github.com/NixOS/nixpkgs/archive/4d513ab5f170d66afa3387bdd718d41aa936ee9f.tar.gz") { + "https://github.com/NixOS/nixpkgs/archive/056faf24027e12f0ba6edebe299ed136e030d29a.tar.gz") { overlays = [ rust ]; }; rust = import (builtins.fetchTarball - "https://github.com/oxalica/rust-overlay/archive/ab150c7412db7bea5879ce2776718f53fba37aa2.tar.gz"); + "https://github.com/oxalica/rust-overlay/archive/f61820fa2c3844d6940cce269a6afdec30aa2e6c.tar.gz"); rust-nightly = pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.minimal); diff --git a/src/cmd/add.rs b/src/cmd/add.rs index 945bbe5..e7d6e0b 100644 --- a/src/cmd/add.rs +++ b/src/cmd/add.rs @@ -1,6 +1,6 @@ use std::path::Path; -use anyhow::{bail, Result}; +use anyhow::{Result, bail}; use crate::cmd::{Add, Run}; use crate::db::Database; diff --git a/src/cmd/import.rs b/src/cmd/import.rs index 182a25f..f13381c 100644 --- a/src/cmd/import.rs +++ b/src/cmd/import.rs @@ -1,6 +1,6 @@ use std::fs; -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result, bail}; use crate::cmd::{Import, ImportFrom, Run}; use crate::db::Database; diff --git a/src/cmd/remove.rs b/src/cmd/remove.rs index 55c6989..85204ea 100644 --- a/src/cmd/remove.rs +++ b/src/cmd/remove.rs @@ -1,4 +1,4 @@ -use anyhow::{bail, Result}; +use anyhow::{Result, bail}; use crate::cmd::{Remove, Run}; use crate::db::Database; diff --git a/src/config.rs b/src/config.rs index 4b46a78..0aeda5c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,7 @@ use std::env; use std::ffi::OsString; use std::path::PathBuf; -use anyhow::{ensure, Context, Result}; +use anyhow::{Context, Result, ensure}; use glob::Pattern; use crate::db::Rank; @@ -20,7 +20,7 @@ pub fn data_dir() -> Result { } pub fn echo() -> bool { - env::var_os("_ZO_ECHO").map_or(false, |var| var == "1") + env::var_os("_ZO_ECHO").is_some_and(|var| var == "1") } pub fn exclude_dirs() -> Result> { @@ -58,5 +58,5 @@ pub fn maxage() -> Result { } pub fn resolve_symlinks() -> bool { - env::var_os("_ZO_RESOLVE_SYMLINKS").map_or(false, |var| var == "1") + env::var_os("_ZO_RESOLVE_SYMLINKS").is_some_and(|var| var == "1") } diff --git a/src/db/mod.rs b/src/db/mod.rs index 1eae1a9..a19efe9 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -4,7 +4,7 @@ mod stream; use std::path::{Path, PathBuf}; use std::{fs, io}; -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result, bail}; use bincode::Options; use ouroboros::self_referencing; diff --git a/src/error.rs b/src/error.rs index d2baa7f..0850f50 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,7 @@ use std::fmt::{self, Display, Formatter}; use std::io; -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result, bail}; /// Custom error type for early exit. #[derive(Debug)] diff --git a/src/util.rs b/src/util.rs index 1f8fc95..9f6689d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -8,7 +8,7 @@ use std::{env, mem}; #[cfg(windows)] use anyhow::anyhow; -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result, bail}; use crate::db::{Dir, Epoch}; use crate::error::SilentExit; diff --git a/templates/nushell.txt b/templates/nushell.txt index 90d5cba..67285f7 100644 --- a/templates/nushell.txt +++ b/templates/nushell.txt @@ -12,23 +12,40 @@ {%- else -%} # Initialize hook to add new entries to the database. -if (not ($env | default false __zoxide_hooked | get __zoxide_hooked)) { - $env.__zoxide_hooked = true +export-env { {%- if hook == InitHook::Prompt %} - $env.config = ($env | default {} config).config - $env.config = ($env.config | default {} hooks) - $env.config = ($env.config | update hooks ($env.config.hooks | default [] pre_prompt)) - $env.config = ($env.config | update hooks.pre_prompt ($env.config.hooks.pre_prompt | append { || - zoxide add -- $env.PWD - })) + $env.config = ( + $env.config? + | default {} + | upsert hooks { default {} } + | upsert hooks.pre_prompt { default [] } + ) + let __zoxide_hooked = ( + $env.config.hooks.pre_prompt | any { try { get __zoxide_hook } catch { false } } + ) + if not $__zoxide_hooked { + $env.config.hooks.pre_prompt = ($env.config.hooks.pre_prompt | append { + __zoxide_hook: true, + code: {|| zoxide add -- $env.PWD} + }) + } {%- else if hook == InitHook::Pwd %} - $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 - })) + $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} + }) + } {%- endif %} } @@ -39,13 +56,14 @@ if (not ($env | default false __zoxide_hooked | get __zoxide_hooked)) { # # Jump to a directory using only keywords. -def --env --wrapped __zoxide_z [...rest:string] { - let arg0 = ($rest | append '~').0 - let arg0_is_dir = (try {$arg0 | path expand | path type}) == 'dir' - let path = if (($rest | length) <= 1) and ($arg0 == '-' or $arg0_is_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 {%- if echo %} diff --git a/templates/xonsh.txt b/templates/xonsh.txt index 2ca29a7..a95dded 100644 --- a/templates/xonsh.txt +++ b/templates/xonsh.txt @@ -43,13 +43,13 @@ def __zoxide_pwd() -> str: return pwd -def __zoxide_cd(path: typing.Optional[typing.AnyStr] = None) -> None: +def __zoxide_cd(path: str | bytes | None = None) -> None: """cd + custom logic based on the value of _ZO_ECHO.""" if path is None: args = [] elif isinstance(path, bytes): args = [path.decode("utf-8")] - elif isinstance(path, str): + else: args = [path] _, exc, _ = xonsh.dirstack.cd(args) if exc is not None: