From d8bc6ee19c5ed1b6cd191bd2bf72bee3f652fc84 Mon Sep 17 00:00:00 2001 From: Daniel Sockwell Date: Mon, 21 Jan 2019 14:57:42 -0500 Subject: [PATCH] Add version and date API --- README.md | 5 ++++- examples/main.rs | 2 ++ src/man.rs | 43 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 08111c1..2379de6 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ use man::prelude::*; fn main() { let page = Manual::new("basic") .about("A basic example") + .date("January, 2019") + .version(env!("CARGO_PKG_VERSION")) .author(Author::new("Alice Person").email("alice@person.com")) .author(Author::new("Bob Human").email("bob@human.com")) .flag( @@ -50,7 +52,7 @@ $ cargo run > /tmp/app.man; man /tmp/app.man ``` Which outputs: ```txt -BASIC(1) General Commands Manual BASIC(1) +BASIC(1) USER COMMANDS BASIC(1) NAME basic - A basic example @@ -81,6 +83,7 @@ EXIT STATUS AUTHORS Alice Person Bob Human +1.0.2 January, 2019 BASIC(1) ``` ## Installation diff --git a/examples/main.rs b/examples/main.rs index 0603e1d..81fbe27 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -5,6 +5,8 @@ use man::prelude::*; fn main() { let msg = Manual::new("auth-service") .about("authorize & authenticate members") + .version(env!("CARGO_PKG_VERSION")) + .date("February 2019") .arg(Arg::new("path")) .env(Env::new("PORT").help("The network port to listen to")) .flag( diff --git a/src/man.rs b/src/man.rs index 4bce1da..3c997eb 100644 --- a/src/man.rs +++ b/src/man.rs @@ -7,6 +7,8 @@ pub struct Manual { name: String, about: Option, description: Option, + date: Option, + version: Option, authors: Vec, flags: Vec, options: Vec, @@ -22,6 +24,8 @@ impl Manual { name: name.into(), about: None, description: None, + date: None, + version: None, authors: vec![], flags: vec![], options: vec![], @@ -43,6 +47,18 @@ impl Manual { self } + /// Add a last modified date + pub fn date>(mut self, date: S) -> Self { + self.date = Some(date.into()); + self + } + + /// Add a version + pub fn version>(mut self, version: S) -> Self { + self.version = Some(version.into()); + self + } + /// Add an author. pub fn author(mut self, author: Author) -> Self { self.authors.push(author); @@ -83,8 +99,8 @@ impl Manual { /// Render to a string. pub fn render(self) -> String { - let man_num = 1; - let mut page = Roff::new(&self.name, man_num); + let title_line = title_line(self.name.clone(), self.version, self.date); + let mut page = Roff::new(&title_line, 5); page = about(page, &self.name, &self.about); page = synopsis( page, @@ -374,6 +390,29 @@ fn custom(page: Roff, custom_section: Section) -> Roff { page.section(&custom_section.name, ¶graphs) } +fn title_line( + name: String, + version: Option, + date: Option, +) -> String { + // NOTE(ds): This is less elegant than it could be because Roff::new isn't set up + // to accept date or version information right now. I'll submit a PR to that + // crate, but this (mostly) works in the meantime + let man_num: i8 = 1; + let mut title_line = format!("{} \"{}\"", name, man_num); + match date { + // This Roff::new capitalizes the date, incorrectly + Some(date) => title_line.push_str(format!(" \"{}\"", date).as_str()), + None => title_line.push_str(" \" \""), + } + match version { + Some(version) => title_line.push_str(format!(" \"{}\"", version).as_str()), + None => title_line.push_str(" \" \""), + } + title_line.push_str(" \"User Commands\""); + title_line +} + // NOTE(yw): This code was taken from the npm-install(1) command. The location // on your system may vary. In all honesty I just copy-pasted this. We should // probably port this to troff-rs at some point.