Skip to content

Commit

Permalink
feat: adding zsh installation on zsh-completions data_dir xdg (linux …
Browse files Browse the repository at this point in the history
…only)
  • Loading branch information
freexploit committed Jan 26, 2024
1 parent b50e4ab commit 17b436d
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 33 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/aiken/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ clap_complete = "4.3.2"
inquire = "0.6.2"
num-bigint = "0.4.3"
ordinal = "0.3.2"
xdg = "2.5.2"
33 changes: 0 additions & 33 deletions crates/aiken/src/cmd/completion.rs

This file was deleted.

92 changes: 92 additions & 0 deletions crates/aiken/src/cmd/completion/completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

use std::{fs::File, io::Write};

use clap::{Command, Subcommand};
use clap_complete::{generate, Shell};
use crate::cmd::Cmd as MainCmd;
use std::fs::OpenOptions;

/// Generates shell completion scripts
#[derive(clap::Args)]
pub struct Args {
#[arg(short, long, default_value_t = false)]
install: bool,
}

fn generate_wrapper(shell: Shell, buf: &mut dyn Write) {
let cli = Command::new("aiken").disable_version_flag(true);
let mut main = MainCmd::augment_subcommands(cli);
generate(
shell,
&mut main,
"aiken".to_string(),
buf,
);
}

fn zsh() -> miette::Result<()> {


//if oh-my-zsh
//if zsh-completions in data_dir

let xdg_dirs = xdg::BaseDirectories::with_prefix("zsh-completions").unwrap();
let data_home = xdg_dirs.get_data_home();
let home = std::env::var("HOME").expect("Cannot find your home directory");
let mut completion_file : File;

if data_home.exists() {
let completion_path = xdg_dirs.place_data_file("_aiken").expect("cannot create directory");
completion_file = File::create(completion_path).expect("cannot open file");

} else {

let completion_path = xdg_dirs.place_data_file("_aiken").expect("cannot create directory");
completion_file = File::create(completion_path).expect("cannot open file");

let mut zshrc = OpenOptions::new().write(true).append(true).open(format!("{}/.zshrc",home)).expect(".zshrc file not found");
if let Some(home) = data_home.to_str() {
let fpath: String = format!(r#"fpath=($fpath "{}")"#, home);
if let Err(e) = writeln!(zshrc,"{}", fpath ) {
eprintln!("Couldn't write to file: {}", e);
}

}
}

generate_wrapper(Shell::Zsh, &mut completion_file);

Ok(())
}

fn completions_to_file(shell: Shell) -> miette::Result<()> {

match shell {
Shell::Bash => {
todo!()
},
Shell::Fish => {
todo!()
},
Shell::Zsh => {
zsh()?;
},
_ => eprintln!("Shell not supported"),
}

Ok(())
}

pub fn exec(cmd_args: Args, shell: Shell) -> miette::Result<()>{
if cmd_args.install {
completions_to_file(shell)?;
}else {
generate_wrapper(
shell,
&mut std::io::stdout(),
);
}
Ok(())
}

22 changes: 22 additions & 0 deletions crates/aiken/src/cmd/completion/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod completion;

use clap::Subcommand;
use clap_complete::Shell;

/// Commands for working with transactions
#[derive(Subcommand)]
pub enum Cmd {
Bash(completion::Args),
Zsh(completion::Args),
Fish(completion::Args),
}

pub fn exec(cmd: Cmd) -> miette::Result<()> {
match cmd {
Cmd::Bash(args) => completion::exec(args,Shell::Bash),
Cmd::Zsh(args) => completion::exec(args,Shell::Zsh),
Cmd::Fish(args) => completion::exec(args,Shell::Fish),
}
}


0 comments on commit 17b436d

Please sign in to comment.