Using owo_colors Extension Traits to colorize terminal output

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet

We can print lines to the terminal and ask the user for various input. So let's make the interface a bit more usable by using colored output.

We can do this by bringing an extension trait for formatting into scope: OwoColorize.

Extension traits are one-off traits that are used to add functions to a type that our crate doesn't own. A crate can create the new trait, then implement the functions defined by that trait on a type the crate doesn't own and if we bring that trait into scope, the type will get the extra functions. In this case, OwoColorize is used for adding color functions to formatters like Display and Debug.

We'll start with ask_for_filename

use color_eyre::owo_colors::OwoColorize;

fn ask_for_filename() -> Result<String> {
    rprompt::prompt_reply_stderr(&format!(
        "{}",
        "\
Enter filename
> "
        .blue()
        .bold(),
    ))
    .wrap_err("Failed to get filename")
    .map(|title| slug::slugify(title))
}

In confirm_filename we can use multiple formatters to get different colors:

let result = rprompt::prompt_reply_stderr(&format!(
    "\
{} {}
Do you want a different title? (y/N): ",
    "current title:".green().bold(),
    raw_title,
))
.wrap_err("Failed to get input for y/n question")?;

Instructor: [0:00] We have two functions for interacting with the user and printing lines to terminal asking for input. Let's make this nicer to use by adding colors, so that the user can differentiate between different pieces of the input. We can do this by bringing in extension trait for formatting into Scope called OwoColorize.

[0:22] In this case, color-eyre re-exports it, so we can pull it in from there. It's also its own package if you want to use this in other projects. Extension traits are one-off traits that are used to add functions to a type that our create doesn't own.

[0:38] A create can create the new trait and implement the functions defined by that trait on a type that create doesn't own. If we bring that trait into Scope, like we just did, the type will get the extra functions. In this case, OwoColorize is used for adding color functions to formatters like display and debug.

[1:01] We'll start with ask for file name. To access the extension trait functions we need to use a format. Now that we've introduced the format macro, we'll add a Display formatter.

[1:18] This means that the string that we're passing in here will be formatted with the Display trait in this string here. This allows us to use the extension traits on this string, which we can then do to make it blue and bold.

[1:37] Confirm file name will do a very similar thing, making two display formatters and some additional text where we're asking for user input. We'll pull out current title and make it green and bold.

[1:56] With cargo checking and all of our tests passing, we'll now take it for a spin. We can see that the current title is now green and it says, Some heading. Do we want a different title? Yes, we do because we want to see the other input, which we can see is now blue and also bold.

[2:14] This gives user input a different color than the program output, helping to differentiate the user's own input from what the program is asking them to do.