Clobber conflicting definitions in shells

This commit is contained in:
Ajeet D'Souza 2020-10-24 20:23:10 +05:30
parent c0a1a78c29
commit bc17c25cf6
9 changed files with 174 additions and 24 deletions

View File

@ -15,6 +15,7 @@ jobs:
- run: sudo apt update - run: sudo apt update
- run: sudo add-apt-repository universe - run: sudo add-apt-repository universe
- run: sudo apt install bash dash fish powershell shellcheck xonsh zsh - run: sudo apt install bash dash fish powershell shellcheck xonsh zsh
- run: sudo snap install shfmt
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1 - uses: actions-rs/toolchain@v1
with: with:

View File

@ -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`. - `zoxide init --no-aliases` no longer generates `z` or `zi`.
### Fixed
- Clobber conflicting alias definitions in Bash/POSIX/Zsh shells.
### Removed ### Removed
- Deprecated PWD hooks for POSIX shells. - Deprecated PWD hooks for POSIX shells.

View File

@ -31,6 +31,10 @@ pub struct Opts<'a> {
pub resolve_symlinks: bool, pub resolve_symlinks: bool,
} }
impl Opts<'_> {
pub const DEVNULL: &'static str = if cfg!(windows) { "NUL" } else { "/dev/null" };
}
#[derive(Debug, Template)] #[derive(Debug, Template)]
#[template(path = "bash.txt")] #[template(path = "bash.txt")]
pub struct Bash<'a>(pub &'a Opts<'a>); pub struct Bash<'a>(pub &'a Opts<'a>);

View File

@ -119,16 +119,48 @@ function __zoxide_zri() {
{%- match cmd %} {%- match cmd %}
{%- when Some with (cmd) %} {%- when Some with (cmd) %}
alias {{cmd}}='__zoxide_z' # Remove definitions.
alias {{cmd}}i='__zoxide_zi' 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' __zoxide_unset '{{cmd}}i'
alias {{cmd}}qi='__zoxide_zqi' function {{cmd}}i() {
__zoxide_zi "$@"
}
alias {{cmd}}r='__zoxide_zr' __zoxide_unset '{{cmd}}a'
alias {{cmd}}ri='__zoxide_zri' 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 %} {%- when None %}
{{ NOT_CONFIGURED }} {{ NOT_CONFIGURED }}

View File

@ -5,6 +5,8 @@
# Utility functions for zoxide. # Utility functions for zoxide.
# #
# Remove definitions.
# pwd based on the value of _ZO_RESOLVE_SYMLINKS. # pwd based on the value of _ZO_RESOLVE_SYMLINKS.
function __zoxide_pwd function __zoxide_pwd
{%- if resolve_symlinks %} {%- if resolve_symlinks %}
@ -99,30 +101,44 @@ end
{%- match cmd %} {%- match cmd %}
{%- when Some with (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}} function {{cmd}}
__zoxide_z $argv __zoxide_z $argv
end end
__zoxide_unset '{{cmd}}i'
function {{cmd}}i function {{cmd}}i
__zoxide_zi $argv __zoxide_zi $argv
end end
__zoxide_unset '{{cmd}}a'
function {{cmd}}a function {{cmd}}a
__zoxide_za $argv __zoxide_za $argv
end end
__zoxide_unset '{{cmd}}q'
function {{cmd}}q function {{cmd}}q
__zoxide_zq $argv __zoxide_zq $argv
end end
__zoxide_unset '{{cmd}}qi'
function {{cmd}}qi function {{cmd}}qi
__zoxide_zqi $argv __zoxide_zqi $argv
end end
__zoxide_unset '{{cmd}}r'
function {{cmd}}r function {{cmd}}r
__zoxide_zr $argv __zoxide_zr $argv
end end
__zoxide_unset '{{cmd}}ri'
function {{cmd}}ri function {{cmd}}ri
__zoxide_zri $argv __zoxide_zri $argv
end end

View File

@ -119,16 +119,48 @@ __zoxide_zri() {
{%- match cmd %} {%- match cmd %}
{%- when Some with (cmd) %} {%- when Some with (cmd) %}
alias {{cmd}}='__zoxide_z' # Remove definitions.
alias {{cmd}}i='__zoxide_zi' __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' __zoxide_unset '{{cmd}}i'
alias {{cmd}}qi='__zoxide_zqi' {{cmd}}i() {
__zoxide_zi "$@"
}
alias {{cmd}}r='__zoxide_zr' __zoxide_unset '{{cmd}}a'
alias {{cmd}}ri='__zoxide_zri' {{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 %} {%- when None %}
{{ NOT_CONFIGURED }} {{ NOT_CONFIGURED }}

View File

@ -106,16 +106,47 @@ function __zoxide_zri() {
{%- match cmd %} {%- match cmd %}
{%- when Some with (cmd) %} {%- when Some with (cmd) %}
alias {{cmd}}='__zoxide_z' # Remove definitions.
alias {{cmd}}i='__zoxide_zi' 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' __zoxide_unset '{{cmd}}i'
alias {{cmd}}qi='__zoxide_zqi' function {{cmd}}i() {
__zoxide_zi "$@"
}
alias {{cmd}}r='__zoxide_zr' __zoxide_unset '{{cmd}}a'
alias {{cmd}}ri='__zoxide_zri' 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 %} {%- when None %}
{{ NOT_CONFIGURED }} {{ NOT_CONFIGURED }}

View File

@ -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] #[test]
fn test_xonsh() { fn test_xonsh() {
for opts in opts() { for opts in opts() {

View File

@ -20,9 +20,10 @@ fn env_help() -> &'static str {
format!( format!(
"\ "\
ENVIRONMENT VARIABLES: 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_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_FZF_OPTS Custom flags to pass to fzf
_ZO_MAXAGE Maximum total age after which entries start getting deleted _ZO_MAXAGE Maximum total age after which entries start getting deleted
_ZO_RESOLVE_SYMLINKS Resolve symlinks when storing paths", _ZO_RESOLVE_SYMLINKS Resolve symlinks when storing paths",
@ -37,7 +38,6 @@ ENVIRONMENT VARIABLES:
#[clap( #[clap(
about, about,
author, author,
global_setting(AppSettings::ColoredHelp),
global_setting(AppSettings::GlobalVersion), global_setting(AppSettings::GlobalVersion),
global_setting(AppSettings::VersionlessSubcommands), global_setting(AppSettings::VersionlessSubcommands),
version = env!("ZOXIDE_VERSION"))] version = env!("ZOXIDE_VERSION"))]