From 82cd486efa4e900f3edac2264086e56777db0105 Mon Sep 17 00:00:00 2001 From: TD-Sky Date: Mon, 16 Oct 2023 20:42:11 +0800 Subject: [PATCH] feat(completions): generate shell completion scripts --- Cargo.lock | 32 ++++++++++++++++++++++++++++++++ app/Cargo.toml | 30 +++++++++++++++--------------- config/Cargo.toml | 6 ++++++ config/build.rs | 29 +++++++++++++++++++++++++++++ config/src/boot/boot.rs | 22 ++-------------------- config/src/boot/cli.rs | 22 ++++++++++++++++++++++ config/src/boot/mod.rs | 1 + 7 files changed, 107 insertions(+), 35 deletions(-) create mode 100644 config/build.rs create mode 100644 config/src/boot/cli.rs diff --git a/Cargo.lock b/Cargo.lock index 92d7620f..edf46a8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -307,6 +307,35 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ae8ba90b9d8b007efe66e55e48fb936272f5ca00349b5b0e89877520d35ea7" +dependencies = [ + "clap", +] + +[[package]] +name = "clap_complete_fig" +version = "4.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29bdbe21a263b628f83fcbeac86a4416a1d588c7669dd41473bc4149e4e7d2f1" +dependencies = [ + "clap", + "clap_complete", +] + +[[package]] +name = "clap_complete_nushell" +version = "4.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df47268eb308aaac1ffbfcd8ae43e43820cd094a051b03956d238f2bc60dc3fe" +dependencies = [ + "clap", + "clap_complete", +] + [[package]] name = "clap_derive" version = "4.4.2" @@ -363,6 +392,9 @@ version = "0.1.5" dependencies = [ "anyhow", "clap", + "clap_complete", + "clap_complete_fig", + "clap_complete_nushell", "crossterm", "dirs", "futures", diff --git a/app/Cargo.toml b/app/Cargo.toml index 7fe24117..891b00f0 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -1,32 +1,32 @@ [package] -name = "app" +name = "app" version = "0.1.5" edition = "2021" [dependencies] adaptor = { path = "../adaptor" } -config = { path = "../config" } -core = { path = "../core" } -plugin = { path = "../plugin" } -shared = { path = "../shared" } +config = { path = "../config" } +core = { path = "../core" } +plugin = { path = "../plugin" } +shared = { path = "../shared" } # External dependencies -ansi-to-tui = "^3" -anyhow = "^1" -crossterm = { version = "^0", features = [ "event-stream" ] } -futures = "^0" -ratatui = "^0" -tokio = { version = "^1", features = [ "parking_lot" ] } +ansi-to-tui = "^3" +anyhow = "^1" +crossterm = { version = "^0", features = ["event-stream"] } +futures = "^0" +ratatui = "^0" +tokio = { version = "^1", features = ["parking_lot"] } unicode-width = "^0" # Logging -tracing = "^0" -tracing-appender = "^0" +tracing = "^0" +tracing-appender = "^0" tracing-subscriber = "^0" [target."cfg(unix)".dependencies] -libc = "^0" -signal-hook-tokio = { version = "^0", features = [ "futures-v0_3" ] } +libc = "^0" +signal-hook-tokio = { version = "^0", features = ["futures-v0_3"] } [[bin]] name = "yazi" diff --git a/config/Cargo.toml b/config/Cargo.toml index 7f398c97..725f16a4 100644 --- a/config/Cargo.toml +++ b/config/Cargo.toml @@ -20,3 +20,9 @@ serde = { version = "^1", features = [ "derive" ] } shell-words = "^1" toml = { version = "^0", features = [ "preserve_order" ] } validator = { version = "^0", features = [ "derive" ] } + +[build-dependencies] +clap = { version = "^4", features = [ "derive" ] } +clap_complete = "^4" +clap_complete_nushell = "^4" +clap_complete_fig = "^4" diff --git a/config/build.rs b/config/build.rs new file mode 100644 index 00000000..340c88a5 --- /dev/null +++ b/config/build.rs @@ -0,0 +1,29 @@ +#[path = "src/boot/cli.rs"] +mod cli; + +use std::{fs, io}; + +use clap::CommandFactory; +use clap_complete::{generate_to, Shell::*}; +use clap_complete_fig::Fig; +use clap_complete_nushell::Nushell; + +use self::cli::Args; + +fn main() -> io::Result<()> { + let cmd = &mut Args::command(); + let name = "yazi"; + let dir = "completions"; + + fs::create_dir_all(dir)?; + + generate_to(Bash, cmd, name, dir)?; + generate_to(Fish, cmd, name, dir)?; + generate_to(Zsh, cmd, name, dir)?; + generate_to(Elvish, cmd, name, dir)?; + generate_to(PowerShell, cmd, name, dir)?; + generate_to(Nushell, cmd, name, dir)?; + generate_to(Fig, cmd, name, dir)?; + + Ok(()) +} diff --git a/config/src/boot/boot.rs b/config/src/boot/boot.rs index df3a207e..518103b7 100644 --- a/config/src/boot/boot.rs +++ b/config/src/boot/boot.rs @@ -1,8 +1,9 @@ use std::{env, fs, path::PathBuf, process}; -use clap::{command, Parser}; +use clap::Parser; use shared::expand_path; +use super::cli::Args; use crate::{Xdg, PREVIEW}; #[derive(Debug)] @@ -14,25 +15,6 @@ pub struct Boot { pub chooser_file: Option, } -#[derive(Debug, Parser)] -#[command(name = "yazi", version)] -struct Args { - /// Set the current working directory - #[arg(index = 1)] - cwd: Option, - - /// Write the cwd on exit to this file - #[arg(long)] - cwd_file: Option, - /// Write the selected files on open emitted by the chooser mode - #[arg(long)] - chooser_file: Option, - - /// Clear the cache directory - #[arg(long, action)] - clear_cache: bool, -} - impl Default for Boot { fn default() -> Self { let args = Args::parse(); diff --git a/config/src/boot/cli.rs b/config/src/boot/cli.rs new file mode 100644 index 00000000..bd1f3b8e --- /dev/null +++ b/config/src/boot/cli.rs @@ -0,0 +1,22 @@ +use std::path::PathBuf; + +use clap::{command, Parser}; + +#[derive(Debug, Parser)] +#[command(name = "yazi", version)] +pub(super) struct Args { + /// Set the current working directory + #[arg(index = 1)] + pub cwd: Option, + + /// Write the cwd on exit to this file + #[arg(long)] + pub cwd_file: Option, + /// Write the selected files on open emitted by the chooser mode + #[arg(long)] + pub chooser_file: Option, + + /// Clear the cache directory + #[arg(long, action)] + pub clear_cache: bool, +} diff --git a/config/src/boot/mod.rs b/config/src/boot/mod.rs index 5922b49a..865dc405 100644 --- a/config/src/boot/mod.rs +++ b/config/src/boot/mod.rs @@ -1,3 +1,4 @@ mod boot; +mod cli; pub use boot::*;