use std::fs::File;
use crate::errors::NoWay;
use super::prelude::*;
use anyhow::Context;
use clap::{CommandFactory, ValueEnum};
#[async_trait]
impl CliSubcommand for Completions {
async fn run(self: Box<Self>) -> anyhow::Result<()> {
let shell_name = self
.shell
.to_possible_value()
.no_way_because("already deserialized by clap")
.get_name()
.to_string();
if shell_name != "zsh" || self.never_write {
self.shell
.generate(&mut Cli::command(), &mut std::io::stdout());
return Ok(());
}
let completions_path = {
let mut home = homedir::my_home()
.ok()
.and_then(|option| option)
.context("No home directory detected")?;
let directory = format!(".{shell_name}/completion");
home.push(directory);
home
};
if let Err(error) = cmd!("mkdir -p {}", &completions_path.to_string_lossy()).run() {
anyhow::bail!(
"Error creating completion directory `{}`\n> {:?}",
completions_path.to_string_lossy(),
error
);
};
let file_path = completions_path.join("_sbp");
let mut file = File::create(&file_path).context("Error creating file")?;
self.shell.generate(&mut Cli::command(), &mut file);
file.sync_all().context("Error closing completions file")?;
println!(
r#"Completion file correctly written to `{}`.
In order to use it, you will also need to add:
fpath=($fpath ~/.zsh/completion)
to your ~/.zshrc file if you haven't done that so far.
Remember to source .zshrc or restart your shell."#,
file_path.to_string_lossy()
);
Ok(())
}
}