2018-07-02 06:43:43 +10:00
|
|
|
//!
|
2018-07-02 06:40:07 +10:00
|
|
|
//! Examples of coloring the terminal.
|
2018-01-18 09:06:45 +11:00
|
|
|
//!
|
2019-03-22 02:00:30 +11:00
|
|
|
#[macro_use]
|
|
|
|
extern crate crosstterm;
|
2018-01-18 09:06:45 +11:00
|
|
|
|
2019-03-22 02:00:30 +11:00
|
|
|
use self::crossterm_style::{
|
|
|
|
color, style, Attribute, Color, Colored, Colorize, Styler, TerminalColor,
|
|
|
|
};
|
2018-01-18 09:06:45 +11:00
|
|
|
|
|
|
|
/// print some red font | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn paint_foreground() {
|
2019-03-22 02:00:30 +11:00
|
|
|
println!("{}", "Red foreground text: {}".red());
|
|
|
|
println!("{} Red foreground text", Colored::Fg(Color::Red));
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// print some font on red background | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn paint_background() {
|
2019-03-22 02:00:30 +11:00
|
|
|
println!("{}", "Red background text: {}".on_red());
|
|
|
|
println!("{} Red background text", Colored::Bg(Color::Red));
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Print all available foreground colors | demonstration.
|
2019-03-22 02:00:30 +11:00
|
|
|
pub fn print_all_foreground_colors_with_enum() {
|
|
|
|
// we use `Reset` to restore the foreground back to normal at the end of the line.
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Black : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::Black),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Red : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::Red),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkRed : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::DarkRed),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Cyan : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::Cyan),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkCyan : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::DarkCyan),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Green : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::Green),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkGreen : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::DarkGreen),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Blue : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::Blue),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkBlue : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::DarkBlue),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Magenta : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::Magenta),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkMagenta : \t\t{} ■ {}\n",
|
|
|
|
Colored::Fg(Color::DarkMagenta),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Yellow : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::Yellow),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkYellow : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::DarkYellow),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Grey : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::Grey),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"White : \t\t {} ■ {}\n",
|
|
|
|
Colored::Fg(Color::White),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
2018-08-12 22:51:08 +10:00
|
|
|
|
2019-03-22 02:00:30 +11:00
|
|
|
// custom rgb value (Windows 10 and UNIX systems)
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"{} some colored text",
|
|
|
|
Colored::Fg(Color::Rgb {
|
2018-11-22 03:48:22 +11:00
|
|
|
r: 10,
|
|
|
|
g: 10,
|
|
|
|
b: 10
|
|
|
|
})
|
|
|
|
);
|
2018-08-12 22:51:08 +10:00
|
|
|
|
2019-03-22 02:00:30 +11:00
|
|
|
// custom ansi color value (Windows 10 and UNIX systems)
|
|
|
|
println!("{} some colored text", Colored::Fg(Color::AnsiValue(10)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print all available foreground colors | demonstration.
|
|
|
|
pub fn print_all_foreground_colors_with_method() {
|
|
|
|
println!(
|
|
|
|
"Black : \t\t {} {}\n",
|
|
|
|
"■".black(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!("Red : \t\t {} {}\n", "■".red(), Attribute::Reset);
|
|
|
|
println!(
|
|
|
|
"DarkRed : \t\t {} {}\n",
|
|
|
|
"■".dark_red(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!("Cyan : \t\t {} {}\n", "■".cyan(), Attribute::Reset);
|
|
|
|
println!(
|
|
|
|
"DarkCyan : \t\t {} {}\n",
|
|
|
|
"■".dark_cyan(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Green : \t\t {} {}\n",
|
|
|
|
"■".green(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
2019-01-01 05:55:48 +11:00
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkGreen : \t\t {} {}\n",
|
|
|
|
"■".dark_green(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!("Blue : \t\t {} {}\n", "■".blue(), Attribute::Reset);
|
|
|
|
println!(
|
|
|
|
"DarkBlue : \t\t {} {}\n",
|
|
|
|
"■".dark_blue(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Magenta : \t\t {} {}\n",
|
|
|
|
"■".magenta(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"DarkMagenta : \t\t {} {}\n",
|
|
|
|
"■".dark_magenta(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Yellow : \t\t {} {}\n",
|
|
|
|
"■".yellow(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"DarkYellow : \t\t {} {}\n",
|
|
|
|
"■".dark_yellow(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!("Grey : \t\t {} {}\n", "■".grey(), Attribute::Reset);
|
|
|
|
println!(
|
|
|
|
"White : \t\t {} {}\n",
|
|
|
|
"■".white(),
|
|
|
|
Attribute::Reset
|
2019-01-01 05:55:48 +11:00
|
|
|
);
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Print all available foreground colors | demonstration.
|
2019-03-22 02:00:30 +11:00
|
|
|
pub fn print_all_background_colors_with_enum() {
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Black : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::Black),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Red : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::Red),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkRed : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::DarkRed),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Cyan : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::Cyan),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkCyan : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::DarkCyan),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Green : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::Green),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkGreen : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::DarkGreen),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Blue : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::Blue),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkBlue : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::DarkBlue),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Magenta : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::Magenta),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkMagenta : \t\t{} ■ {}\n",
|
|
|
|
Colored::Bg(Color::DarkMagenta),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Yellow : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::Yellow),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"DarkYellow : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::DarkYellow),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Grey : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::Grey),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"White : \t\t {} ■ {}\n",
|
|
|
|
Colored::Bg(Color::White),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
|
|
|
|
2019-03-22 02:00:30 +11:00
|
|
|
// custom rgb value (Windows 10 and UNIX systems)
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"{} some colored text",
|
|
|
|
Colored::Bg(Color::Rgb {
|
|
|
|
r: 80,
|
2018-11-22 03:48:22 +11:00
|
|
|
g: 10,
|
|
|
|
b: 10
|
|
|
|
})
|
|
|
|
);
|
2018-08-12 22:51:08 +10:00
|
|
|
|
2019-03-22 02:00:30 +11:00
|
|
|
// custom ansi color value (Windows 10 and UNIX systems)
|
|
|
|
println!("{} some colored text", Colored::Bg(Color::AnsiValue(10)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print all available foreground colors | demonstration.
|
|
|
|
pub fn print_all_background_colors_with_method() {
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
2019-03-22 02:00:30 +11:00
|
|
|
"Black : \t\t {} {}\n",
|
|
|
|
"■".on_black(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Red : \t\t {} {}\n",
|
|
|
|
"■".on_red(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"DarkRed : \t\t {} {}\n",
|
|
|
|
"■".on_dark_red(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Cyan : \t\t {} {}\n",
|
|
|
|
"■".on_cyan(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"DarkCyan : \t\t {} {}\n",
|
|
|
|
"■".on_dark_cyan(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Green : \t\t {} {}\n",
|
|
|
|
"■".on_green(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"DarkGreen : \t\t {} {}\n",
|
|
|
|
"■".on_dark_green(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Blue : \t\t {} {}\n",
|
|
|
|
"■".on_blue(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"DarkBlue : \t\t {} {}\n",
|
|
|
|
"■".on_dark_blue(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Magenta : \t\t {} {}\n",
|
|
|
|
"■".on_magenta(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"DarkMagenta : \t\t {} {}\n",
|
|
|
|
"■".on_dark_magenta(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Yellow : \t\t {} {}\n",
|
|
|
|
"■".on_yellow(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"DarkYellow : \t\t {} {}\n",
|
|
|
|
"■".on_dark_yellow(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Grey : \t\t {} {}\n",
|
|
|
|
"■".on_grey(),
|
|
|
|
Attribute::Reset
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"White : \t\t {} {}\n",
|
|
|
|
"■".on_white(),
|
|
|
|
Attribute::Reset
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
2018-01-28 04:26:08 +11:00
|
|
|
}
|
|
|
|
|
2018-01-31 07:20:11 +11:00
|
|
|
/// Print font with all available attributes. Note that this can only be used at unix systems and that some are not supported widely | demonstration..
|
2018-01-28 04:26:08 +11:00
|
|
|
#[cfg(unix)]
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn print_font_with_attributes() {
|
2019-03-22 02:00:30 +11:00
|
|
|
println!("{}", "Normal text");
|
|
|
|
println!("{}", "Bold text".bold());
|
|
|
|
println!("{}", "Italic text".italic());
|
|
|
|
println!("{}", "Slow blinking text".slow_blink());
|
|
|
|
println!("{}", "Rapid blinking text".rapid_blink());
|
|
|
|
println!("{}", "Hidden text".hidden());
|
|
|
|
println!("{}", "Underlined text".underlined());
|
|
|
|
println!("{}", "Reversed text".reverse());
|
|
|
|
println!("{}", "Dim text".dim());
|
|
|
|
println!("{}", "Crossed out font".crossed_out());
|
|
|
|
// ...
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"{} Underlined {} No Underline",
|
|
|
|
Attribute::Underlined,
|
|
|
|
Attribute::NoUnderline
|
|
|
|
);
|
|
|
|
// ...
|
2018-01-28 04:26:08 +11:00
|
|
|
}
|
|
|
|
|
2019-03-22 02:00:30 +11:00
|
|
|
// Print font with all available attributes. Note that this can only be used at unix systems and that some are not supported widely | demonstration..
|
2018-12-31 10:13:45 +11:00
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn print_font_with_attributes() {
|
2019-03-22 02:00:30 +11:00
|
|
|
println!("{}", "Normal text");
|
|
|
|
println!("{}", "Bold text".bold());
|
|
|
|
println!("{}", "Underlined text".underlined());
|
|
|
|
println!("{}", "Negative text".negative());
|
2018-12-31 10:13:45 +11:00
|
|
|
}
|
|
|
|
|
2019-03-22 02:00:30 +11:00
|
|
|
/// Print all supported RGB colors, not supported for Windows systems < 10 | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn print_supported_colors() {
|
2018-11-22 03:48:22 +11:00
|
|
|
let count = color().get_available_color_count().unwrap();
|
2018-07-02 06:43:43 +10:00
|
|
|
|
|
|
|
for i in 0..count {
|
2019-03-22 02:00:30 +11:00
|
|
|
println!("Test {}", Colored::Bg(Color::AnsiValue(i as u8)));
|
2018-01-28 04:26:08 +11:00
|
|
|
}
|
2018-07-02 06:43:43 +10:00
|
|
|
}
|
2019-03-17 19:31:39 +11:00
|
|
|
|
|
|
|
fn main() {
|
2019-03-22 02:00:30 +11:00
|
|
|
print_all_background_colors_with_method();
|
|
|
|
print_all_foreground_colors_with_method();
|
2019-03-17 19:31:39 +11:00
|
|
|
}
|