Skip to content

Commit

Permalink
initial man page generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Dec 8, 2023
1 parent 5600b4a commit 86ddf98
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"Nonblank",
"nonprinting",
"pico",
"Roff",
"struct",
"uutils",
"xflags"
Expand Down
1 change: 1 addition & 0 deletions complete/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
roff = "0.2.1"
3 changes: 2 additions & 1 deletion complete/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
mod fish;
mod md;
mod zsh;
mod man;

#[derive(Default)]
pub struct Command {
Expand Down Expand Up @@ -39,7 +40,7 @@ pub fn render(c: &Command, shell: &str) -> String {
"md" => md::render(c),
"fish" => fish::render(c),
"zsh" => zsh::render(c),
"man" => panic!("manpages are not implemented yet"),
"man" => man::render(c),
"sh" | "bash" | "csh" | "elvish" | "powershell" => panic!("shell '{shell}' completion is not implemented yet!"),
_ => panic!("unknown option '{shell}'! Expected one of: \"md\", \"fish\", \"zsh\", \"man\", \"sh\", \"bash\", \"csh\", \"elvish\", \"powershell\""),
}
Expand Down
33 changes: 33 additions & 0 deletions complete/src/man.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::Command;
use roff::{bold, roman, Roff};

pub fn render(c: &Command) -> String {
let mut page = Roff::new();
page.control("TH", [&c.name.to_uppercase(), "1"]);
page.control("SH", ["NAME"]);
page.text([roman(&c.name)]);
page.control("SH", ["DESCRIPTION"]);
page.text([roman(&c.summary)]);
page.control("SH", ["OPTIONS"]);

for arg in &c.args {
page.control("TP", []);

let mut flags = Vec::new();
for l in &arg.long {
if !flags.is_empty() {
flags.push(roman(", "));
}
flags.push(bold(format!("--{l}")));
}
for s in &arg.short {
if !flags.is_empty() {
flags.push(roman(", "));
}
flags.push(bold(format!("-{s}")));
}
page.text(flags);
page.text([roman(&arg.help)]);
}
page.render()
}

0 comments on commit 86ddf98

Please sign in to comment.