We’re asking the user for input interactively now, but the text we’re using to do that doesn’t look any different than regular output, which can make it hard to understand what is program-provided text and what the user has typed.
We can solve this by stylizing the text with colors and font weights.
❯ cargo add owo-colors
Updating crates.io index
Adding owo-colors v3.5.0 to dependencies.
Features:
- alloc
- supports-color
- supports-colors
Updating crates.io index
owo-colors
is a crate that offers the OwoColorize
trait, which is an extension trait that enables coloring formatters.
An extension trait is a trait that is written to extend the behavior of existing types. For example, the OwoColorize
trait defines a number of functions for foreground and background colors that we can use, and implements those functions for &str
, for example.
We need to bring the trait into scope to access the functions it provides.
use owo_colors::OwoColorize;
The new functions appear right on the strings we’re passing in to the format!
macro. We’ll start off by using .bold()
and .green()
on a few of the segments of the confirm_filename
string.
let result = rprompt::prompt_reply(&format!(
"current title: {}
Do you want a different title? (y/{}): ",
&raw_title.bold().green(),
"N".bold(),
))?;
Similarly we can update ask_for_filename
. This time I’ll use .blue
.
fn ask_for_filename() -> io::Result<String> {
rprompt::prompt_reply(
"Enter filename
> "
.blue()
.bold(),
)
}
There are a whole bunch of different functions we can use to stylize terminal text using owo-colors
.
other colors for background colors, foreground colors, underlines and more.
owo-colors also has support for conditionally enabling colors… which we’ll use when we get to testing.