From da0fdb2bae01394398f2cada7e4a9e645cd81004 Mon Sep 17 00:00:00 2001 From: Bahex Date: Mon, 10 Feb 2025 00:17:41 +0300 Subject: [PATCH 1/4] Update nushell init script (#966) --- README.md | 6 +++++ rustfmt.toml | 2 +- shell.nix | 4 +-- src/cmd/add.rs | 2 +- src/cmd/import.rs | 2 +- src/cmd/remove.rs | 2 +- src/config.rs | 6 ++--- src/db/mod.rs | 2 +- src/error.rs | 2 +- src/util.rs | 2 +- templates/nushell.txt | 62 ++++++++++++++++++++++++++++--------------- templates/xonsh.txt | 4 +-- 12 files changed, 60 insertions(+), 36 deletions(-) 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: From 06f9f3f27ccebe19bd1e04b9a312c489b9c64b48 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Mon, 10 Feb 2025 03:10:20 +0530 Subject: [PATCH 2/4] Release v0.9.7 --- CHANGELOG.md | 11 +++++++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- templates/xonsh.txt | 12 ++++++------ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1bd8c3..7ddda73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.9.7] - 2025-02-10 + +### Added + +- Nushell: support for 0.102.0. +- Bash / Zsh: add doctor to diagnose common issues. + +### Fixed + +- ksh: alias to regular POSIX implementation for better compatibility. + ## [0.9.6] - 2024-09-19 ### Fixed diff --git a/Cargo.lock b/Cargo.lock index e041a4a..2e9124f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1115,7 +1115,7 @@ dependencies = [ [[package]] name = "zoxide" -version = "0.9.6" +version = "0.9.7" dependencies = [ "anyhow", "assert_cmd", diff --git a/Cargo.toml b/Cargo.toml index aa5d941..7835ccc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ name = "zoxide" readme = "README.md" repository = "https://github.com/ajeetdsouza/zoxide" rust-version = "1.74.1" -version = "0.9.6" +version = "0.9.7" [badges] maintenance = { status = "actively-developed" } diff --git a/templates/xonsh.txt b/templates/xonsh.txt index a95dded..c769795 100644 --- a/templates/xonsh.txt +++ b/templates/xonsh.txt @@ -26,7 +26,7 @@ def __zoxide_bin() -> str: return zoxide -def __zoxide_env() -> typing.Dict[str, str]: +def __zoxide_env() -> dict[str, str]: """Returns the current environment.""" return builtins.__xonsh__.env.detype() # type: ignore # pylint:disable=no-member @@ -64,11 +64,11 @@ class ZoxideSilentException(Exception): def __zoxide_errhandler( - func: typing.Callable[[typing.List[str]], None] -) -> typing.Callable[[typing.List[str]], int]: + func: typing.Callable[[list[str]], None] +) -> typing.Callable[[list[str]], int]: """Print exception and exit with error code 1.""" - def wrapper(args: typing.List[str]) -> int: + def wrapper(args: list[str]) -> int: try: func(args) return 0 @@ -113,7 +113,7 @@ if "__zoxide_hook" not in globals(): @__zoxide_errhandler -def __zoxide_z(args: typing.List[str]) -> None: +def __zoxide_z(args: list[str]) -> None: """Jump to a directory using only keywords.""" if args == []: __zoxide_cd() @@ -138,7 +138,7 @@ def __zoxide_z(args: typing.List[str]) -> None: @__zoxide_errhandler -def __zoxide_zi(args: typing.List[str]) -> None: +def __zoxide_zi(args: list[str]) -> None: """Jump to a directory using interactive search.""" try: zoxide = __zoxide_bin() From d74bce3b7418ed965f5056297db8bb081a29121c Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Mon, 10 Feb 2025 03:19:05 +0530 Subject: [PATCH 4/4] Fix link in CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ddda73..419c5ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -512,6 +512,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - GitHub Actions pipeline to build and upload releases. - Add support for Zsh. +[0.9.7]: https://github.com/ajeetdsouza/zoxide/compare/v0.9.6...v0.9.7 [0.9.6]: https://github.com/ajeetdsouza/zoxide/compare/v0.9.5...v0.9.6 [0.9.5]: https://github.com/ajeetdsouza/zoxide/compare/v0.9.4...v0.9.5 [0.9.4]: https://github.com/ajeetdsouza/zoxide/compare/v0.9.3...v0.9.4