From 56972fa63dcb2e692c03b97f46245db26491abac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Sun, 24 Sep 2023 17:56:51 +0300 Subject: [PATCH] feat(cli): add shell completions and manpage generator --- Cargo.lock | 27 +++++++++++++++++++++++++++ Cargo.toml | 11 +++++++++++ src/bin/completions.rs | 32 ++++++++++++++++++++++++++++++++ src/bin/mangen.rs | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 src/bin/completions.rs create mode 100644 src/bin/mangen.rs diff --git a/Cargo.lock b/Cargo.lock index ef1271b..5d5c02a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -253,6 +253,15 @@ dependencies = [ "terminal_size", ] +[[package]] +name = "clap_complete" +version = "4.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4110a1e6af615a9e6d0a36f805d5c99099f8bab9b8042f5bc1fa220a4a89e36f" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.4.2" @@ -271,6 +280,16 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" +[[package]] +name = "clap_mangen" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b44f35c514163027542f7147797ff930523eea288e03642727348ef1a9666f6b" +dependencies = [ + "clap", + "roff", +] + [[package]] name = "cocoa" version = "0.22.0" @@ -444,6 +463,8 @@ name = "daktilo" version = "0.1.0" dependencies = [ "clap", + "clap_complete", + "clap_mangen", "colored", "dirs", "pretty_assertions", @@ -1142,6 +1163,12 @@ dependencies = [ "symphonia", ] +[[package]] +name = "roff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316" + [[package]] name = "rust-embed" version = "8.0.0" diff --git a/Cargo.toml b/Cargo.toml index cd16210..abbfeab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,15 @@ repository = "https://github.com/orhun/daktilo" keywords = ["typewriter", "keyboard", "nostalgic", "type"] categories = ["command-line-utilities", "multimedia"] edition = "2021" +default-run = "daktilo" + +[[bin]] +name = "daktilo-completions" +path = "src/bin/completions.rs" + +[[bin]] +name = "daktilo-mangen" +path = "src/bin/mangen.rs" [dependencies] rdev = { version = "0.5.3", features = ["serialize"] } @@ -28,6 +37,8 @@ toml = "0.8.0" regex = "1.9.5" serde_regex = "1.1.0" colored = "2.0.4" +clap_complete = "4.4.1" +clap_mangen = "0.2.14" [dev-dependencies] pretty_assertions = "1.4.0" diff --git a/src/bin/completions.rs b/src/bin/completions.rs new file mode 100644 index 0000000..8088ef0 --- /dev/null +++ b/src/bin/completions.rs @@ -0,0 +1,32 @@ +use clap::{CommandFactory, ValueEnum}; +use clap_complete::Shell; +use daktilo::args::Args; +use std::env; +use std::io::Result; + +/// Shell completions can be created with: +/// +/// ```sh +/// cargo run --bin daktilo-completions +/// ``` +/// +/// in a directory specified by the environment variable OUT_DIR. +/// See +fn main() -> Result<()> { + let out_dir = env::var("OUT_DIR").expect("OUT_DIR is not set"); + let mut app = Args::command(); + for &shell in Shell::value_variants() { + clap_complete::generate_to(shell, &mut app, env!("CARGO_PKG_NAME"), &out_dir)?; + } + println!("Completion scripts are generated in {out_dir:?}"); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn generate_completions() -> Result<()> { + main() + } +} diff --git a/src/bin/mangen.rs b/src/bin/mangen.rs new file mode 100644 index 0000000..e52cf33 --- /dev/null +++ b/src/bin/mangen.rs @@ -0,0 +1,36 @@ +use clap::CommandFactory; +use clap_mangen::Man; +use daktilo::args::Args; +use std::env; +use std::fs; +use std::io::Result; +use std::path::PathBuf; + +/// Man page can be created with: +/// +/// ```sh +/// cargo run --bin daktilo-mangen +/// ```` +/// +/// in a directory specified by the environment variable OUT_DIR. +/// See +fn main() -> Result<()> { + let out_dir = env::var("OUT_DIR").expect("OUT_DIR is not set"); + let out_path = PathBuf::from(out_dir).join(format!("{}.1", env!("CARGO_PKG_NAME"))); + let app = Args::command(); + let man = Man::new(app); + let mut buffer = Vec::::new(); + man.render(&mut buffer)?; + fs::write(&out_path, buffer)?; + println!("Man page is generated at {out_path:?}"); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn generate_manpage() -> Result<()> { + main() + } +}