diff --git a/Cargo.lock b/Cargo.lock index ce66693..b92f4a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -113,6 +113,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "660c0520455b1013b9bcb0393d5f643d7e4454fb69c915b8d6d2aa0e9a45acc3" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.6.1" @@ -453,6 +462,7 @@ name = "kotofetch" version = "0.2.21" dependencies = [ "clap", + "clap_complete", "console", "ctrlc", "dirs", diff --git a/Cargo.toml b/Cargo.toml index 3b6562d..98a83db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ license = "MIT" [dependencies] clap = { version = "4.5.47", features = ["derive"] } +clap_complete = { version = "4.6.3" } console = "0.16.1" ctrlc = "3.5.0" dirs = "6.0.0" diff --git a/README.md b/README.md index 58b79b0..43894d3 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ - [Custom quotes](#custom-quotes) - [Usage](#usage) - [Translation Modes](#translation-modes) +- [Autocompletion](#autocompletion) - [Community Showcase](#community-showcase) - [Contributing](#contributing) @@ -241,6 +242,15 @@ Multiple modes can be combined by passing a comma-separated list: `--translation Furigana displays readings aligned with their kanji, supporting both single-kanji annotations (`知(し)`) and compound words (`大海(たいかい)`). Only quotes that include inline ruby markup in their `japanese` field will show furigana readings. Use `--furigana-position above` (or `furigana_position = "above"` in config) to render readings above the Japanese text instead of below. +## Autocompletion + +You can get the script by running this command: +```bash +kotofetch completion +``` +The setup depends on the specific shell. + +The supported shells are those supported by the currently used `clap_complete` version. ## Community Showcase diff --git a/src/cli.rs b/src/cli.rs index 514c65c..b57e06a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,8 +1,9 @@ use clap::{Args, Parser, Subcommand, ValueEnum}; +use clap_complete::Shell; use std::path::PathBuf; #[derive(Parser, Debug)] -#[command(author, version, about)] +#[command(name = "kotofetch", author, version, about)] pub struct Cli { #[command(subcommand)] pub command: Option, @@ -87,6 +88,11 @@ pub enum Commands { #[command(subcommand)] source: InitSource, }, + /// Output the completion script + Completion { + /// Select the shell + shell: Shell, + }, } #[derive(Subcommand, Debug)] diff --git a/src/main.rs b/src/main.rs index 7defee3..711a8f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,9 @@ mod display; mod quotes; use crate::cli::{Cli, Commands, InitSource}; -use clap::Parser; +use clap::{CommandFactory, Parser}; +use clap_complete::generate; +use std::io; fn main() { let cli = Cli::parse(); @@ -19,6 +21,11 @@ fn main() { std::process::exit(1); } } + Some(Commands::Completion { shell }) => { + let mut cmd = Cli::command(); + let cmdname = cmd.get_name().to_string(); + generate(*shell, &mut cmd, cmdname, &mut io::stdout()); + } None => { let user_cfg = config::load_user_config(cli.config.clone()); let runtime = config::make_runtime_config(user_cfg, &cli);