[][src]Macro tcprint::tcprint

macro_rules! tcprint {
    ($cps:expr, $($clause:tt),*) => { ... };
}

Print to standard output with colorization, without a trailing newline.

The arguments to this macro are structured as:

This example is not tested
tcprint!(state_object, clause1, ...clauseN);

Where state is a ColorPrintState and each clause takes on one of the following forms:

  • (format, args...) to print without applying colorization
  • [colorname: format, args...] to print applying the named color
  • {colors_var, block: format, args...} to print with a color chosen dynamically by evaluating a code block (see example below)

In all cases the format, args... items are passed through the standard Rust string formatting mechanism.

The colorname specifier should refer to a public field of the state object’s "colors" structure. If using the BasicColors structure, the available options are: green, yellow, red, and hl (highlight).

Examples

let attempt_num = 2;
let server = "example.com";
tcprint!(state,
   ("attempting to connect to "),
   [hl: "{}", server],
   (" ({}th attempt) ...", attempt_num)
);

Note that no spaces are inserted between clauses.

tcprint!(state,
   ("putting the "),
   [hl: "fun"],
   (" in dys"),
   [yellow: "fun"],
   ("ctional")
);

When using the {} specifier, the two parameters are the name of a variable that will be set to your "colors" structure, and a code block that should evaluate to a &ColorSpec that will then be used for the printing. This way you can choose colors dynamically based on the values of local variables:

let seconds_left = compute_time_left();

tcprint!(state,
    {colors, {
        if seconds_left < 5 { &colors.red } else { &colors.hl }
    }: "{}", seconds_left}, (" seconds left to abort")
);