add support for zsh completions

This commit is contained in:
Daylin Morgan 2024-03-27 11:54:33 -05:00
parent 933f7e1ae0
commit 779c1cc710
Signed by: daylin
GPG key ID: 950D13E9719334AD
4 changed files with 54 additions and 11 deletions

View file

@ -130,6 +130,15 @@ dependencies = [
"strsim", "strsim",
] ]
[[package]]
name = "clap_complete"
version = "4.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "885e4d7d5af40bfb99ae6f9433e292feac98d452dcb3ec3d25dfe7552b77da8c"
dependencies = [
"clap",
]
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "4.5.4" version = "4.5.4"
@ -372,6 +381,7 @@ name = "oizys"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"clap_complete",
"homedir", "homedir",
"hostname", "hostname",
] ]

View file

@ -7,5 +7,6 @@ edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.5.4", features = ["derive"] } clap = { version = "4.5.4", features = ["derive"] }
clap_complete = "4.5.1"
homedir = "0.2.1" homedir = "0.2.1"
hostname = "0.3.1" hostname = "0.3.1"

View file

@ -1,4 +1,7 @@
{rustPlatform}: {
installShellFiles,
rustPlatform,
}:
rustPlatform.buildRustPackage { rustPlatform.buildRustPackage {
pname = "oizys"; pname = "oizys";
version = "unstable"; version = "unstable";
@ -6,4 +9,11 @@ rustPlatform.buildRustPackage {
cargoLock = { cargoLock = {
lockFile = ./Cargo.lock; lockFile = ./Cargo.lock;
}; };
nativeBuildInputs = [installShellFiles];
postInstall = ''
installShellCompletion --cmd oizys \
--zsh <($out/bin/oizys --completions zsh)
'';
} }

View file

@ -1,5 +1,6 @@
use clap::{Parser, Subcommand}; use clap::{CommandFactory, Parser, Subcommand};
use std::{env, path::PathBuf, process::Command}; use clap_complete::{generate, Generator, Shell};
use std::{env, io, path::PathBuf, process::Command};
#[derive(Parser)] #[derive(Parser)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@ -20,8 +21,12 @@ struct Cli {
#[arg(long, global=true, action = clap::ArgAction::SetTrue)] #[arg(long, global=true, action = clap::ArgAction::SetTrue)]
no_pinix: bool, no_pinix: bool,
/// generate shell completion
#[arg(long, value_enum)]
completions: Option<Shell>,
#[command(subcommand)] #[command(subcommand)]
command: Commands, command: Option<Commands>,
} }
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
@ -49,6 +54,10 @@ enum Commands {
Path {}, Path {},
} }
fn print_completions<G: Generator>(gen: G, cmd: &mut clap::Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}
#[derive(Debug)] #[derive(Debug)]
struct Oizys { struct Oizys {
host: String, host: String,
@ -148,6 +157,13 @@ fn main() {
let cli = Cli::parse(); let cli = Cli::parse();
let oizys = Oizys::new(cli.host, cli.flake, cli.no_pinix, cli.verbose); let oizys = Oizys::new(cli.host, cli.flake, cli.no_pinix, cli.verbose);
if let Some(completions) = cli.completions {
let mut cmd = Cli::command();
eprintln!("Generating completion for {completions:?}");
print_completions(completions, &mut cmd);
std::process::exit(0);
}
if oizys.verbose > 2 { if oizys.verbose > 2 {
println!("-vv is max verbosity") println!("-vv is max verbosity")
} }
@ -155,7 +171,8 @@ fn main() {
println!("{:?}", oizys) println!("{:?}", oizys)
} }
match &cli.command { if let Some(command) = &cli.command {
match command {
Commands::Dry {} => oizys.build(true), Commands::Dry {} => oizys.build(true),
Commands::Build {} => oizys.build(false), Commands::Build {} => oizys.build(false),
Commands::Path {} => println!("{}", oizys.output()), Commands::Path {} => println!("{}", oizys.output()),
@ -163,4 +180,9 @@ fn main() {
Commands::Switch {} => oizys.nixos_rebuild("switch"), Commands::Switch {} => oizys.nixos_rebuild("switch"),
Commands::Cache { name } => oizys.cache(name), Commands::Cache { name } => oizys.cache(name),
} }
} else {
eprintln!("No subcommand provided..");
let mut cmd = Cli::command();
cmd.print_help().unwrap();
}
} }