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-01-28 07:16:14 +11:00
|
|
|
extern crate crossterm_style;
|
2018-01-18 09:06:45 +11:00
|
|
|
|
2019-01-28 07:16:14 +11:00
|
|
|
use self::crossterm_style::{color, style, Color};
|
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() {
|
2018-11-15 02:53:27 +11:00
|
|
|
// Create a styled object.
|
2018-08-12 22:51:08 +10:00
|
|
|
// Call the method `with()` on the object given by `style()` and pass in any Color from the Color enum.
|
2019-01-01 05:55:48 +11:00
|
|
|
let styledobject = style("Red foreground").with(Color::Red);
|
2018-08-12 22:51:08 +10:00
|
|
|
|
|
|
|
// Print the object to the given screen and.
|
2018-11-15 02:53:27 +11:00
|
|
|
println!("Colored text: {}", styledobject);
|
2018-08-12 22:51:08 +10:00
|
|
|
|
2018-11-15 02:53:27 +11:00
|
|
|
// Or print inline
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
|
|
|
"Colored text: {}",
|
2019-01-02 08:27:50 +11:00
|
|
|
style("Blue foreground").with(Color::Blue)
|
2018-11-22 03:48:22 +11:00
|
|
|
);
|
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() {
|
2018-11-15 02:53:27 +11:00
|
|
|
// Create a styled object.
|
|
|
|
// Call the method `with()` on the object given by `style()` and pass in any Color from the Color enum.
|
2019-01-01 05:55:48 +11:00
|
|
|
let styledobject = style("Red foreground").on(Color::Red);
|
2018-01-18 09:06:45 +11:00
|
|
|
|
2018-08-12 22:51:08 +10:00
|
|
|
// Print the object to the given screen and.
|
2018-11-15 02:53:27 +11:00
|
|
|
println!("Colored text: {}", styledobject);
|
2018-06-17 04:10:51 +10:00
|
|
|
|
2018-11-15 02:53:27 +11:00
|
|
|
// Or print inline
|
|
|
|
println!("Colored text: {}", style("Red foreground").on(Color::Blue));
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Print all available foreground colors | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn print_all_foreground_colors() {
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Black : \t\t {} \n", "■")).with(Color::Black)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Red : \t\t {} \n", "■")).with(Color::Red)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Cyan : \t\t {} \n", "■")).with(Color::Cyan)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkCyan : \t {} \n", "■")).with(Color::DarkCyan)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkRed : \t {} \n", "■")).with(Color::DarkRed)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Green : \t {} \n", "■")).with(Color::Green)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkGreen : \t {} \n", "■")).with(Color::DarkGreen)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Blue : \t\t {} \n", "■")).with(Color::Blue)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkBlue : \t {} \n", "■")).with(Color::DarkBlue)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Magenta : \t {} \n", "■")).with(Color::Magenta)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkMagenta : \t {} \n", "■")).with(Color::DarkMagenta)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Yellow : \t {} \n", "■")).with(Color::Yellow)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkYellow : \t {} \n", "■")).with(Color::DarkYellow)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Grey : \t\t {} \n", "■")).with(Color::Grey)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("White : \t {} \n", "■")).with(Color::White)
|
|
|
|
);
|
2018-08-12 22:51:08 +10:00
|
|
|
|
2019-01-28 07:16:14 +11:00
|
|
|
// supported by Unix and < Windows 10 terminals
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style("RGB color (10,10,10) ").with(Color::Rgb {
|
|
|
|
r: 10,
|
|
|
|
g: 10,
|
|
|
|
b: 10
|
|
|
|
})
|
|
|
|
);
|
2018-08-12 22:51:08 +10:00
|
|
|
|
2019-01-28 07:16:14 +11:00
|
|
|
// supported by Unix and < Windows 10 terminals
|
2019-01-01 05:55:48 +11:00
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style("RGB color (10,10,10) ").with(Color::AnsiValue(50))
|
|
|
|
);
|
2018-01-18 09:06:45 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Print all available foreground colors | demonstration.
|
2018-07-02 06:43:43 +10:00
|
|
|
pub fn print_all_background_colors() {
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Black : \t {} \n", "■")).on(Color::Black)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Red : \t\t {} \n", "■")).on(Color::Red)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Cyan : \t\t {} \n", "■")).on(Color::Cyan)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkCyan : \t {} \n", "■")).on(Color::DarkCyan)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkRed : \t {} \n", "■")).on(Color::DarkRed)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Green : \t {} \n", "■")).on(Color::Green)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkGreen : \t {} \n", "■")).on(Color::DarkGreen)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Blue : \t\t {} \n", "■")).on(Color::Blue)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkBlue : \t {} \n", "■")).on(Color::DarkBlue)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Magenta : \t {} \n", "■")).on(Color::Magenta)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkMagenta : \t {} \n", "■")).on(Color::DarkMagenta)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Yellow : \t {} \n", "■")).on(Color::Yellow)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("DarkYellow : \t {} \n", "■")).on(Color::DarkYellow)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("Grey : \t\t {} \n", "■")).on(Color::Grey)
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("White : \t {} \n", "■")).on(Color::White)
|
|
|
|
);
|
|
|
|
|
2019-01-28 07:16:14 +11:00
|
|
|
// supported by Unix and < Windows 10 terminals
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style("RGB color (10,10,10) ").on(Color::Rgb {
|
|
|
|
r: 10,
|
|
|
|
g: 10,
|
|
|
|
b: 10
|
|
|
|
})
|
|
|
|
);
|
2019-01-28 07:16:14 +11:00
|
|
|
// supported by Unix and < Windows 10 terminals
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style("RGB color (10,10,10) ").on(Color::AnsiValue(50))
|
|
|
|
);
|
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() {
|
2018-11-22 03:48:22 +11:00
|
|
|
println!("{}", style("Normal text"));
|
|
|
|
println!("{}", style("Bold text").bold());
|
|
|
|
println!("{}", style("Italic text").italic());
|
|
|
|
println!("{}", style("Slow blinking text").slow_blink());
|
|
|
|
println!("{}", style("Rapid blinking text").rapid_blink());
|
|
|
|
println!("{}", style("Hidden text").hidden());
|
|
|
|
println!("{}", style("Underlined text").underlined());
|
|
|
|
println!("{}", style("Reversed text").reverse());
|
|
|
|
println!("{}", style("Dim text").dim());
|
|
|
|
println!("{}", style("Crossed out font").crossed_out());
|
2018-01-28 04:26:08 +11:00
|
|
|
}
|
|
|
|
|
2018-12-31 10:13:45 +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..
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn print_font_with_attributes() {
|
|
|
|
println!("{}", style("Normal text"));
|
|
|
|
println!("{}", style("Bold text").bold());
|
|
|
|
println!("{}", style("Underlined text").underlined());
|
|
|
|
println!("{}", style("Negative text").negative());
|
|
|
|
}
|
|
|
|
|
2019-01-28 07:16:14 +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 {
|
2018-11-22 03:48:22 +11:00
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
style(format!("White : \t {}", i)).on(Color::AnsiValue(i as u8))
|
|
|
|
);
|
2018-01-28 04:26:08 +11:00
|
|
|
}
|
2018-07-02 06:43:43 +10:00
|
|
|
}
|
2019-01-28 07:16:14 +11:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
print_all_background_colors();
|
|
|
|
print_all_foreground_colors();
|
|
|
|
print_font_with_attributes();
|
|
|
|
}
|