From 5ca0b20e1a7308f6783ec47ea4204e201a75f533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Nerma?= Date: Fri, 20 Sep 2024 19:53:56 +0200 Subject: [PATCH] Add support for shell completions --- Cargo.lock | 28 +++++++++++++++++++--------- Cargo.toml | 3 ++- src/cmd.rs | 30 ++++++++++++++++++++++++------ src/main.rs | 29 +++++++++++++++++++++++------ 4 files changed, 68 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 80b3803..51ca10e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,9 +19,9 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" [[package]] name = "anstyle-parse" @@ -65,9 +65,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.5.4" +version = "4.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac" dependencies = [ "clap_builder", "clap_derive", @@ -75,9 +75,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.2" +version = "4.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73" dependencies = [ "anstream", "anstyle", @@ -85,11 +85,20 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b378c786d3bde9442d2c6dd7e6080b2a818db2b96e30d6e7f1b6d224eb617d3" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" -version = "4.5.4" +version = "4.5.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" dependencies = [ "heck", "proc-macro2", @@ -161,9 +170,10 @@ checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" [[package]] name = "jumpy" -version = "0.4.3" +version = "0.4.4" dependencies = [ "clap", + "clap_complete", "dirs", "dunce", ] diff --git a/Cargo.toml b/Cargo.toml index 1f9bd91..dc6ef8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jumpy" -version = "0.4.3" +version = "0.4.4" edition = "2021" authors = ["Clément Nerma "] license = "Apache-2.0" @@ -10,5 +10,6 @@ repository = "https://github.com/ClementNerma/Jumpy" [dependencies] clap = { version = "4.5.4", features = ["derive"] } +clap_complete = "4.5.28" dirs = "5.0.1" dunce = "1.0.4" diff --git a/src/cmd.rs b/src/cmd.rs index 6de8fac..12dfc36 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -1,11 +1,11 @@ use std::path::PathBuf; -use clap::{Args, Parser, Subcommand}; +use clap::{Args, Parser, Subcommand, ValueEnum, ValueHint}; #[derive(Parser)] #[clap(author, version, about, long_about = None)] pub struct Command { - #[clap(short, long)] + #[clap(short, long, value_hint = ValueHint::FilePath)] pub index_file: Option, #[clap(subcommand)] @@ -46,17 +46,20 @@ pub enum Action { #[clap(about = "Get the path of the index file")] Path(Path), + + #[clap(about = "Generate completions for a given shell")] + Completions(Completions), } #[derive(Args)] pub struct Add { - #[clap()] + #[clap(value_hint = ValueHint::DirPath)] pub path: String, } #[derive(Args)] pub struct Inc { - #[clap()] + #[clap(value_hint = ValueHint::DirPath)] pub path: String, #[clap(long, help = "Give the maximum score to this directory")] @@ -68,7 +71,7 @@ pub struct Query { #[clap()] pub query: String, - #[clap(short, long)] + #[clap(short, long, value_hint = ValueHint::DirPath)] pub after: Option, #[clap(short, long)] @@ -86,7 +89,7 @@ pub struct List { #[derive(Args)] pub struct Del { - #[clap()] + #[clap(value_hint = ValueHint::DirPath)] pub path: String, } @@ -108,3 +111,18 @@ pub struct Path { )] pub lossily: bool, } + +#[derive(Args)] +pub struct Completions { + #[clap(help = "Shell to generate completions for")] + pub for_shell: CompletionShellName, +} + +#[derive(Clone, Copy, ValueEnum)] +pub enum CompletionShellName { + Bash, + Zsh, + Fish, + Elvish, + PowerShell, +} diff --git a/src/main.rs b/src/main.rs index 2009944..f99c957 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,9 +7,14 @@ mod cmd; mod index; -use clap::Parser; +use std::{fs, io::stdout}; -use crate::index::IndexEntry; +use clap::{CommandFactory, Parser}; + +use crate::{ + cmd::*, + index::{Index, IndexEntry}, +}; static INDEX_FILENAME: &str = "jumpy.db"; @@ -19,10 +24,6 @@ fn fail(message: &str) -> ! { } fn main() { - use cmd::*; - use index::Index; - use std::fs; - let cmd = Command::parse(); let index_file = cmd.index_file.unwrap_or_else(|| { @@ -128,6 +129,22 @@ fn main() { } } }, + + Action::Completions(Completions { for_shell }) => { + use clap_complete::*; + + let shell = match for_shell { + CompletionShellName::Bash => Shell::Bash, + CompletionShellName::Zsh => Shell::Zsh, + CompletionShellName::Fish => Shell::Fish, + CompletionShellName::Elvish => Shell::Elvish, + CompletionShellName::PowerShell => Shell::PowerShell, + }; + + let cmd = &mut Command::command(); + + aot::generate(shell, cmd, cmd.get_name().to_string(), &mut stdout()); + } } let updated = index.encode();