minicrossterm/examples/color/mod.rs
Timon Post b717d306c3 Putted Screen behind an Option. Now when you call the functions: color, cursor, terminal, input you won't need to provide a Screen anymore.
When you want to work with the 'alternate screen' you can call the following functions: terminal::from_screen etc. Which will give you an instance to the back of the module you are calling it in.

So instead of:

let color = color(Screen::default());
let cursor = cursor(Screen::default());
let input = input(Screen::default());
let terminal = terminal(Screen::default());

You can do:

let color = color();
let cursor = cursor();
let input = input();
let terminal = terminal();
2018-11-21 07:54:16 -08:00

119 lines
5.3 KiB
Rust

//!
//! Examples of coloring the terminal.
//!
extern crate crossterm;
use self::crossterm::style::{Color, style, color};
use self::crossterm::{terminal, Screen};
/// print some red font | demonstration.
pub fn paint_foreground() {
// Create a styled object.
// Call the method `with()` on the object given by `style()` and pass in any Color from the Color enum.
let mut styledobject = style("Red foreground").with(Color::Red);
// Print the object to the given screen and.
println!("Colored text: {}", styledobject);
// Or print inline
println!("Colored text: {}", style("Red foreground").with(Color::Blue));
}
/// print some font on red background | demonstration.
pub fn paint_background() {
// Create a styled object.
// Call the method `with()` on the object given by `style()` and pass in any Color from the Color enum.
let mut styledobject = style("Red foreground").on(Color::Red);
// Print the object to the given screen and.
println!("Colored text: {}", styledobject);
// Or print inline
println!("Colored text: {}", style("Red foreground").on(Color::Blue));
}
/// Print all available foreground colors | demonstration.
pub fn print_all_foreground_colors() {
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));
#[cfg(unix)]
println!("{}", style("RGB color (10,10,10) ").with(Color::Rgb {
r: 10,
g: 10,
b: 10
}));
#[cfg(unix)]
println!("{}",style("RGB color (10,10,10) ")).with(Color::AnsiValue(50));
}
/// Print all available foreground colors | demonstration.
pub fn print_all_background_colors() {
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));
#[cfg(unix)]
println!("{}", style("RGB color (10,10,10) ").on(Color::Rgb {
r: 10,
g: 10,
b: 10
}));
#[cfg(unix)]
println!("{}",style("RGB color (10,10,10) ").on(Color::AnsiValue(50)));
}
/// 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(unix)]
pub fn print_font_with_attributes() {
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());
}
/// Print all supported RGB colors | demonstration.
#[cfg(unix)]
pub fn print_supported_colors() {
let count = color()
.get_available_color_count()
.unwrap();
for i in 0..count {
println!("{}", style(format!("White : \t {}", i)).on(Color::AnsiValue(i as u8)));
}
}