pub struct Printer<'t> {
pub full_width: bool,
pub max_width: Option<usize>,
/* private fields */
}
Expand description
An object which you can configure to print the help of a command
Fields§
§full_width: bool
§max_width: Option<usize>
Implementations§
source§impl<'t> Printer<'t>
impl<'t> Printer<'t>
sourcepub fn new(cmd: Command) -> Self
pub fn new(cmd: Command) -> Self
Examples found in repository?
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
fn main() {
let args = Args::parse();
if args.help {
Printer::new(Args::command())
.with("introduction", INTRO)
.without("author")
.print_help();
return;
}
let (w, h) = (args.width, args.height);
println!("Computation strategy: {:?}", args.strategy);
println!("{w} x {h} = {}", w*h);
}
More examples
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
pub fn print_help() {
let mut printer = clap_help::Printer::new(Args::command())
.with("introduction", INTRO_TEMPLATE)
.without("author");
printer.template_keys_mut().push("examples");
printer.set_template("examples", EXAMPLES_TEMPLATE);
for (i, example) in EXAMPLES.iter().enumerate() {
printer
.expander_mut()
.sub("examples")
.set("example-number", i + 1)
.set("example-title", example.title)
.set("example-cmd", example.cmd)
.set_md("example-comments", example.comments);
}
printer.print_help();
}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
fn main() {
let args = Args::parse();
if args.help {
let mut printer = Printer::new(Args::command())
.without("author")
.with("introduction", INTRO)
.with("options", TEMPLATE_OPTIONS);
let skin = printer.skin_mut();
skin.headers[0].compound_style.set_fg(ansi(202));
skin.bold.set_fg(ansi(202));
skin.italic = termimad::CompoundStyle::with_fg(ansi(45));
skin.inline_code = termimad::CompoundStyle::with_fg(ansi(223));
printer.print_help();
return;
}
let (w, h) = (args.width, args.height);
println!("Computation strategy: {:?}", args.strategy);
println!("{w} x {h} = {}", w*h);
}
sourcepub fn make_skin() -> MadSkin
pub fn make_skin() -> MadSkin
Build a skin for the detected theme of the terminal (i.e. dark, light, or other)
sourcepub fn with_max_width(self, w: usize) -> Self
pub fn with_max_width(self, w: usize) -> Self
Set a maximal width, so that the whole terminal width isn’t used.
This may make some long sentences easier to read on super wide terminals, especially when the whole text is short. Depending on your texts and parameters, you may set up a width of 100 or 150.
sourcepub fn skin_mut(&mut self) -> &mut MadSkin
pub fn skin_mut(&mut self) -> &mut MadSkin
Give a mutable reference to the current skin (by default the automatically selected one) so that it can be modified
Examples found in repository?
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
fn main() {
let args = Args::parse();
if args.help {
let mut printer = Printer::new(Args::command())
.without("author")
.with("introduction", INTRO)
.with("options", TEMPLATE_OPTIONS);
let skin = printer.skin_mut();
skin.headers[0].compound_style.set_fg(ansi(202));
skin.bold.set_fg(ansi(202));
skin.italic = termimad::CompoundStyle::with_fg(ansi(45));
skin.inline_code = termimad::CompoundStyle::with_fg(ansi(223));
printer.print_help();
return;
}
let (w, h) = (args.width, args.height);
println!("Computation strategy: {:?}", args.strategy);
println!("{w} x {h} = {}", w*h);
}
sourcepub fn set_template(&mut self, key: &'static str, template: &'t str)
pub fn set_template(&mut self, key: &'static str, template: &'t str)
Change a template
Examples found in repository?
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
pub fn print_help() {
let mut printer = clap_help::Printer::new(Args::command())
.with("introduction", INTRO_TEMPLATE)
.without("author");
printer.template_keys_mut().push("examples");
printer.set_template("examples", EXAMPLES_TEMPLATE);
for (i, example) in EXAMPLES.iter().enumerate() {
printer
.expander_mut()
.sub("examples")
.set("example-number", i + 1)
.set("example-title", example.title)
.set("example-cmd", example.cmd)
.set_md("example-comments", example.comments);
}
printer.print_help();
}
sourcepub fn with(self, key: &'static str, template: &'t str) -> Self
pub fn with(self, key: &'static str, template: &'t str) -> Self
Change or add a template
Examples found in repository?
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
fn main() {
let args = Args::parse();
if args.help {
Printer::new(Args::command())
.with("introduction", INTRO)
.without("author")
.print_help();
return;
}
let (w, h) = (args.width, args.height);
println!("Computation strategy: {:?}", args.strategy);
println!("{w} x {h} = {}", w*h);
}
More examples
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
pub fn print_help() {
let mut printer = clap_help::Printer::new(Args::command())
.with("introduction", INTRO_TEMPLATE)
.without("author");
printer.template_keys_mut().push("examples");
printer.set_template("examples", EXAMPLES_TEMPLATE);
for (i, example) in EXAMPLES.iter().enumerate() {
printer
.expander_mut()
.sub("examples")
.set("example-number", i + 1)
.set("example-title", example.title)
.set("example-cmd", example.cmd)
.set_md("example-comments", example.comments);
}
printer.print_help();
}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
fn main() {
let args = Args::parse();
if args.help {
let mut printer = Printer::new(Args::command())
.without("author")
.with("introduction", INTRO)
.with("options", TEMPLATE_OPTIONS);
let skin = printer.skin_mut();
skin.headers[0].compound_style.set_fg(ansi(202));
skin.bold.set_fg(ansi(202));
skin.italic = termimad::CompoundStyle::with_fg(ansi(45));
skin.inline_code = termimad::CompoundStyle::with_fg(ansi(223));
printer.print_help();
return;
}
let (w, h) = (args.width, args.height);
println!("Computation strategy: {:?}", args.strategy);
println!("{w} x {h} = {}", w*h);
}
sourcepub fn without(self, key: &'static str) -> Self
pub fn without(self, key: &'static str) -> Self
Unset a template
Examples found in repository?
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
fn main() {
let args = Args::parse();
if args.help {
Printer::new(Args::command())
.with("introduction", INTRO)
.without("author")
.print_help();
return;
}
let (w, h) = (args.width, args.height);
println!("Computation strategy: {:?}", args.strategy);
println!("{w} x {h} = {}", w*h);
}
More examples
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
pub fn print_help() {
let mut printer = clap_help::Printer::new(Args::command())
.with("introduction", INTRO_TEMPLATE)
.without("author");
printer.template_keys_mut().push("examples");
printer.set_template("examples", EXAMPLES_TEMPLATE);
for (i, example) in EXAMPLES.iter().enumerate() {
printer
.expander_mut()
.sub("examples")
.set("example-number", i + 1)
.set("example-title", example.title)
.set("example-cmd", example.cmd)
.set_md("example-comments", example.comments);
}
printer.print_help();
}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
fn main() {
let args = Args::parse();
if args.help {
let mut printer = Printer::new(Args::command())
.without("author")
.with("introduction", INTRO)
.with("options", TEMPLATE_OPTIONS);
let skin = printer.skin_mut();
skin.headers[0].compound_style.set_fg(ansi(202));
skin.bold.set_fg(ansi(202));
skin.italic = termimad::CompoundStyle::with_fg(ansi(45));
skin.inline_code = termimad::CompoundStyle::with_fg(ansi(223));
printer.print_help();
return;
}
let (w, h) = (args.width, args.height);
println!("Computation strategy: {:?}", args.strategy);
println!("{w} x {h} = {}", w*h);
}
sourcepub fn template_keys_mut(&mut self) -> &mut Vec<&'static str>
pub fn template_keys_mut(&mut self) -> &mut Vec<&'static str>
A mutable reference to the list of template keys, so that you can insert new keys, or change their order. Any key without matching template will just be ignored
Examples found in repository?
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
pub fn print_help() {
let mut printer = clap_help::Printer::new(Args::command())
.with("introduction", INTRO_TEMPLATE)
.without("author");
printer.template_keys_mut().push("examples");
printer.set_template("examples", EXAMPLES_TEMPLATE);
for (i, example) in EXAMPLES.iter().enumerate() {
printer
.expander_mut()
.sub("examples")
.set("example-number", i + 1)
.set("example-title", example.title)
.set("example-cmd", example.cmd)
.set_md("example-comments", example.comments);
}
printer.print_help();
}
sourcepub fn template_order_mut(&mut self) -> &mut Vec<&'static str>
👎Deprecated since 0.6.2: use template_keys_mut instead
pub fn template_order_mut(&mut self) -> &mut Vec<&'static str>
A mutable reference to the list of template keys, so that you can insert new keys, or change their order. Any key without matching template will just be ignored
sourcepub fn expander_mut(&mut self) -> &mut OwningTemplateExpander<'static>
pub fn expander_mut(&mut self) -> &mut OwningTemplateExpander<'static>
Give you a mut reference to the expander, so that you can overload the variable of the expander used to fill the templates of the help, or add new variables for your own templates
Examples found in repository?
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
pub fn print_help() {
let mut printer = clap_help::Printer::new(Args::command())
.with("introduction", INTRO_TEMPLATE)
.without("author");
printer.template_keys_mut().push("examples");
printer.set_template("examples", EXAMPLES_TEMPLATE);
for (i, example) in EXAMPLES.iter().enumerate() {
printer
.expander_mut()
.sub("examples")
.set("example-number", i + 1)
.set("example-title", example.title)
.set("example-cmd", example.cmd)
.set_md("example-comments", example.comments);
}
printer.print_help();
}
sourcepub fn print_template(&self, template: &str)
pub fn print_template(&self, template: &str)
Print the provided template with the printer’s expander
It’s normally more convenient to change template_keys or some templates, unless you want none of the standard templates
sourcepub fn print_help(&self)
pub fn print_help(&self)
Print all the templates, in order
Examples found in repository?
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
fn main() {
let args = Args::parse();
if args.help {
Printer::new(Args::command())
.with("introduction", INTRO)
.without("author")
.print_help();
return;
}
let (w, h) = (args.width, args.height);
println!("Computation strategy: {:?}", args.strategy);
println!("{w} x {h} = {}", w*h);
}
More examples
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
pub fn print_help() {
let mut printer = clap_help::Printer::new(Args::command())
.with("introduction", INTRO_TEMPLATE)
.without("author");
printer.template_keys_mut().push("examples");
printer.set_template("examples", EXAMPLES_TEMPLATE);
for (i, example) in EXAMPLES.iter().enumerate() {
printer
.expander_mut()
.sub("examples")
.set("example-number", i + 1)
.set("example-title", example.title)
.set("example-cmd", example.cmd)
.set_md("example-comments", example.comments);
}
printer.print_help();
}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
fn main() {
let args = Args::parse();
if args.help {
let mut printer = Printer::new(Args::command())
.without("author")
.with("introduction", INTRO)
.with("options", TEMPLATE_OPTIONS);
let skin = printer.skin_mut();
skin.headers[0].compound_style.set_fg(ansi(202));
skin.bold.set_fg(ansi(202));
skin.italic = termimad::CompoundStyle::with_fg(ansi(45));
skin.inline_code = termimad::CompoundStyle::with_fg(ansi(223));
printer.print_help();
return;
}
let (w, h) = (args.width, args.height);
println!("Computation strategy: {:?}", args.strategy);
println!("{w} x {h} = {}", w*h);
}