From 697afe7ba630abb3cd2b11a9ce5528eef0dbef2c Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Thu, 29 Apr 2021 01:23:43 +0530 Subject: [PATCH 1/2] Fix return values in Bash hook (#196) --- README.md | 28 ++++++++++++++++------------ src/db/query.rs | 2 +- templates/bash.txt | 12 ++++++++++++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 6f4b224..691ecbd 100644 --- a/README.md +++ b/README.md @@ -42,18 +42,20 @@ If you would rather not run a script, you can download the binary from the #### On Linux -| Distribution | Repository | Instructions | -| -------------- | ----------------------- | --------------------------------------------------------- | -| **Any** | [crates.io] | `cargo install zoxide` | -| **Any** | [Linuxbrew] | `brew install zoxide` | -| Alpine Linux | [Alpine Linux Packages] | `apk add zoxide` | -| Arch Linux | [AUR] | `yay -Sy zoxide-bin` | -| CentOS | [Copr] | `dnf copr enable atim/zoxide`
`dnf install zoxide` | -| Debian Testing | [Debian Packages] | `apt install zoxide` | -| Fedora | [Fedora Packages] | `dnf install zoxide` | -| NixOS | [nixpkgs] | `nix-env -iA nixpkgs.zoxide` | -| Parrot OS | | `apt install zoxide` | -| Void Linux | [Void Linux Packages] | `xbps-install -S zoxide` | +| Distribution | Repository | Instructions | +| ------------------ | ----------------------- | --------------------------------------------------------- | +| **Any** | [crates.io] | `cargo install zoxide` | +| **Any** | [Linuxbrew] | `brew install zoxide` | +| Alpine Linux 3.13+ | [Alpine Linux Packages] | `apk add zoxide` | +| Arch Linux | [AUR] | `yay -Sy zoxide-bin` | +| CentOS 7+ | [Copr] | `dnf copr enable atim/zoxide`
`dnf install zoxide` | +| Debian Testing | [Debian Packages] | `apt install zoxide` | +| Devuan 4.0+ | [Devuan Packages] | `apt install zoxide` | +| Fedora 32+ | [Fedora Packages] | `dnf install zoxide` | +| NixOS | [nixpkgs] | `nix-env -iA nixpkgs.zoxide` | +| Parrot OS | | `apt install zoxide` | +| Ubuntu 21.04+ | [Ubuntu Packages] | `apt install zoxide` | +| Void Linux | [Void Linux Packages] | `xbps-install -S zoxide` | #### On macOS @@ -244,6 +246,7 @@ Be sure to set these before calling `zoxide init`. [crates.io]: https://crates.io/crates/zoxide [debian packages]: https://packages.debian.org/testing/admin/zoxide [demo.gif]: demo.gif +[devuan packages]: https://pkginfo.devuan.org/cgi-bin/package-query.html?c=package&q=zoxide [dports]: https://github.com/DragonFlyBSD/DPorts/tree/master/sysutils/zoxide [fedora packages]: https://src.fedoraproject.org/rpms/rust-zoxide [freshports]: https://www.freshports.org/sysutils/zoxide/ @@ -260,6 +263,7 @@ Be sure to set these before calling `zoxide init`. [releases]: https://github.com/ajeetdsouza/zoxide/releases [scoop]: https://github.com/ScoopInstaller/Main/tree/master/bucket/zoxide.json [termux]: https://github.com/termux/termux-packages/tree/master/packages/zoxide +[ubuntu packages]: https://packages.ubuntu.com/hirsute/zoxide [void linux packages]: https://github.com/void-linux/void-packages/tree/master/srcpkgs/zoxide [xxh-plugin-prerun-zoxide]: https://github.com/xxh/xxh-plugin-prerun-zoxide [xxh]: https://github.com/xxh/xxh diff --git a/src/db/query.rs b/src/db/query.rs index 1f9721b..7a61a65 100644 --- a/src/db/query.rs +++ b/src/db/query.rs @@ -8,7 +8,7 @@ impl Query { I: IntoIterator, S: AsRef, { - Query(keywords.into_iter().map(|s: S| to_lowercase(s)).collect()) + Query(keywords.into_iter().map(to_lowercase).collect()) } pub fn matches>(&self, path: S) -> bool { diff --git a/templates/bash.txt b/templates/bash.txt index e2245f6..0c8e675 100644 --- a/templates/bash.txt +++ b/templates/bash.txt @@ -24,6 +24,10 @@ function __zoxide_cd() { # Hook configuration for zoxide. # +{# Custom prompts often use "$?" to show the exit status of the previous + # command. Adding __zoxide_hook to the front of $PROMPT_COMMAND would change + # the exit status, so we must capture it and return it manually instead. -#} + # Hook to add new entries to the database. {%- match hook %} {%- when Hook::None %} @@ -31,11 +35,14 @@ function __zoxide_cd() { {%- when Hook::Prompt %} function __zoxide_hook() { + \builtin local -r __zoxide_retval="$?" zoxide add -- "$(__zoxide_pwd)" + return "${__zoxide_retval}" } {%- when Hook::Pwd %} function __zoxide_hook() { + \builtin local -r __zoxide_retval="$?" \builtin local -r __zoxide_pwd_tmp="$(__zoxide_pwd)" if [ -z "${__zoxide_pwd_old}" ]; then __zoxide_pwd_old="${__zoxide_pwd_tmp}" @@ -43,10 +50,15 @@ function __zoxide_hook() { __zoxide_pwd_old="${__zoxide_pwd_tmp}" zoxide add -- "${__zoxide_pwd_old}" fi + return "${__zoxide_retval}" } {%- endmatch %} +{# bash throws an error if $PROMPT_COMMAND contains two semicolons in sequence. + # This is hard to avoid perfectly, but adding __zoxide_hook to the front of + # $PROMPT_COMMAND rather than the back makes this scenario unlikely. -#} + # Initialize hook. if [ "${__zoxide_hooked}" != '1' ]; then __zoxide_hooked='1' From 0eb4418fd6684d0cdf21bbaae8985643404a5aec Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Thu, 29 Apr 2021 01:24:25 +0530 Subject: [PATCH 2/2] _ZO_EXCLUDE_DIRS should default to "$HOME" (#194) --- CHANGELOG.md | 4 ++++ README.md | 1 + man/zoxide.1 | 4 ++-- src/config.rs | 16 +++++++++++++--- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64b79aa..33ceacb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Manpages for each subcommand. - Default prompt for Nushell. +### Changed + +- `$_ZO_EXCLUDE_DIRS` now defaults to `"$HOME"`. + ### Fixed - `cd -` on fish shells. diff --git a/README.md b/README.md index 691ecbd..8396814 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,7 @@ Be sure to set these before calling `zoxide init`. | ------------------- | --------- | ----------------------- | | 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`][fzf]. See `man fzf` for the list of options. diff --git a/man/zoxide.1 b/man/zoxide.1 index b047754..5364ffe 100644 --- a/man/zoxide.1 +++ b/man/zoxide.1 @@ -84,8 +84,8 @@ T} Windows|\fI;\fR eg. $HOME;$HOME/private/* .TE .sp -After setting this up, you might need to use \fBzoxide-remove\fR(1) to remove -any existing entries from the database. +By default, this is set to \fI"$HOME"\fR. After setting this up, you might need +to use \fBzoxide-remove\fR(1) to remove any existing entries from the database. .TP .B _ZO_FZF_OPTS Custom options to pass to fzf. See \fBman fzf\fR for the list of options. diff --git a/src/config.rs b/src/config.rs index 690d549..d471891 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,7 @@ use crate::db::Rank; use anyhow::{bail, Context, Result}; use dirs_next as dirs; +use glob::Pattern; use std::env; use std::ffi::OsString; @@ -29,18 +30,27 @@ pub fn zo_echo() -> bool { } } -pub fn zo_exclude_dirs() -> Result> { +pub fn zo_exclude_dirs() -> Result> { match env::var_os("_ZO_EXCLUDE_DIRS") { Some(dirs_osstr) => env::split_paths(&dirs_osstr) .map(|path| { let pattern = path .to_str() .context("invalid unicode in _ZO_EXCLUDE_DIRS")?; - glob::Pattern::new(&pattern) + Pattern::new(&pattern) .with_context(|| format!("invalid glob in _ZO_EXCLUDE_DIRS: {}", pattern)) }) .collect(), - None => Ok(Vec::new()), + None => { + let pattern = (|| { + let home = dirs::home_dir()?; + let home_str = home.to_str()?; + let home_esc = Pattern::escape(home_str); + Pattern::new(&home_esc).ok() + })(); + + Ok(pattern.into_iter().collect()) + } } }