use std::fmt::Write as _;
use clap::CommandFactory;
use crate::cli::Cli;
pub fn render() -> std::io::Result<Vec<u8>> {
let man = clap_mangen::Man::new(Cli::command());
let mut buf = Vec::new();
man.render_title(&mut buf)?;
man.render_name_section(&mut buf)?;
man.render_synopsis_section(&mut buf)?;
man.render_description_section(&mut buf)?;
man.render_options_section(&mut buf)?;
let mut extra = String::new();
render_commands(&mut extra);
render_exit_status(&mut extra);
render_files(&mut extra);
buf.extend_from_slice(extra.as_bytes());
man.render_version_section(&mut buf)?;
Ok(buf)
}
fn render_commands(out: &mut String) {
out.push_str(".SH COMMANDS\n");
for sub in Cli::command().get_subcommands() {
if sub.is_hide_set() {
continue;
}
if sub.has_subcommands() {
for inner in sub.get_subcommands().filter(|c| !c.is_hide_set()) {
render_verb(out, &format!("sokf {}", sub.get_name()), inner);
}
} else {
render_verb(out, "sokf", sub);
}
}
}
fn render_verb(out: &mut String, prefix: &str, sub: &clap::Command) {
let _ = writeln!(out, ".SS \"{}\"", usage(prefix, sub));
let about = sub.get_about().map(ToString::to_string).unwrap_or_default();
out.push_str(&text(&about));
for arg in sub.get_arguments() {
if arg.get_id() == "help" || arg.is_global_set() {
continue;
}
out.push_str(".TP\n");
let _ = writeln!(out, "{}", arg_header(arg));
let help = arg.get_help().map(ToString::to_string).unwrap_or_default();
out.push_str(&text(&help));
}
}
fn usage(prefix: &str, sub: &clap::Command) -> String {
let mut s = format!("{prefix} {}", sub.get_name());
for pos in sub.get_positionals() {
if pos.is_required_set() {
let _ = write!(s, " <{}>", value_name(pos));
} else {
let _ = write!(s, " [{}]", value_name(pos));
}
}
if sub.get_arguments().any(|a| !a.is_positional()) {
s.push_str(" [OPTIONS]");
}
s
}
fn arg_header(arg: &clap::Arg) -> String {
if arg.is_positional() {
return format!("\\fI<{}>\\fR", value_name(arg));
}
let mut s = String::new();
match (arg.get_short(), arg.get_long()) {
(Some(short), Some(long)) => {
let _ = write!(s, "\\fB\\-{short}\\fR, \\fB\\-\\-{}\\fR", opt(long));
}
(Some(short), None) => {
let _ = write!(s, "\\fB\\-{short}\\fR");
}
(None, Some(long)) => {
let _ = write!(s, "\\fB\\-\\-{}\\fR", opt(long));
}
(None, None) => {}
}
if arg.get_action().takes_values() {
let _ = write!(s, " \\fI<{}>\\fR", value_name(arg));
}
s
}
fn value_name(arg: &clap::Arg) -> String {
arg.get_value_names()
.and_then(|names| names.first())
.map_or_else(
|| arg.get_id().to_string().to_uppercase(),
ToString::to_string,
)
}
fn render_exit_status(out: &mut String) {
out.push_str(
".SH EXIT STATUS\n\
.TP\n.B 0\nThe command succeeded and found no errors.\n\
.TP\n.B 1\nThe command found errors in the knowledge.\n\
.TP\n.B 2\nA usage or internal error occurred. sokf reports it on stderr.\n",
);
}
fn render_files(out: &mut String) {
out.push_str(
".SH FILES\n\
.TP\n.I /.sokf/config.toml\nThis optional file lists the knowledge roots and the paths that the repository excludes.\n\
.TP\n.I /.sokf/formats.yaml\nThis optional file holds the line formats defined for this repository.\n\
.TP\n.I /.sokf/roots.d/\nThis optional folder holds files *.toml, at any depth, that each list further knowledge roots.\n\
.TP\n.I /.sokf/formats.d/\nThis optional folder holds files *.yaml, at any depth, of further line formats, of which no file takes precedence over another, so /.sokf/formats.yaml decides a format or a type that two files declare differently.\n\
.TP\n.I /.sokf/cache/\nThis directory holds a cache entry for each file, and git ignores the directory. The \\-\\-no\\-cache flag skips the cache.\n\
.TP\n.I <root>/manifest.sokf.yaml\nThis file is the manifest of a knowledge store.\n",
);
}
fn text(s: &str) -> String {
let escaped = s.replace('\\', "\\\\");
if escaped.starts_with('.') || escaped.starts_with('\'') {
format!("\\&{escaped}\n")
} else {
format!("{escaped}\n")
}
}
fn opt(long: &str) -> String {
long.replace('-', "\\-")
}