fix: clippy and add docs to the new flag

This commit is contained in:
rvcas 2024-05-10 10:51:29 -04:00
parent 8a6957dfd0
commit c86978b5ac
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
2 changed files with 42 additions and 9 deletions

View File

@ -1,4 +1,4 @@
pub mod completion; pub mod shell;
use clap::Subcommand; use clap::Subcommand;
use clap_complete::Shell; use clap_complete::Shell;
@ -6,15 +6,15 @@ use clap_complete::Shell;
/// Commands for working with transactions /// Commands for working with transactions
#[derive(Subcommand)] #[derive(Subcommand)]
pub enum Cmd { pub enum Cmd {
Bash(completion::Args), Bash(shell::Args),
Zsh(completion::Args), Zsh(shell::Args),
Fish(completion::Args), Fish(shell::Args),
} }
pub fn exec(cmd: Cmd) -> miette::Result<()> { pub fn exec(cmd: Cmd) -> miette::Result<()> {
match cmd { match cmd {
Cmd::Bash(args) => completion::exec(args, Shell::Bash), Cmd::Bash(args) => shell::exec(args, Shell::Bash),
Cmd::Zsh(args) => completion::exec(args, Shell::Zsh), Cmd::Zsh(args) => shell::exec(args, Shell::Zsh),
Cmd::Fish(args) => completion::exec(args, Shell::Fish), Cmd::Fish(args) => shell::exec(args, Shell::Fish),
} }
} }

View File

