diff --git a/Cargo.lock b/Cargo.lock index 031bc34..1504580 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -343,9 +343,9 @@ checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" [[package]] name = "ordered-float" -version = "2.1.1" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "766f840da25490628d8e63e529cd21c014f6600c6b8517add12a6fa6167a6218" +checksum = "b50b8919aecb97e5ee9aceef27e24f39c46b11831130f4a6b7b091ec5de0de12" dependencies = [ "num-traits", ] diff --git a/build.rs b/build.rs index 5441331..dd5e40c 100644 --- a/build.rs +++ b/build.rs @@ -1,23 +1,6 @@ -use clap::IntoApp; -use clap_generate::{generate_to, generators::*}; - use std::env; use std::process::Command; -include!("src/app.rs"); - -fn completions() { - let mut app = Cli::into_app(); - let bin_name = env!("CARGO_PKG_NAME"); - let out_dir = "contrib/completions"; - - generate_to::(&mut app, bin_name, out_dir); - generate_to::(&mut app, bin_name, out_dir); - generate_to::(&mut app, bin_name, out_dir); - generate_to::(&mut app, bin_name, out_dir); - generate_to::(&mut app, bin_name, out_dir); -} - fn git_version() -> Option { // Packages releases of zoxide almost always use the source tarball // provided by GitHub, which does not include the `.git` folder. Since this @@ -35,8 +18,29 @@ fn crate_version() -> String { format!("v{}", env::var("CARGO_PKG_VERSION").unwrap()) } +fn generate_completions() { + mod app { + include!("src/app.rs"); + } + + use app::App; + use clap::IntoApp; + use clap_generate::generate_to; + use clap_generate::generators::{Bash, Elvish, Fish, PowerShell, Zsh}; + + let app = &mut App::into_app(); + let bin_name = &env::var("CARGO_PKG_NAME").unwrap(); + let out_dir = "contrib/completions"; + + generate_to::(app, bin_name, out_dir); + generate_to::(app, bin_name, out_dir); + generate_to::(app, bin_name, out_dir); + generate_to::(app, bin_name, out_dir); + generate_to::(app, bin_name, out_dir); +} + fn main() { let version = git_version().unwrap_or_else(crate_version); println!("cargo:rustc-env=ZOXIDE_VERSION={}", version); - completions(); + generate_completions(); } diff --git a/contrib/completions/README.md b/contrib/completions/README.md new file mode 100644 index 0000000..2de3803 --- /dev/null +++ b/contrib/completions/README.md @@ -0,0 +1,8 @@ +# completions + +Shell completions for zoxide, auto-generated by [`clap`][clap]. + +Since `clap` itself is in beta at the moment, these completions should not be +treated as stable either. + +[clap]: https://github.com/clap-rs/clap diff --git a/src/app.rs b/src/app.rs index 96c552f..92a7c34 100644 --- a/src/app.rs +++ b/src/app.rs @@ -20,9 +20,9 @@ const ENV_HELP: &str = "ENVIRONMENT VARIABLES: global_setting(AppSettings::DisableHelpSubcommand), global_setting(AppSettings::GlobalVersion), global_setting(AppSettings::VersionlessSubcommands), - version = option_env!("ZOXIDE_VERSION").unwrap_or("") + version = option_env!("ZOXIDE_VERSION").unwrap_or_default() )] -pub enum Cli { +pub enum App { Add(Add), Import(Import), Init(Init), @@ -43,7 +43,7 @@ pub struct Import { /// Application to import from #[clap(arg_enum, long)] - pub from: From, + pub from: ImportFrom, /// Merge into existing database #[clap(long)] @@ -51,7 +51,7 @@ pub struct Import { } #[derive(ArgEnum, Debug)] -pub enum From { +pub enum ImportFrom { Autojump, Z, } @@ -60,7 +60,7 @@ pub enum From { #[derive(Clap, Debug)] pub struct Init { #[clap(arg_enum)] - pub shell: Shell, + pub shell: InitShell, /// Prevents zoxide from defining any commands #[clap(long)] @@ -72,11 +72,18 @@ pub struct Init { /// Chooses event upon which an entry is added to the database #[clap(arg_enum, long, default_value = "pwd")] - pub hook: Hook, + pub hook: InitHook, +} + +#[derive(ArgEnum, Clone, Copy, Debug, Eq, PartialEq)] +pub enum InitHook { + None, + Prompt, + Pwd, } #[derive(ArgEnum, Debug)] -pub enum Shell { +pub enum InitShell { Bash, Elvish, Fish, @@ -87,13 +94,6 @@ pub enum Shell { Zsh, } -#[derive(ArgEnum, Clone, Copy, Debug, Eq, PartialEq)] -pub enum Hook { - None, - Prompt, - Pwd, -} - /// Search for a directory in the database #[derive(Clap, Debug)] pub struct Query { diff --git a/src/cmd/import.rs b/src/cmd/import.rs index 53ae0e8..474d7d9 100644 --- a/src/cmd/import.rs +++ b/src/cmd/import.rs @@ -1,5 +1,5 @@ use super::Run; -use crate::app::{From, Import}; +use crate::app::{Import, ImportFrom}; use crate::config; use crate::import::{Autojump, Import as _, Z}; use crate::util; @@ -19,12 +19,12 @@ impl Run for Import { let resolve_symlinks = config::zo_resolve_symlinks(); match self.from { - From::Autojump => Autojump { + ImportFrom::Autojump => Autojump { resolve_symlinks, now: util::current_time()?, } .import(&mut db, &self.path), - From::Z => Z { resolve_symlinks }.import(&mut db, &self.path), + ImportFrom::Z => Z { resolve_symlinks }.import(&mut db, &self.path), } } } diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 1bd39e9..1d9718e 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -1,5 +1,5 @@ use super::Run; -use crate::app::{Init, Shell}; +use crate::app::{Init, InitShell}; use crate::config; use crate::error::WriteErrorHandler; use crate::shell::{self, Opts}; @@ -28,14 +28,14 @@ impl Run for Init { }; let source = match self.shell { - Shell::Bash => shell::Bash(opts).render(), - Shell::Elvish => shell::Elvish(opts).render(), - Shell::Fish => shell::Fish(opts).render(), - Shell::Nushell => shell::Nushell(opts).render(), - Shell::Posix => shell::Posix(opts).render(), - Shell::Powershell => shell::Powershell(opts).render(), - Shell::Xonsh => shell::Xonsh(opts).render(), - Shell::Zsh => shell::Zsh(opts).render(), + InitShell::Bash => shell::Bash(opts).render(), + InitShell::Elvish => shell::Elvish(opts).render(), + InitShell::Fish => shell::Fish(opts).render(), + InitShell::Nushell => shell::Nushell(opts).render(), + InitShell::Posix => shell::Posix(opts).render(), + InitShell::Powershell => shell::Powershell(opts).render(), + InitShell::Xonsh => shell::Xonsh(opts).render(), + InitShell::Zsh => shell::Zsh(opts).render(), } .context("could not render template")?; writeln!(io::stdout(), "{}", source).wrap_write("stdout") diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index c52b21d..f41c576 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -4,7 +4,7 @@ mod init; mod query; mod remove; -use crate::app::Cli; +use crate::app::App; use anyhow::Result; @@ -12,14 +12,14 @@ pub trait Run { fn run(&self) -> Result<()>; } -impl Run for Cli { +impl Run for App { fn run(&self) -> Result<()> { match self { - Cli::Add(cmd) => cmd.run(), - Cli::Import(cmd) => cmd.run(), - Cli::Init(cmd) => cmd.run(), - Cli::Query(cmd) => cmd.run(), - Cli::Remove(cmd) => cmd.run(), + App::Add(cmd) => cmd.run(), + App::Import(cmd) => cmd.run(), + App::Init(cmd) => cmd.run(), + App::Query(cmd) => cmd.run(), + App::Remove(cmd) => cmd.run(), } } } diff --git a/src/main.rs b/src/main.rs index 967b33b..4a9f0c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ mod import; mod shell; mod util; -use crate::app::Cli; +use crate::app::App; use crate::cmd::Run; use crate::error::SilentExit; @@ -23,7 +23,7 @@ pub fn main() { env::remove_var("RUST_LIB_BACKTRACE"); env::remove_var("RUST_BACKTRACE"); - if let Err(e) = Cli::parse().run() { + if let Err(e) = App::parse().run() { match e.downcast::() { Ok(SilentExit { code }) => process::exit(code), Err(e) => { diff --git a/src/shell.rs b/src/shell.rs index 2dbfd47..eb83c8f 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1,9 +1,9 @@ -use crate::app::Hook; +use crate::app::InitHook; #[derive(Debug, Eq, PartialEq)] pub struct Opts<'a> { pub cmd: Option<&'a str>, - pub(crate) hook: Hook, + pub hook: InitHook, pub echo: bool, pub resolve_symlinks: bool, } @@ -51,7 +51,7 @@ mod tests { fn opts() -> &'static [Opts<'static>] { static OPTS: OnceCell> = OnceCell::new(); const BOOLS: &[bool] = &[false, true]; - const HOOKS: &[Hook] = &[Hook::None, Hook::Prompt, Hook::Pwd]; + const HOOKS: &[InitHook] = &[InitHook::None, InitHook::Prompt, InitHook::Pwd]; const CMDS: &[Option<&str>] = &[None, Some("z")]; OPTS.get_or_init(|| { @@ -203,7 +203,7 @@ mod tests { .success() .stderr(""); - if opts.hook != Hook::Pwd { + if opts.hook != InitHook::Pwd { assert.stdout(""); } } @@ -218,7 +218,7 @@ mod tests { .success() .stderr(""); - if opts.hook != Hook::Pwd { + if opts.hook != InitHook::Pwd { assert.stdout(""); } } @@ -233,7 +233,7 @@ mod tests { .success() .stderr(""); - if opts.hook != Hook::Pwd { + if opts.hook != InitHook::Pwd { assert.stdout(""); } } diff --git a/templates/bash.txt b/templates/bash.txt index 0c8e675..e0b1cef 100644 --- a/templates/bash.txt +++ b/templates/bash.txt @@ -30,17 +30,17 @@ function __zoxide_cd() { # Hook to add new entries to the database. {%- match hook %} -{%- when Hook::None %} +{%- when InitHook::None %} {{ not_configured }} -{%- when Hook::Prompt %} +{%- when InitHook::Prompt %} function __zoxide_hook() { \builtin local -r __zoxide_retval="$?" zoxide add -- "$(__zoxide_pwd)" return "${__zoxide_retval}" } -{%- when Hook::Pwd %} +{%- when InitHook::Pwd %} function __zoxide_hook() { \builtin local -r __zoxide_retval="$?" \builtin local -r __zoxide_pwd_tmp="$(__zoxide_pwd)" @@ -62,7 +62,7 @@ function __zoxide_hook() { # Initialize hook. if [ "${__zoxide_hooked}" != '1' ]; then __zoxide_hooked='1' -{%- if hook == Hook::None %} +{%- if hook == InitHook::None %} {{ not_configured }} {%- else %} PROMPT_COMMAND="__zoxide_hook;${PROMPT_COMMAND:+${PROMPT_COMMAND}}" diff --git a/templates/elvish.txt b/templates/elvish.txt index 7db8174..4a253ee 100644 --- a/templates/elvish.txt +++ b/templates/elvish.txt @@ -29,11 +29,11 @@ if (not (and (builtin:has-env __zoxide_hooked) (builtin:eq (builtin:get-env __zo # Initialize hook to add directories to zoxide. {%- match hook %} -{%- when Hook::None %} +{%- when InitHook::None %} {{ not_configured }} -{%- when Hook::Prompt %} +{%- when InitHook::Prompt %} edit:before-readline = [$@edit:before-readline []{ zoxide add -- $pwd }] -{%- when Hook::Pwd %} +{%- when InitHook::Pwd %} after-chdir = [$@after-chdir [_]{ zoxide add -- $pwd }] {%- endmatch %} } diff --git a/templates/fish.txt b/templates/fish.txt index b71b4ff..4a0ed74 100644 --- a/templates/fish.txt +++ b/templates/fish.txt @@ -34,11 +34,11 @@ end if test "$__zoxide_hooked" != 1 set __zoxide_hooked 1 {%- match hook %} -{%- when Hook::None %} +{%- when InitHook::None %} function __zoxide_hook -{%- when Hook::Prompt %} +{%- when InitHook::Prompt %} function __zoxide_hook --on-event fish_prompt -{%- when Hook::Pwd %} +{%- when InitHook::Pwd %} function __zoxide_hook --on-variable PWD {%- endmatch %} command zoxide add -- (__zoxide_pwd) diff --git a/templates/nushell.txt b/templates/nushell.txt index f2806d0..d34993c 100644 --- a/templates/nushell.txt +++ b/templates/nushell.txt @@ -17,15 +17,15 @@ def __zoxide_prompt [] { # Hook to add new entries to the database. {%- match hook %} -{%- when Hook::None %} +{%- when InitHook::None %} def __zoxide_hook [] {} -{%- when Hook::Prompt %} +{%- when InitHook::Prompt %} def __zoxide_hook [] { shells | where active == $true && name == filesystem | get path | each { zoxide add -- $it } } -{%- when Hook::Pwd %} +{%- when InitHook::Pwd %} def __zoxide_hook [] {} printf "zoxide: PWD hooks are not supported on Nushell.\n Use 'zoxide init nushell --hook prompt' instead.\n" diff --git a/templates/posix.txt b/templates/posix.txt index 9088b6b..05a776a 100644 --- a/templates/posix.txt +++ b/templates/posix.txt @@ -26,15 +26,15 @@ __zoxide_cd() { # Hook to add new entries to the database. {%- match hook %} -{%- when Hook::None %} +{%- when InitHook::None %} {{ not_configured }} -{%- when Hook::Prompt %} +{%- when InitHook::Prompt %} __zoxide_hook() { zoxide add -- "$(__zoxide_pwd)" } -{%- when Hook::Pwd %} +{%- when InitHook::Pwd %} {{ not_configured }} {%- endmatch %} @@ -43,11 +43,11 @@ __zoxide_hook() { if [ "${__zoxide_hooked}" != '1' ]; then __zoxide_hooked='1' {%- match hook %} -{%- when Hook::None %} +{%- when InitHook::None %} {{ not_configured }} -{%- when Hook::Prompt %} +{%- when InitHook::Prompt %} PS1="${PS1}\$(__zoxide_hook)" -{%- when Hook::Pwd %} +{%- when InitHook::Pwd %} \printf "%s\n%s\n" \ "zoxide: PWD hooks are not supported on POSIX shells." \ " Use 'zoxide init posix --hook prompt' instead." diff --git a/templates/powershell.txt b/templates/powershell.txt index 6379587..b7610af 100644 --- a/templates/powershell.txt +++ b/templates/powershell.txt @@ -31,15 +31,15 @@ function __zoxide_hook { if ($__zoxide_hooked -ne '1') { $__zoxide_hooked = '1' {%- match hook %} -{%- when Hook::None %} +{%- when InitHook::None %} {{ not_configured }} -{%- when Hook::Prompt %} +{%- when InitHook::Prompt %} $__zoxide_prompt_old = $function:prompt function prompt { $null = __zoxide_hook & $__zoxide_prompt_old } -{%- when Hook::Pwd %} +{%- when InitHook::Pwd %} if ($PSVersionTable.PSVersion.Major -ge 6) { $ExecutionContext.InvokeCommand.LocationChangedAction = { $null = __zoxide_hook diff --git a/templates/xonsh.txt b/templates/xonsh.txt index 07f4d2d..0fb3148 100644 --- a/templates/xonsh.txt +++ b/templates/xonsh.txt @@ -10,7 +10,7 @@ import sys {%- if cmd.is_some() %} from builtins import aliases # type: ignore # pylint: disable=no-name-in-module {%- endif %} -{%- if hook != Hook::None %} +{%- if hook != InitHook::None %} from builtins import events # type: ignore # pylint: disable=no-name-in-module {%- endif %} from subprocess import CalledProcessError @@ -88,11 +88,11 @@ def __zoxide_errhandler(func): if globals().get("__zoxide_hooked") is not True: globals()["__zoxide_hooked"] = True {% match hook -%} -{%- when Hook::None %} +{%- when InitHook::None %} {{ not_configured }} -{%- when Hook::Prompt %} +{%- when InitHook::Prompt %} @events.on_post_prompt # type: ignore # pylint:disable=undefined-variable -{%- when Hook::Pwd %} +{%- when InitHook::Pwd %} @events.on_chdir # type: ignore # pylint:disable=undefined-variable {%- endmatch %} def __zoxide_hook(**_kwargs): diff --git a/templates/zsh.txt b/templates/zsh.txt index b95f733..96f0ef0 100644 --- a/templates/zsh.txt +++ b/templates/zsh.txt @@ -33,11 +33,11 @@ function __zoxide_hook() { if [ "${__zoxide_hooked}" != '1' ]; then __zoxide_hooked='1' {%- match hook %} -{%- when Hook::None %} +{%- when InitHook::None %} {{ not_configured }} -{%- when Hook::Prompt %} +{%- when InitHook::Prompt %} precmd_functions+=(__zoxide_hook) -{%- when Hook::Pwd %} +{%- when InitHook::Pwd %} chpwd_functions=("${chpwd_functions[@]}" "__zoxide_hook") {%- endmatch %} fi