From bc17c25cf6eb0ae4015be83393f31e545bbaf115 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Sat, 24 Oct 2020 20:23:10 +0530 Subject: [PATCH] Clobber conflicting definitions in shells --- .github/workflows/cargo-test.yml | 1 + CHANGELOG.md | 4 +++ crates/zoxide-shell/src/lib.rs | 4 +++ crates/zoxide-shell/templates/bash.txt | 46 +++++++++++++++++++++---- crates/zoxide-shell/templates/fish.txt | 16 +++++++++ crates/zoxide-shell/templates/posix.txt | 46 +++++++++++++++++++++---- crates/zoxide-shell/templates/zsh.txt | 45 ++++++++++++++++++++---- crates/zoxide-shell/tests/syntax.rs | 30 ++++++++++++++++ src/main.rs | 6 ++-- 9 files changed, 174 insertions(+), 24 deletions(-) diff --git a/.github/workflows/cargo-test.yml b/.github/workflows/cargo-test.yml index 2b30a87..04b52d1 100644 --- a/.github/workflows/cargo-test.yml +++ b/.github/workflows/cargo-test.yml @@ -15,6 +15,7 @@ jobs: - run: sudo apt update - run: sudo add-apt-repository universe - run: sudo apt install bash dash fish powershell shellcheck xonsh zsh + - run: sudo snap install shfmt - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 with: diff --git a/CHANGELOG.md b/CHANGELOG.md index 35918de..63c6a88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `zoxide init --no-aliases` no longer generates `z` or `zi`. +### Fixed + +- Clobber conflicting alias definitions in Bash/POSIX/Zsh shells. + ### Removed - Deprecated PWD hooks for POSIX shells. diff --git a/crates/zoxide-shell/src/lib.rs b/crates/zoxide-shell/src/lib.rs index 69bf3a4..8cf8506 100644 --- a/crates/zoxide-shell/src/lib.rs +++ b/crates/zoxide-shell/src/lib.rs @@ -31,6 +31,10 @@ pub struct Opts<'a> { pub resolve_symlinks: bool, } +impl Opts<'_> { + pub const DEVNULL: &'static str = if cfg!(windows) { "NUL" } else { "/dev/null" }; +} + #[derive(Debug, Template)] #[template(path = "bash.txt")] pub struct Bash<'a>(pub &'a Opts<'a>); diff --git a/crates/zoxide-shell/templates/bash.txt b/crates/zoxide-shell/templates/bash.txt index 1b16d36..201d25d 100644 --- a/crates/zoxide-shell/templates/bash.txt +++ b/crates/zoxide-shell/templates/bash.txt @@ -119,16 +119,48 @@ function __zoxide_zri() { {%- match cmd %} {%- when Some with (cmd) %} -alias {{cmd}}='__zoxide_z' -alias {{cmd}}i='__zoxide_zi' +# Remove definitions. +function __zoxide_unset() { + # shellcheck disable=SC1001 + \unset -f "$@" &>{{ Opts::DEVNULL }} + # shellcheck disable=SC1001 + \unset -v "$@" &>{{ Opts::DEVNULL }} +} -alias {{cmd}}a='__zoxide_za' +__zoxide_unset '{{cmd}}' +function {{cmd}}() { + __zoxide_z "$@" +} -alias {{cmd}}q='__zoxide_zq' -alias {{cmd}}qi='__zoxide_zqi' +__zoxide_unset '{{cmd}}i' +function {{cmd}}i() { + __zoxide_zi "$@" +} -alias {{cmd}}r='__zoxide_zr' -alias {{cmd}}ri='__zoxide_zri' +__zoxide_unset '{{cmd}}a' +function {{cmd}}a() { + __zoxide_za "$@" +} + +__zoxide_unset '{{cmd}}q' +function {{cmd}}q() { + __zoxide_zq "$@" +} + +__zoxide_unset '{{cmd}}qi' +function {{cmd}}qi() { + __zoxide_zqi "$@" +} + +__zoxide_unset '{{cmd}}r' +function {{cmd}}r() { + __zoxide_zr "$@" +} + +__zoxide_unset '{{cmd}}ri' +function {{cmd}}ri() { + __zoxide_zri "$@" +} {%- when None %} {{ NOT_CONFIGURED }} diff --git a/crates/zoxide-shell/templates/fish.txt b/crates/zoxide-shell/templates/fish.txt index 4340f88..3387ddc 100644 --- a/crates/zoxide-shell/templates/fish.txt +++ b/crates/zoxide-shell/templates/fish.txt @@ -5,6 +5,8 @@ # Utility functions for zoxide. # +# Remove definitions. + # pwd based on the value of _ZO_RESOLVE_SYMLINKS. function __zoxide_pwd {%- if resolve_symlinks %} @@ -99,30 +101,44 @@ end {%- match cmd %} {%- when Some with (cmd) %} +# Remove definitions. +function __zoxide_unset + set --erase $argv > {{ Opts::DEVNULL }} 2>&1 + abbr --erase $argv > {{ Opts::DEVNULL }} 2>&1 + functions --erase $argv > {{ Opts::DEVNULL }} 2>&1 +end + +__zoxide_unset '{{cmd}}' function {{cmd}} __zoxide_z $argv end +__zoxide_unset '{{cmd}}i' function {{cmd}}i __zoxide_zi $argv end +__zoxide_unset '{{cmd}}a' function {{cmd}}a __zoxide_za $argv end +__zoxide_unset '{{cmd}}q' function {{cmd}}q __zoxide_zq $argv end +__zoxide_unset '{{cmd}}qi' function {{cmd}}qi __zoxide_zqi $argv end +__zoxide_unset '{{cmd}}r' function {{cmd}}r __zoxide_zr $argv end +__zoxide_unset '{{cmd}}ri' function {{cmd}}ri __zoxide_zri $argv end diff --git a/crates/zoxide-shell/templates/posix.txt b/crates/zoxide-shell/templates/posix.txt index 0210f26..4056b60 100644 --- a/crates/zoxide-shell/templates/posix.txt +++ b/crates/zoxide-shell/templates/posix.txt @@ -119,16 +119,48 @@ __zoxide_zri() { {%- match cmd %} {%- when Some with (cmd) %} -alias {{cmd}}='__zoxide_z' -alias {{cmd}}i='__zoxide_zi' +# Remove definitions. +__zoxide_unset() { + # shellcheck disable=SC1001 + \unset -f "$@" >{{ Opts::DEVNULL }} 2>&1 + # shellcheck disable=SC1001 + \unset -v "$@" >{{ Opts::DEVNULL }} 2>&1 +} -alias {{cmd}}a='__zoxide_za' +__zoxide_unset '{{cmd}}' +{{cmd}}() { + __zoxide_z "$@" +} -alias {{cmd}}q='__zoxide_zq' -alias {{cmd}}qi='__zoxide_zqi' +__zoxide_unset '{{cmd}}i' +{{cmd}}i() { + __zoxide_zi "$@" +} -alias {{cmd}}r='__zoxide_zr' -alias {{cmd}}ri='__zoxide_zri' +__zoxide_unset '{{cmd}}a' +{{cmd}}a() { + __zoxide_za "$@" +} + +__zoxide_unset '{{cmd}}q' +{{cmd}}q() { + __zoxide_zq "$@" +} + +__zoxide_unset '{{cmd}}qi' +{{cmd}}qi() { + __zoxide_zqi "$@" +} + +__zoxide_unset '{{cmd}}r' +{{cmd}}r() { + __zoxide_zr "$@" +} + +__zoxide_unset '{{cmd}}ri' +{{cmd}}ri() { + __zoxide_zri "$@" +} {%- when None %} {{ NOT_CONFIGURED }} diff --git a/crates/zoxide-shell/templates/zsh.txt b/crates/zoxide-shell/templates/zsh.txt index bac4a47..55ca1c9 100644 --- a/crates/zoxide-shell/templates/zsh.txt +++ b/crates/zoxide-shell/templates/zsh.txt @@ -106,16 +106,47 @@ function __zoxide_zri() { {%- match cmd %} {%- when Some with (cmd) %} -alias {{cmd}}='__zoxide_z' -alias {{cmd}}i='__zoxide_zi' +# Remove definitions. +function __zoxide_unset() { + \unalias "$@" &>{{ Opts::DEVNULL }} + \unfunction "$@" &>{{ Opts::DEVNULL }} + \unset "$@" &>{{ Opts::DEVNULL }} +} -alias {{cmd}}a='__zoxide_za' +__zoxide_unset '{{cmd}}' +function {{cmd}}() { + __zoxide_z "$@" +} -alias {{cmd}}q='__zoxide_zq' -alias {{cmd}}qi='__zoxide_zqi' +__zoxide_unset '{{cmd}}i' +function {{cmd}}i() { + __zoxide_zi "$@" +} -alias {{cmd}}r='__zoxide_zr' -alias {{cmd}}ri='__zoxide_zri' +__zoxide_unset '{{cmd}}a' +function {{cmd}}a() { + __zoxide_za "$@" +} + +__zoxide_unset '{{cmd}}q' +function {{cmd}}q() { + __zoxide_zq "$@" +} + +__zoxide_unset '{{cmd}}qi' +function {{cmd}}qi() { + __zoxide_zqi "$@" +} + +__zoxide_unset '{{cmd}}r' +function {{cmd}}r() { + __zoxide_zr "$@" +} + +__zoxide_unset '{{cmd}}ri' +function {{cmd}}ri() { + __zoxide_zri "$@" +} {%- when None %} {{ NOT_CONFIGURED }} diff --git a/crates/zoxide-shell/tests/syntax.rs b/crates/zoxide-shell/tests/syntax.rs index 84ec495..2904d52 100644 --- a/crates/zoxide-shell/tests/syntax.rs +++ b/crates/zoxide-shell/tests/syntax.rs @@ -124,6 +124,36 @@ fn test_shellcheck_sh() { } } +#[test] +fn test_shfmt_bash() { + for opts in opts() { + let source = crate::Bash(opts).render().unwrap(); + Command::new("shfmt") + .args(&["-d", "-s", "-ln", "bash", "-i", "4", "-ci", "-"]) + .write_stdin(source.as_bytes()) + .write_stdin(b"\n".as_ref()) + .assert() + .success() + .stdout("") + .stderr(""); + } +} + +#[test] +fn test_shfmt_posix() { + for opts in opts() { + let source = crate::Posix(opts).render().unwrap(); + Command::new("shfmt") + .args(&["-d", "-s", "-ln", "posix", "-i", "4", "-ci", "-"]) + .write_stdin(source.as_bytes()) + .write_stdin(b"\n".as_ref()) + .assert() + .success() + .stdout("") + .stderr(""); + } +} + #[test] fn test_xonsh() { for opts in opts() { diff --git a/src/main.rs b/src/main.rs index 9852b0f..5902f73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,9 +20,10 @@ fn env_help() -> &'static str { format!( "\ ENVIRONMENT VARIABLES: - _ZO_DATA_DIR Path for zoxide data files (current: `{data_dir}`) + _ZO_DATA_DIR Path for zoxide data files + [current: {data_dir}] _ZO_ECHO Prints the matched directory before navigating to it when set to 1 - _ZO_EXCLUDE_DIRS List of directories to be excluded, separated by `{split_paths_separator}` + _ZO_EXCLUDE_DIRS List of directory globs to be excluded, separated by '{split_paths_separator}' _ZO_FZF_OPTS Custom flags to pass to fzf _ZO_MAXAGE Maximum total age after which entries start getting deleted _ZO_RESOLVE_SYMLINKS Resolve symlinks when storing paths", @@ -37,7 +38,6 @@ ENVIRONMENT VARIABLES: #[clap( about, author, - global_setting(AppSettings::ColoredHelp), global_setting(AppSettings::GlobalVersion), global_setting(AppSettings::VersionlessSubcommands), version = env!("ZOXIDE_VERSION"))]