From 1eed93f7d396c849e87e068ca4c22af8851385cb Mon Sep 17 00:00:00 2001 From: mautam Date: Sat, 15 Jan 2022 08:33:44 -0600 Subject: [PATCH] Autocomplete(*): Attempt to begin adding autocomplete support, #45 --- Cargo.lock | 41 ++++++++++++++++------------------------- Cargo.toml | 1 + src/main.rs | 37 ++++++++++++++++++++++++------------- src/models/mod.rs | 2 ++ src/models/shells.rs | 26 ++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 38 deletions(-) create mode 100644 src/models/shells.rs diff --git a/Cargo.lock b/Cargo.lock index 55a14b4..5a90d97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,9 +69,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.0.0-beta.5" +version = "3.0.0-rc.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "feff3878564edb93745d58cf63e17b63f24142506e7a20c87a5521ed7bfb1d63" +checksum = "484f17839417b695a6f4a75c20e49820ba0a1d00aa41ebd8ba0e5dfe0fbc3b74" dependencies = [ "atty", "bitflags", @@ -82,14 +82,13 @@ dependencies = [ "strsim", "termcolor", "textwrap", - "unicase", ] [[package]] name = "clap_derive" -version = "3.0.0-beta.5" +version = "3.0.0-rc.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b15c6b4f786ffb6192ffe65a36855bc1fc2444bcd0945ae16748dcd6ed7d0d3" +checksum = "a86d4ec799f94ddc4a4a4edf652f89b360219905f86edcb8abe5974dfef135b8" dependencies = [ "heck", "proc-macro-error", @@ -98,6 +97,15 @@ dependencies = [ "syn", ] +[[package]] +name = "clap_generate" +version = "3.0.0-rc.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46edf9e899ef90f3cefafb75dfae4c52746911cb905408bdf147a4bfe6fd72aa" +dependencies = [ + "clap", +] + [[package]] name = "colored" version = "2.0.0" @@ -494,6 +502,7 @@ name = "leftwm-theme" version = "0.1.1" dependencies = [ "clap", + "clap_generate", "colored", "dirs-next", "edit-distance", @@ -686,9 +695,9 @@ dependencies = [ [[package]] name = "os_str_bytes" -version = "4.2.0" +version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "addaa943333a514159c80c97ff4a93306530d965d27e139188283cd13e06a799" +checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" dependencies = [ "memchr", ] @@ -1047,9 +1056,6 @@ name = "textwrap" version = "0.14.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" -dependencies = [ - "unicode-width", -] [[package]] name = "thread_local" @@ -1156,15 +1162,6 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" -[[package]] -name = "unicase" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" -dependencies = [ - "version_check", -] - [[package]] name = "unicode-bidi" version = "0.3.7" @@ -1186,12 +1183,6 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" -[[package]] -name = "unicode-width" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" - [[package]] name = "unicode-xid" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index eb7d57a..043e2f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ readme = "README.md" [dependencies] clap = { version = "3.0.0-beta.4", features=["derive"] } +clap_generate = {version = "3.0.0-beta.4"} colored = "2.0.0" dirs-next = "2.0.0" edit-distance = "2.1.0" diff --git a/src/main.rs b/src/main.rs index 330f528..8aa6719 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,11 +25,11 @@ pub mod utils; use colored::Colorize; use errors::{LeftErrorKind, Result}; -use crate::models::Config; +use crate::models::{Config, Shell}; use crate::operations::{ Apply, Current, Install, List, New, Search, Status, Uninstall, Update, Upgrade, }; -use clap::Parser; +use clap::{IntoApp, Parser}; use log::error; use std::env; @@ -41,7 +41,9 @@ pub struct Opt { pub verbose: u8, /// Operation to be performed by the theme manager #[clap(subcommand)] - pub operation: Operation, + pub operation: Option, + #[clap(long, short, arg_enum, value_name="SHELL")] + pub completions: Option } #[derive(Parser, Debug)] @@ -88,16 +90,25 @@ fn main() { let wrapper: Result<()> = match opt.operation { //Operation::AutoFind(args) => AutoFind::exec(&args), - Operation::Install(args) => Install::exec(&args, &mut config), - Operation::Uninstall(args) => Uninstall::exec(&args, &mut config), - Operation::List(args) => List::exec(&args, &mut config), - Operation::Apply(args) => Apply::exec(&args, &mut config), - Operation::Status(args) => Status::exec(&args, &mut config), - Operation::New(args) => New::exec(&args, &mut config), - Operation::Upgrade(args) => Upgrade::exec(&args, &mut config), - Operation::Update(args) => Update::exec(&args, &mut config), - Operation::Search(args) => Search::exec(&args, &mut config), - Operation::Current(args) => Current::exec(&args, &mut config), + Some(Operation::Install(args)) => Install::exec(&args, &mut config), + Some(Operation::Uninstall(args)) => Uninstall::exec(&args, &mut config), + Some(Operation::List(args)) => List::exec(&args, &mut config), + Some(Operation::Apply(args)) => Apply::exec(&args, &mut config), + Some(Operation::Status(args)) => Status::exec(&args, &mut config), + Some(Operation::New(args)) => New::exec(&args, &mut config), + Some(Operation::Upgrade(args)) => Upgrade::exec(&args, &mut config), + Some(Operation::Update(args)) => Update::exec(&args, &mut config), + Some(Operation::Search(args)) => Search::exec(&args, &mut config), + Some(Operation::Current(args)) => Current::exec(&args, &mut config), + None => { + if let Some(shell) = opt.completions { + let app = Opt::into_app(); + shell.generate(app); + Ok(()) + } else { + Err(errors::friendly_message("No command passed")) + } + } }; if let Err(e) = wrapper { diff --git a/src/models/mod.rs b/src/models/mod.rs index 09d02ea..85f54e6 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,7 +1,9 @@ mod config; mod leftwm; mod theme; +mod shells; pub use config::{Config, Repo, THEMES_DIR}; pub use leftwm::LeftWm; pub use theme::{DependencyL, Theme}; +pub use shells::Shell; diff --git a/src/models/shells.rs b/src/models/shells.rs new file mode 100644 index 0000000..18ae857 --- /dev/null +++ b/src/models/shells.rs @@ -0,0 +1,26 @@ +use clap::{App, ArgEnum}; +use clap_generate::{generators::*, generate}; + +#[derive(ArgEnum, Copy, Clone, Debug)] +pub enum Shell { + Bash, + Zsh, + Fish, + PowerShell, + Elvish +} + +impl Shell{ + pub fn generate(&self, mut app: App) { + let mut fd = std::io::stdout(); + match self{ + Shell::Bash => generate::(Bash, &mut app, "leftwm-theme", &mut fd), + Shell::Zsh => generate::(Zsh, &mut app, "leftwm-theme", &mut fd), + Shell::Fish => generate::(Fish, &mut app, "leftwm-theme", &mut fd), + Shell::PowerShell => generate::(PowerShell, &mut app, "leftwm-theme", &mut fd), + Shell::Elvish => generate::(Elvish, &mut app, "leftwm-theme", &mut fd), + } + } +} + +