Add PWD hook for bash + fish

This commit is contained in:
Ajeet D'Souza 2020-03-13 07:09:21 +05:30
parent 9c8e8da71a
commit 41aabccb8f
2 changed files with 29 additions and 13 deletions

View File

@ -23,7 +23,7 @@ pub fn main() -> Result<()> {
let opt = Zoxide::from_args(); let opt = Zoxide::from_args();
match opt { match opt {
Zoxide::Add(add) => add.run()?, Zoxide::Add(add) => add.run()?,
Zoxide::Init(init) => init.run()?, Zoxide::Init(init) => init.run(),
Zoxide::Migrate(migrate) => migrate.run()?, Zoxide::Migrate(migrate) => migrate.run()?,
Zoxide::Query(query) => query.run()?, Zoxide::Query(query) => query.run()?,
Zoxide::Remove(remove) => remove.run()?, Zoxide::Remove(remove) => remove.run()?,

View File

@ -1,4 +1,3 @@
use anyhow::{bail, Result};
use clap::arg_enum; use clap::arg_enum;
use std::io::{self, Write}; use std::io::{self, Write};
use structopt::StructOpt; use structopt::StructOpt;
@ -26,7 +25,7 @@ pub struct Init {
} }
impl Init { impl Init {
pub fn run(&self) -> Result<()> { pub fn run(&self) {
let config = match self.shell { let config = match self.shell {
Shell::bash => BASH_CONFIG, Shell::bash => BASH_CONFIG,
Shell::fish => FISH_CONFIG, Shell::fish => FISH_CONFIG,
@ -44,13 +43,8 @@ impl Init {
match self.hook { match self.hook {
Hook::none => (), Hook::none => (),
Hook::prompt => writeln!(handle, "{}", config.hook.prompt).unwrap(), Hook::prompt => writeln!(handle, "{}", config.hook.prompt).unwrap(),
Hook::pwd => match config.hook.pwd { Hook::pwd => writeln!(handle, "{}", config.hook.pwd).unwrap(),
Some(pwd) => writeln!(handle, "{}", pwd).unwrap(),
None => bail!("pwd hooks are currently not supported for this shell"),
},
}; };
Ok(())
} }
} }
@ -79,7 +73,7 @@ const BASH_CONFIG: ShellConfig = ShellConfig {
alias: BASH_ALIAS, alias: BASH_ALIAS,
hook: HookConfig { hook: HookConfig {
prompt: BASH_HOOK_PROMPT, prompt: BASH_HOOK_PROMPT,
pwd: None, pwd: BASH_HOOK_PWD,
}, },
}; };
@ -88,7 +82,7 @@ const FISH_CONFIG: ShellConfig = ShellConfig {
alias: FISH_ALIAS, alias: FISH_ALIAS,
hook: HookConfig { hook: HookConfig {
prompt: FISH_HOOK_PROMPT, prompt: FISH_HOOK_PROMPT,
pwd: None, pwd: FISH_HOOK_PWD,
}, },
}; };
@ -97,7 +91,7 @@ const ZSH_CONFIG: ShellConfig = ShellConfig {
alias: ZSH_ALIAS, alias: ZSH_ALIAS,
hook: HookConfig { hook: HookConfig {
prompt: ZSH_HOOK_PROMPT, prompt: ZSH_HOOK_PROMPT,
pwd: Some(ZSH_HOOK_PWD), pwd: ZSH_HOOK_PWD,
}, },
}; };
@ -109,7 +103,7 @@ struct ShellConfig {
struct HookConfig { struct HookConfig {
prompt: &'static str, prompt: &'static str,
pwd: Option<&'static str>, pwd: &'static str,
} }
const BASH_Z: &str = r#" const BASH_Z: &str = r#"
@ -200,6 +194,28 @@ _zoxide_hook() {
} }
"#; "#;
const BASH_HOOK_PWD: &str = r#"
_zoxide_hook() {
if [ -z "${_ZO_PWD}" ]; then
_ZO_PWD="${PWD}"
elif [ "${_ZO_PWD}" != "${PWD}" ]; then
_ZO_PWD="${PWD}"
zoxide add
fi
}
case "$PROMPT_COMMAND" in
*_zoxide_hook*) ;;
*) PROMPT_COMMAND="_zoxide_hook${PROMPT_COMMAND:+;${PROMPT_COMMAND}}" ;;
esac
"#;
const FISH_HOOK_PWD: &str = r#"
function _zoxide_hook --on-variable PWD
zoxide add
end
"#;
const ZSH_HOOK_PWD: &str = r#" const ZSH_HOOK_PWD: &str = r#"
_zoxide_hook() { _zoxide_hook() {
zoxide add zoxide add