@ -6,42 +6,52 @@ use clap_complete::{generate, Shell};
use std::fs::OpenOptions; use std::fs::OpenOptions;
/// Generates shell completion scripts /// Generates shell completion scripts
#[derive(clap::Args)] #[derive(clap::Args)]
pub struct Args { pub struct Args {
/// Install the completion scripts
#[arg(short, long, default_value_t = false)] #[arg(short, long, default_value_t = false)]
install: bool, install: bool,
} }
fn generate_wrapper(shell: Shell, buf: &mut dyn Write) { fn generate_wrapper(shell: Shell, buf: &mut dyn Write) {
let cli = Command::new("aiken").disable_version_flag(true); let cli = Command::new("aiken").disable_version_flag(true);
let mut main = MainCmd::augment_subcommands(cli); let mut main = MainCmd::augment_subcommands(cli);
generate(shell, &mut main, "aiken".to_string(), buf); generate(shell, &mut main, "aiken".to_string(), buf);
} }
fn zsh() -> miette::Result<()> { fn zsh() -> miette::Result<()> {
//if oh-my-zsh //if oh-my-zsh
let prefix_dir = "zsh-completions/site-functions"; let prefix_dir = "zsh-completions/site-functions";
let home = std::env::var("HOME").expect("Environment variable 'HOME' not set but needed."); let home = std::env::var("HOME").expect("Environment variable 'HOME' not set but needed.");
let xdg_dirs = xdg::BaseDirectories::with_prefix(prefix_dir) let xdg_dirs = xdg::BaseDirectories::with_prefix(prefix_dir)
.expect("Could not find completion directory {prefix_dir} in xdg directories."); .expect("Could not find completion directory {prefix_dir} in xdg directories.");
let data_home = xdg_dirs.get_data_home(); let data_home = xdg_dirs.get_data_home();
let mut completion_file: File; let mut completion_file: File;
let oh_my_zsh_path = Path::new(&home).join(".oh-my-zsh"); let oh_my_zsh_path = Path::new(&home).join(".oh-my-zsh");
if oh_my_zsh_path.exists() { if oh_my_zsh_path.exists() {
let completions_path = oh_my_zsh_path.join("completions"); let completions_path = oh_my_zsh_path.join("completions");
let aiken_completion_path = completions_path.join("_aiken"); let aiken_completion_path = completions_path.join("_aiken");
if !completions_path.exists() { if !completions_path.exists() {
std::fs::create_dir(completions_path.as_path()).expect( std::fs::create_dir(completions_path.as_path()).expect(
"Cannot create directory: {completions_path.into_os_string().into_string()}", "Cannot create directory: {completions_path.into_os_string().into_string()}",
); );
} }
completion_file = File::create(aiken_completion_path) completion_file = File::create(aiken_completion_path)
.expect("Cannot open file at: {aiken_completion_path.into_os_string().into_string()}"); .expect("Cannot open file at: {aiken_completion_path.into_os_string().into_string()}");
generate_wrapper(Shell::Zsh, &mut completion_file); generate_wrapper(Shell::Zsh, &mut completion_file);
return Ok(()); return Ok(());
} }
@ -49,15 +59,19 @@ fn zsh() -> miette::Result<()> {
let completion_path = xdg_dirs let completion_path = xdg_dirs
.place_data_file("_aiken") .place_data_file("_aiken")
.expect("cannot create directory"); .expect("cannot create directory");
completion_file = File::create(completion_path) completion_file = File::create(completion_path)
.expect("Cannot open file at: {completion_path.into_os_string().into_string()}"); .expect("Cannot open file at: {completion_path.into_os_string().into_string()}");
generate_wrapper(Shell::Zsh, &mut completion_file); generate_wrapper(Shell::Zsh, &mut completion_file);
return Ok(()); return Ok(());
} }
let completion_path = xdg_dirs let completion_path = xdg_dirs
.place_data_file("_aiken") .place_data_file("_aiken")
.expect("cannot create directory"); .expect("cannot create directory");
completion_file = File::create(completion_path) completion_file = File::create(completion_path)
.expect("Cannot open file at: {completion_path.into_os_string().into_string()}"); .expect("Cannot open file at: {completion_path.into_os_string().into_string()}");
@ -66,38 +80,51 @@ fn zsh() -> miette::Result<()> {
.append(true) .append(true)
.open(format!("{}/.zshrc", home)) .open(format!("{}/.zshrc", home))
.expect(".zshrc file not found"); .expect(".zshrc file not found");
if let Some(home) = data_home.to_str() { if let Some(home) = data_home.to_str() {
let fpath: String = format!(r#"fpath=($fpath "{}")"#, home); let fpath: String = format!(r#"fpath=($fpath "{}")"#, home);
if let Err(e) = writeln!(zshrc, "{}", fpath) { if let Err(e) = writeln!(zshrc, "{}", fpath) {
eprintln!("Couldn't write to file: {}", e); eprintln!("Couldn't write to file: {}", e);
} }
} }
generate_wrapper(Shell::Zsh, &mut completion_file); generate_wrapper(Shell::Zsh, &mut completion_file);
Ok(()) Ok(())
} }
fn fish() -> miette::Result<()> { fn fish() -> miette::Result<()> {
// NOTE: Installing completion on ~/.confi/fish/completions // NOTE: Installing completion on ~/.confi/fish/completions
let prefix_dir = "fish/completions"; let prefix_dir = "fish/completions";
let xdg_dirs = xdg::BaseDirectories::with_prefix(prefix_dir) let xdg_dirs = xdg::BaseDirectories::with_prefix(prefix_dir)
.expect("Could not find completion directory {prefix_dir} in xdg directories."); .expect("Could not find completion directory {prefix_dir} in xdg directories.");
let completion_path = xdg_dirs let completion_path = xdg_dirs
.place_config_file("aiken.fish") .place_config_file("aiken.fish")
.expect("Cannot create path"); .expect("Cannot create path");
let mut completion_file = File::create(completion_path) let mut completion_file = File::create(completion_path)
.expect("Cannot open file at: {completion_path.into_os_string().into_string()}"); .expect("Cannot open file at: {completion_path.into_os_string().into_string()}");
generate_wrapper(Shell::Fish, &mut completion_file); generate_wrapper(Shell::Fish, &mut completion_file);
Ok(()) Ok(())
} }
fn bash() -> miette::Result<()> { fn bash() -> miette::Result<()> {
let prefix_dir = "bash-completion/completions"; let prefix_dir = "bash-completion/completions";
let aiken_bash = "aiken.completion.bash"; let aiken_bash = "aiken.completion.bash";
let xdg_dirs = xdg::BaseDirectories::with_prefix(prefix_dir) let xdg_dirs = xdg::BaseDirectories::with_prefix(prefix_dir)
.expect("Could not find completion directory {prefix_dir} in xdg directories."); .expect("Could not find completion directory {prefix_dir} in xdg directories.");
let home = std::env::var("HOME").expect("Environment variable 'HOME' not set but needed."); let home = std::env::var("HOME").expect("Environment variable 'HOME' not set but needed.");
let config_home = xdg_dirs.get_config_home(); let config_home = xdg_dirs.get_config_home();
let completion_path = xdg_dirs let completion_path = xdg_dirs
.place_config_file(aiken_bash) .place_config_file(aiken_bash)
.expect("Cannot create completion file {aiken_bash} under xdg directories"); .expect("Cannot create completion file {aiken_bash} under xdg directories");
@ -107,15 +134,20 @@ fn bash() -> miette::Result<()> {
.append(true) .append(true)
.open(format!("{}/.bashrc", home)) .open(format!("{}/.bashrc", home))
.expect(".bashrc file not found in {home} directory"); .expect(".bashrc file not found in {home} directory");
if let Some(config) = config_home.to_str() { if let Some(config) = config_home.to_str() {
let path: String = format!("source {config}"); let path: String = format!("source {config}");
if let Err(e) = writeln!(bashrc, "{}", path) { if let Err(e) = writeln!(bashrc, "{}", path) {
eprintln!("Couldn't write to file: {}", e); eprintln!("Couldn't write to file: {}", e);
} }
} }
let mut completion_file = File::create(completion_path) let mut completion_file = File::create(completion_path)
.expect("Cannot open file at: {completion_path.into_os_string().into_string()}"); .expect("Cannot open file at: {completion_path.into_os_string().into_string()}");
generate_wrapper(Shell::Bash, &mut completion_file); generate_wrapper(Shell::Bash, &mut completion_file);
Ok(()) Ok(())
} }
@ -130,7 +162,7 @@ fn completions_to_file(shell: Shell) -> miette::Result<()> {
Shell::Zsh => { Shell::Zsh => {
zsh()?; zsh()?;
} }
_ => eprintln!("Shell not supported"), s => eprintln!("{s} not supported"),
} }
Ok(()) Ok(())
@ -142,5 +174,6 @@ pub fn exec(cmd_args: Args, shell: Shell) -> miette::Result<()> {
} else { } else {
generate_wrapper(shell, &mut std::io::stdout()); generate_wrapper(shell, &mut std::io::stdout());
} }
Ok(()) Ok(())
} }