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] 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'