use click::{echo, style, Color, Command, Context, Result};
const ALL_COLORS: &[(&str, Color)] = &[
("black", Color::Black),
("red", Color::Red),
("green", Color::Green),
("yellow", Color::Yellow),
("blue", Color::Blue),
("magenta", Color::Magenta),
("cyan", Color::Cyan),
("white", Color::White),
("bright_black", Color::BrightBlack),
("bright_red", Color::BrightRed),
("bright_green", Color::BrightGreen),
("bright_yellow", Color::BrightYellow),
("bright_blue", Color::BrightBlue),
("bright_magenta", Color::BrightMagenta),
("bright_cyan", Color::BrightCyan),
("bright_white", Color::BrightWhite),
];
fn build_command() -> Command {
Command::new("colors")
.help(
"This script prints some colors. It will also automatically remove \
all ANSI styles if data is piped into a file.\n\n\
Give it a try!",
)
.callback(cli_callback)
.build()
}
fn cli_callback(_ctx: &Context) -> Result<()> {
for (name, color) in ALL_COLORS {
let text = format!("I am colored {}", name);
let styled = style(
&text,
Some(*color),
None, false, false, false, false, false, false, false, true, );
echo(&styled, true, false, None);
}
for (name, color) in ALL_COLORS {
let text = format!("I am colored {} and bold", name);
let styled = style(
&text,
Some(*color),
None, true, false, false, false, false, false, false, true, );
echo(&styled, true, false, None);
}
for (name, color) in ALL_COLORS {
let text = format!("I am reverse colored {}", name);
let styled = style(
&text,
None, Some(*color), false, false, false, false, false, false, false, true, );
echo(&styled, true, false, None);
}
let blinking = style(
"I am blinking",
None,
None,
false, false, false, false, false, true, false, true, );
echo(&blinking, true, false, None);
let underlined = style(
"I am underlined",
None,
None,
false, false, true, false, false, false, false, true, );
echo(&underlined, true, false, None);
Ok(())
}
fn main() {
let cmd = build_command();
let args: Vec<String> = std::env::args().skip(1).collect();
if let Err(e) = cmd.main(args) {
eprintln!("{}", e.format_full());
std::process::exit(e.exit_code());
}
}