Update crossterm_style to 0.2 (#107)

This commit is contained in:
Timon 2019-03-21 16:00:30 +01:00 committed by GitHub
parent 70d6fa9b2a
commit b8e75da40a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 321 additions and 153 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "crossterm" name = "crossterm"
version = "0.6.0" version = "0.7.0"
authors = ["T. Post"] authors = ["T. Post"]
description = "An crossplatform terminal library for manipulating terminals." description = "An crossplatform terminal library for manipulating terminals."
repository = "https://github.com/TimonPost/crossterm" repository = "https://github.com/TimonPost/crossterm"
@ -35,7 +35,7 @@ members = [
crossterm_screen = { optional = true, version = "0.1.0" } crossterm_screen = { optional = true, version = "0.1.0" }
crossterm_cursor = { optional = true, version = "0.1.0" } crossterm_cursor = { optional = true, version = "0.1.0" }
crossterm_terminal = { optional = true, version = "0.1.0" } crossterm_terminal = { optional = true, version = "0.1.0" }
crossterm_style = { optional = true, version = "0.1.0" } crossterm_style = { optional = true, version = "0.2.0" }
crossterm_input = { optional = true, version = "0.1.0" } crossterm_input = { optional = true, version = "0.1.0" }
crossterm_utils = { version = "0.1.0" } crossterm_utils = { version = "0.1.0" }

View File

@ -124,45 +124,50 @@ println!("{}", crossterm.style("Black font on Green background color").with(Colo
### Styled Font | [see more](http://atcentra.com/crossterm/styling.html) ### Styled Font | [see more](http://atcentra.com/crossterm/styling.html)
This module provides the functionalities to style the terminal. This module provides the functionalities to style the terminal.
**[crossterm_style](https://github.com/TimonPost/crossterm/tree/master/crossterm_style) 0.2 has a new way to style the terminal more easily and will be usable in crossterm soon. First include those types:
If you only use the styling you might want to use that crate.**
```rust ```rust
use crossterm::{Color, style}; use crossterm::{Colored, Color, Colorize, Styler, Attribute};
```
_style font with attributes_
```rust
// pass any `Attribute` value to the formatting braces.
println!("{} Underlined {} No Underline", Attribute::Underlined, Attribute::NoUnderline);
// store objcets so it could be painted later to the screen. // you could also call different attribute methods on a `&str` and keep on chaining if needed.
let style1 = style("Some Blue font on Black background").with(Color::Blue).on(Color::Black); let styled_text = "Bold Underlined".bold().underlined();
let style2 = style("Some Red font on Yellow background").with(Color::Red).on(Color::Yellow); println!("{}", styled_text);
// syling font with (Windows 10 and UNIX systems) // old-way but still usable
let normal = style("Normal text"); let styled_text = style("Bold Underlined").bold().underlined();
let bold = style("Bold text").bold(); ```
let italic = style("Italic text").italic();
let slow_blink = style("Slow blinking text").slow_blink();
let rapid_blink = style("Rapid blinking text").rapid_blink();
let hidden = style("Hidden text").hidden();
let underlined = style("Underlined text").underlined();
let reversed = style("Reversed text").reverse();
let dimmed = style("Dim text").dim();
let crossed_out = style("Crossed out font").crossed_out();
// paint styled text to screen (this could also be called inline) _style font with colors_
println!("{}", style1); ```rust
println!("{}", style2); println!("{} Red foreground color", Colored::Fg(Color::Red));
println!("{}", bold); println!("{} Blue background color", Colored::Bg(Color::Blue));
println!("{}", hidden);
...
// cursom rgb value (Windows 10 and UNIX systems) // you can also call different coloring methods on a `&str`.
style("RGB color (10,10,10) ").with(Color::Rgb { let styled_text = "Bold Underlined".red().on_blue();
println!("{}", styled_text);
// old-way but still usable
let styled_text = style("Bold Underlined").with(Color::Red).on(Color::Blue);
```
_style font with RGB and ANSI Value_
```rust
// custom rgb value (Windows 10 and UNIX systems)
println!("{} some colored text", Colored::Fg(Color::Rgb {
r: 10, r: 10,
g: 10, g: 10,
b: 10 b: 10
})); }));
// custom ansi color value (Windows 10 and UNIX systems) // custom ansi color value (Windows 10 and UNIX systems)
style("ANSI color value (50) ").with(Color::AnsiValue(50)); println!("{} some colored text", Colored::Fg(Color::AnsiValue(10)));
``` ```
### Cursor | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/cursor.rs) ### Cursor | [see more](https://github.com/TimonPost/crossterm/blob/master/examples/cursor.rs)
This module provides the functionalities to work with the terminal cursor. This module provides the functionalities to work with the terminal cursor.

View File

@ -1,3 +1,9 @@
## Upgrade crossterm to 0.7.0
Upgrade to `crossterm_style 0.2` caused some API changes.
- Introduced more `Attributes`
- Introduced easier ways to style text [issue 87](https://github.com/TimonPost/crossterm/issues/87).
- Removed `ColorType` since it was unnecessary.
## Upgrade crossterm to 0.6.0 ## Upgrade crossterm to 0.6.0
#### Namespace refactor #### Namespace refactor
Some namespaces have been changed. All types of could be used directly by `use crossterm::*;` instead of having to go to a specific module for importing a type. Some namespaces have been changed. All types of could be used directly by `use crossterm::*;` instead of having to go to a specific module for importing a type.

View File

@ -1,238 +1,394 @@
//! //!
//! Examples of coloring the terminal. //! Examples of coloring the terminal.
//! //!
extern crate crossterm; #[macro_use]
extern crate crosstterm;
use self::crossterm::{style, Color, color}; use self::crossterm_style::{
color, style, Attribute, Color, Colored, Colorize, Styler, TerminalColor,
};
/// print some red font | demonstration. /// print some red font | demonstration.
pub fn paint_foreground() { pub fn paint_foreground() {
// Create a styled object. println!("{}", "Red foreground text: {}".red());
// Call the method `with()` on the object given by `style()` and pass in any Color from the Color enum. println!("{} Red foreground text", Colored::Fg(Color::Red));
let 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("Blue foreground").with(Color::Blue)
);
} }
/// print some font on red background | demonstration. /// print some font on red background | demonstration.
pub fn paint_background() { pub fn paint_background() {
// Create a styled object. println!("{}", "Red background text: {}".on_red());
// Call the method `with()` on the object given by `style()` and pass in any Color from the Color enum. println!("{} Red background text", Colored::Bg(Color::Red));
let 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. /// Print all available foreground colors | demonstration.
pub fn print_all_foreground_colors() { pub fn print_all_foreground_colors_with_enum() {
// we use `Reset` to restore the foreground back to normal at the end of the line.
println!( println!(
"{}", "Black : \t\t {} ■ {}\n",
style(format!("Black : \t\t {} \n", "")).with(Color::Black) Colored::Fg(Color::Black),
Attribute::Reset
); );
println!( println!(
"{}", "Red : \t\t {} ■ {}\n",
style(format!("Red : \t\t {} \n", "")).with(Color::Red) Colored::Fg(Color::Red),
Attribute::Reset
); );
println!( println!(
"{}", "DarkRed : \t\t {} ■ {}\n",
style(format!("Cyan : \t\t {} \n", "")).with(Color::Cyan) Colored::Fg(Color::DarkRed),
Attribute::Reset
); );
println!( println!(
"{}", "Cyan : \t\t {} ■ {}\n",
style(format!("DarkCyan : \t {} \n", "")).with(Color::DarkCyan) Colored::Fg(Color::Cyan),
Attribute::Reset
); );
println!( println!(
"{}", "DarkCyan : \t\t {} ■ {}\n",
style(format!("DarkRed : \t {} \n", "")).with(Color::DarkRed) Colored::Fg(Color::DarkCyan),
Attribute::Reset
); );
println!( println!(
"{}", "Green : \t\t {} ■ {}\n",
style(format!("Green : \t {} \n", "")).with(Color::Green) Colored::Fg(Color::Green),
Attribute::Reset
); );
println!( println!(
"{}", "DarkGreen : \t\t {} ■ {}\n",
style(format!("DarkGreen : \t {} \n", "")).with(Color::DarkGreen) Colored::Fg(Color::DarkGreen),
Attribute::Reset
); );
println!( println!(
"{}", "Blue : \t\t {} ■ {}\n",
style(format!("Blue : \t\t {} \n", "")).with(Color::Blue) Colored::Fg(Color::Blue),
Attribute::Reset
); );
println!( println!(
"{}", "DarkBlue : \t\t {} ■ {}\n",
style(format!("DarkBlue : \t {} \n", "")).with(Color::DarkBlue) Colored::Fg(Color::DarkBlue),
Attribute::Reset
); );
println!( println!(
"{}", "Magenta : \t\t {} ■ {}\n",
style(format!("Magenta : \t {} \n", "")).with(Color::Magenta) Colored::Fg(Color::Magenta),
Attribute::Reset
); );
println!( println!(
"{}", "DarkMagenta : \t\t{} ■ {}\n",
style(format!("DarkMagenta : \t {} \n", "")).with(Color::DarkMagenta) Colored::Fg(Color::DarkMagenta),
Attribute::Reset
); );
println!( println!(
"{}", "Yellow : \t\t {} ■ {}\n",
style(format!("Yellow : \t {} \n", "")).with(Color::Yellow) Colored::Fg(Color::Yellow),
Attribute::Reset
); );
println!( println!(
"{}", "DarkYellow : \t\t {} ■ {}\n",
style(format!("DarkYellow : \t {} \n", "")).with(Color::DarkYellow) Colored::Fg(Color::DarkYellow),
Attribute::Reset
); );
println!( println!(
"{}", "Grey : \t\t {} ■ {}\n",
style(format!("Grey : \t\t {} \n", "")).with(Color::Grey) Colored::Fg(Color::Grey),
Attribute::Reset
); );
println!( println!(
"{}", "White : \t\t {} ■ {}\n",
style(format!("White : \t {} \n", "")).with(Color::White) Colored::Fg(Color::White),
Attribute::Reset
); );
#[cfg(unix)] // custom rgb value (Windows 10 and UNIX systems)
println!( println!(
"{}", "{} some colored text",
style("RGB color (10,10,10) ").with(Color::Rgb { Colored::Fg(Color::Rgb {
r: 10, r: 10,
g: 10, g: 10,
b: 10 b: 10
}) })
); );
#[cfg(unix)] // 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!( println!(
"{}", "Black : \t\t {} {}\n",
style("RGB color (10,10,10) ").with(Color::AnsiValue(50)) "".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
);
println!(
"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
); );
} }
/// Print all available foreground colors | demonstration. /// Print all available foreground colors | demonstration.
pub fn print_all_background_colors() { pub fn print_all_background_colors_with_enum() {
println!( println!(
"{}", "Black : \t\t {} ■ {}\n",
style(format!("Black : \t {} \n", "")).on(Color::Black) Colored::Bg(Color::Black),
Attribute::Reset
); );
println!( println!(
"{}", "Red : \t\t {} ■ {}\n",
style(format!("Red : \t\t {} \n", "")).on(Color::Red) Colored::Bg(Color::Red),
Attribute::Reset
); );
println!( println!(
"{}", "DarkRed : \t\t {} ■ {}\n",
style(format!("Cyan : \t\t {} \n", "")).on(Color::Cyan) Colored::Bg(Color::DarkRed),
Attribute::Reset
); );
println!( println!(
"{}", "Cyan : \t\t {} ■ {}\n",
style(format!("DarkCyan : \t {} \n", "")).on(Color::DarkCyan) Colored::Bg(Color::Cyan),
Attribute::Reset
); );
println!( println!(
"{}", "DarkCyan : \t\t {} ■ {}\n",
style(format!("DarkRed : \t {} \n", "")).on(Color::DarkRed) Colored::Bg(Color::DarkCyan),
Attribute::Reset
); );
println!( println!(
"{}", "Green : \t\t {} ■ {}\n",
style(format!("Green : \t {} \n", "")).on(Color::Green) Colored::Bg(Color::Green),
Attribute::Reset
); );
println!( println!(
"{}", "DarkGreen : \t\t {} ■ {}\n",
style(format!("DarkGreen : \t {} \n", "")).on(Color::DarkGreen) Colored::Bg(Color::DarkGreen),
Attribute::Reset
); );
println!( println!(
"{}", "Blue : \t\t {} ■ {}\n",
style(format!("Blue : \t\t {} \n", "")).on(Color::Blue) Colored::Bg(Color::Blue),
Attribute::Reset
); );
println!( println!(
"{}", "DarkBlue : \t\t {} ■ {}\n",
style(format!("DarkBlue : \t {} \n", "")).on(Color::DarkBlue) Colored::Bg(Color::DarkBlue),
Attribute::Reset
); );
println!( println!(
"{}", "Magenta : \t\t {} ■ {}\n",
style(format!("Magenta : \t {} \n", "")).on(Color::Magenta) Colored::Bg(Color::Magenta),
Attribute::Reset
); );
println!( println!(
"{}", "DarkMagenta : \t\t{} ■ {}\n",
style(format!("DarkMagenta : \t {} \n", "")).on(Color::DarkMagenta) Colored::Bg(Color::DarkMagenta),
Attribute::Reset
); );
println!( println!(
"{}", "Yellow : \t\t {} ■ {}\n",
style(format!("Yellow : \t {} \n", "")).on(Color::Yellow) Colored::Bg(Color::Yellow),
Attribute::Reset
); );
println!( println!(
"{}", "DarkYellow : \t\t {} ■ {}\n",
style(format!("DarkYellow : \t {} \n", "")).on(Color::DarkYellow) Colored::Bg(Color::DarkYellow),
Attribute::Reset
); );
println!( println!(
"{}", "Grey : \t\t {} ■ {}\n",
style(format!("Grey : \t\t {} \n", "")).on(Color::Grey) Colored::Bg(Color::Grey),
Attribute::Reset
); );
println!( println!(
"{}", "White : \t\t {} ■ {}\n",
style(format!("White : \t {} \n", "")).on(Color::White) Colored::Bg(Color::White),
Attribute::Reset
); );
#[cfg(unix)] // custom rgb value (Windows 10 and UNIX systems)
println!( println!(
"{}", "{} some colored text",
style("RGB color (10,10,10) ").on(Color::Rgb { Colored::Bg(Color::Rgb {
r: 10, r: 80,
g: 10, g: 10,
b: 10 b: 10
}) })
); );
#[cfg(unix)] // 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() {
println!( println!(
"{}", "Black : \t\t {} {}\n",
style("RGB color (10,10,10) ").on(Color::AnsiValue(50)) "".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
); );
} }
/// Print font with all available attributes. Note that this can only be used at unix systems and that some are not supported widely | demonstration.. /// 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)] #[cfg(unix)]
pub fn print_font_with_attributes() { pub fn print_font_with_attributes() {
println!("{}", style("Normal text")); println!("{}", "Normal text");
println!("{}", style("Bold text").bold()); println!("{}", "Bold text".bold());
println!("{}", style("Italic text").italic()); println!("{}", "Italic text".italic());
println!("{}", style("Slow blinking text").slow_blink()); println!("{}", "Slow blinking text".slow_blink());
println!("{}", style("Rapid blinking text").rapid_blink()); println!("{}", "Rapid blinking text".rapid_blink());
println!("{}", style("Hidden text").hidden()); println!("{}", "Hidden text".hidden());
println!("{}", style("Underlined text").underlined()); println!("{}", "Underlined text".underlined());
println!("{}", style("Reversed text").reverse()); println!("{}", "Reversed text".reverse());
println!("{}", style("Dim text").dim()); println!("{}", "Dim text".dim());
println!("{}", style("Crossed out font").crossed_out()); println!("{}", "Crossed out font".crossed_out());
// ...
println!(
"{} Underlined {} No Underline",
Attribute::Underlined,
Attribute::NoUnderline
);
// ...
} }
/// Print font with all available attributes. Note that this can only be used at unix systems and that some are not supported widely | demonstration.. // 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)] #[cfg(windows)]
pub fn print_font_with_attributes() { pub fn print_font_with_attributes() {
println!("{}", style("Normal text")); println!("{}", "Normal text");
println!("{}", style("Bold text").bold()); println!("{}", "Bold text".bold());
println!("{}", style("Underlined text").underlined()); println!("{}", "Underlined text".underlined());
println!("{}", style("Negative text").negative()); println!("{}", "Negative text".negative());
} }
/// Print all supported RGB colors | demonstration. /// Print all supported RGB colors, not supported for Windows systems < 10 | demonstration.
#[cfg(unix)]
pub fn print_supported_colors() { pub fn print_supported_colors() {
let count = color().get_available_color_count().unwrap(); let count = color().get_available_color_count().unwrap();
for i in 0..count { for i in 0..count {
println!( println!("Test {}", Colored::Bg(Color::AnsiValue(i as u8)));
"{}",
style(format!("White : \t {}", i)).on(Color::AnsiValue(i as u8))
);
} }
} }
fn main() { fn main() {
print_all_background_colors(); print_all_background_colors_with_method();
print_all_foreground_colors(); print_all_foreground_colors_with_method();
print_font_with_attributes();
} }

View File

@ -22,7 +22,8 @@ pub use self::crossterm_input::{input, AsyncReader, KeyEvent, TerminalInput};
pub use self::crossterm_screen::{AlternateScreen, Screen}; pub use self::crossterm_screen::{AlternateScreen, Screen};
#[cfg(feature = "style")] #[cfg(feature = "style")]
pub use self::crossterm_style::{ pub use self::crossterm_style::{
color, style, Attribute, Color, ColorType, ObjectStyle, StyledObject, TerminalColor, color, style, Attribute, Color, Colored, Colorize, ObjectStyle, StyledObject, Styler,
TerminalColor,
}; };
#[cfg(feature = "terminal")] #[cfg(feature = "terminal")]
pub use self::crossterm_terminal::*; pub use self::crossterm_terminal::*;