Skip to content

Commit

Permalink
feat(cli): add shell completions and manpage generator
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Sep 24, 2023
1 parent 009971b commit 56972fa
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"
Expand Down
32 changes: 32 additions & 0 deletions src/bin/completions.rs
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
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()
}
}
36 changes: 36 additions & 0 deletions src/bin/mangen.rs
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
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::<u8>::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()
}
}

0 comments on commit 56972fa

Please sign in to comment.