diff --git a/CHANGELOG.md b/CHANGELOG.md index fa43406..743db2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,10 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com ## [Unreleased] -### [0.1.9] - 2024-02-05 +### [0.1.9] - 2024-02-06 +- auto complete support for `zsh` and `fish`; Run `fav complete -h` for more information. (e.g. run `fav complete --shell fish --register ~/.config/fish/completions` to register the auto completion script for `fish`) +- I'll also upload some other auto completion scripts for `bash` and `powershell` and so on. ## [0.1.8] - 2024-02-05 diff --git a/Cargo.toml b/Cargo.toml index 8dac75a..d989c1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fav" -version = "0.1.8" +version = "0.1.9" authors = ["Louis <836250617@qq.com>"] description = "Back up your favorite online resources with CLI." license = "MIT" @@ -15,6 +15,7 @@ documentation = "" clap = { version = "4.4", features = ["derive"]} tabled = "0.15.0" indicatif = { version = "0.17.7", features = ["tokio"] } +clap_complete = { version = "4", features = ["unstable-dynamic"] } # Error snafu = { version = "0.8" } # Serde diff --git a/src/cli/mod.rs b/src/cli/mod.rs index c991769..fc887bc 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,7 +1,9 @@ //! The CLI module. pub(crate) mod utils; -use clap::{error::ErrorKind, CommandFactory, Parser, Subcommand, ValueEnum}; +use clap::{ + error::ErrorKind, Command, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum, +}; use crate::{ api::{ @@ -125,8 +127,20 @@ pub(crate) enum Kind { } impl Cli { + fn build_cli() -> Command { + clap_complete::dynamic::shells::CompleteCommand::augment_subcommands(Self::command()) + } + /// Run the CLI. pub async fn run() { + let cli = Self::build_cli(); + let matches = cli.get_matches(); + if let Ok(completions) = + clap_complete::dynamic::shells::CompleteCommand::from_arg_matches(&matches) + { + completions.complete(&mut Self::build_cli()); + return; + }; let args = Self::parse(); match args.subcmd { Commands::Init { path, kind } => init(path, kind).await.unwrap(), diff --git a/src/cli/utils.rs b/src/cli/utils.rs index bdbf4a4..e32778b 100644 --- a/src/cli/utils.rs +++ b/src/cli/utils.rs @@ -1,3 +1,4 @@ +use crate::proto::data::Qn; use clap::{builder::PossibleValue, ValueEnum}; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use std::sync::OnceLock; @@ -6,8 +7,6 @@ use tabled::{ settings::{object::Rows, Alignment, Style}, }; -use crate::proto::data::Qn; - pub(crate) fn show_table(header: H, rows: R) where H: IntoIterator,