use crate::{Result, env};
#[derive(Clone, Copy, Debug, usage_rs::ValueEnum)]
enum Shell {
Bash,
Zsh,
Fish,
}
#[derive(Debug, usage_rs::Args)]
#[usage(
verbatim_doc_comment,
long_about = "\
Activate pitchfork in your shell session
Generates shell code that enables automatic daemon management when changing
directories. Required for auto-start/stop features in pitchfork.toml.
Supported shells: bash, zsh, fish
Add to your shell config:
bash (~/.bashrc):
eval \"$(pitchfork activate bash)\"
zsh (~/.zshrc):
eval \"$(pitchfork activate zsh)\"
fish (~/.config/fish/config.fish):
pitchfork activate fish | source"
)]
pub struct Activate {
#[usage(value_enum)]
shell: Shell,
}
impl Activate {
pub async fn run(&self) -> Result<()> {
let pitchfork = env::PITCHFORK_BIN.to_string_lossy().to_string();
let s = match self.shell {
Shell::Bash => format!(
r#"
__pitchfork() {{
{pitchfork} cd --shell-pid $$
}}
{}
{}
chpwd_functions+=(__pitchfork)
__pitchfork
"#,
include_str!("../../assets/bash_zsh_support/chpwd/function.sh"),
include_str!("../../assets/bash_zsh_support/chpwd/load.sh")
),
Shell::Zsh => format!(
r#"
__pitchfork() {{
{pitchfork} cd --shell-pid $$
}}
chpwd_functions+=(__pitchfork)
__pitchfork
"#
),
Shell::Fish => format!(
r#"
function __pitchfork --on-variable PWD
{pitchfork} cd --shell-pid "$fish_pid"
end
__pitchfork
"#,
),
};
println!("{}", s.trim());
Ok(())
}
}