Skip to content

Commit

Permalink
auto complete (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
kingwingfly authored Feb 5, 2024
1 parent ac397e6 commit bb8616d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fav"
version = "0.1.8"
version = "0.1.9"
authors = ["Louis <[email protected]>"]
description = "Back up your favorite online resources with CLI."
license = "MIT"
Expand All @@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 1 addition & 2 deletions src/cli/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::proto::data::Qn;
use clap::{builder::PossibleValue, ValueEnum};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use std::sync::OnceLock;
Expand All @@ -6,8 +7,6 @@ use tabled::{
settings::{object::Rows, Alignment, Style},
};

use crate::proto::data::Qn;

pub(crate) fn show_table<H, R>(header: H, rows: R)
where
H: IntoIterator,
Expand Down

0 comments on commit bb8616d

Please sign in to comment.