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")?;