Struct clap_help::Printer

source ·
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>

source

pub fn new(cmd: Command) -> Self

Examples found in repository?
examples/area/main.rs (line 54)
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
Hide additional examples
examples/with-examples/main.rs (line 54)
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();
}
examples/custom/main.rs (line 69)
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);
}
source

pub fn make_skin() -> MadSkin

Build a skin for the detected theme of the terminal (i.e. dark, light, or other)

source

pub fn with_skin(self, skin: MadSkin) -> Self

Use the provided skin

source

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.

source

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?
examples/custom/main.rs (line 73)
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);
}
source

pub fn set_template(&mut self, key: &'static str, template: &'t str)

Change a template

Examples found in repository?
examples/with-examples/main.rs (line 58)
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();
}
source

pub fn with(self, key: &'static str, template: &'t str) -> Self

Change or add a template

Examples found in repository?
examples/area/main.rs (line 55)
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
Hide additional examples
examples/with-examples/main.rs (line 55)
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();
}
examples/custom/main.rs (line 71)
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);
}
source

pub fn without(self, key: &'static str) -> Self

Unset a template

Examples found in repository?
examples/area/main.rs (line 56)
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
Hide additional examples
examples/with-examples/main.rs (line 56)
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();
}
examples/custom/main.rs (line 70)
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);
}
source

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?
examples/with-examples/main.rs (line 57)
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();
}
source

pub fn template_order_mut(&mut self) -> &mut Vec<&'static str>

👎Deprecated since 0.6.2: use template_keys_mut instead

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

source

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?
examples/with-examples/main.rs (line 61)
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();
}
source

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

source

pub fn print_help(&self)

Print all the templates, in order

Examples found in repository?
examples/area/main.rs (line 57)
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
Hide additional examples
examples/with-examples/main.rs (line 68)
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();
}
examples/custom/main.rs (line 78)
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);
}

Auto Trait Implementations§

§

impl<'t> RefUnwindSafe for Printer<'t>

§

impl<'t> Send for Printer<'t>

§

impl<'t> Sync for Printer<'t>

§

impl<'t> Unpin for Printer<'t>

§

impl<'t> UnwindSafe for Printer<'t>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.