From b8e75da40a81deae37f0d62e64b83550f1b1c015 Mon Sep 17 00:00:00 2001 From: Timon Date: Thu, 21 Mar 2019 16:00:30 +0100 Subject: [PATCH] Update crossterm_style to 0.2 (#107) --- Cargo.toml | 4 +- README.md | 61 +++---- docs/UPGRADE.md | 6 + examples/style.rs | 400 ++++++++++++++++++++++++++++++++-------------- src/lib.rs | 3 +- 5 files changed, 321 insertions(+), 153 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0cf67d04..1bbeb4db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "crossterm" -version = "0.6.0" +version = "0.7.0" authors = ["T. Post"] description = "An crossplatform terminal library for manipulating terminals." repository = "https://github.com/TimonPost/crossterm" @@ -35,7 +35,7 @@ members = [ crossterm_screen = { optional = true, version = "0.1.0" } crossterm_cursor = { 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_utils = { version = "0.1.0" } diff --git a/README.md b/README.md index 5a325b51..115e70b4 100644 --- a/README.md +++ b/README.md @@ -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) 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. -If you only use the styling you might want to use that crate.** -```rust -use crossterm::{Color, style}; +First include those types: -// store objcets so it could be painted later to the screen. -let style1 = style("Some Blue font on Black background").with(Color::Blue).on(Color::Black); -let style2 = style("Some Red font on Yellow background").with(Color::Red).on(Color::Yellow); +```rust +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); -// syling font with (Windows 10 and UNIX systems) -let normal = style("Normal text"); -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(); +// you could also call different attribute methods on a `&str` and keep on chaining if needed. +let styled_text = "Bold Underlined".bold().underlined(); +println!("{}", styled_text); -// paint styled text to screen (this could also be called inline) -println!("{}", style1); -println!("{}", style2); -println!("{}", bold); -println!("{}", hidden); -... +// old-way but still usable +let styled_text = style("Bold Underlined").bold().underlined(); +``` -// cursom rgb value (Windows 10 and UNIX systems) -style("RGB color (10,10,10) ").with(Color::Rgb { +_style font with colors_ +```rust +println!("{} Red foreground color", Colored::Fg(Color::Red)); +println!("{} Blue background color", Colored::Bg(Color::Blue)); + +// you can also call different coloring methods on a `&str`. +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, g: 10, b: 10 })); // 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) This module provides the functionalities to work with the terminal cursor. diff --git a/docs/UPGRADE.md b/docs/UPGRADE.md index a8699bc9..01460698 100644 --- a/docs/UPGRADE.md +++ b/docs/UPGRADE.md @@ -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 #### 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. diff --git a/examples/style.rs b/examples/style.rs index 78bc3a77..21958b8c 100644 --- a/examples/style.rs +++ b/examples/style.rs @@ -1,238 +1,394 @@ //! //! 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. 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 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) - ); + println!("{}", "Red foreground text: {}".red()); + println!("{} Red foreground text", Colored::Fg(Color::Red)); } /// 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 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)); + println!("{}", "Red background text: {}".on_red()); + println!("{} Red background text", Colored::Bg(Color::Red)); } /// 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!( - "{}", - style(format!("Black : \t\t {} \n", "■")).with(Color::Black) + "Black : \t\t {} ■ {}\n", + Colored::Fg(Color::Black), + Attribute::Reset ); println!( - "{}", - style(format!("Red : \t\t {} \n", "■")).with(Color::Red) + "Red : \t\t {} ■ {}\n", + Colored::Fg(Color::Red), + Attribute::Reset ); println!( - "{}", - style(format!("Cyan : \t\t {} \n", "■")).with(Color::Cyan) + "DarkRed : \t\t {} ■ {}\n", + Colored::Fg(Color::DarkRed), + Attribute::Reset ); println!( - "{}", - style(format!("DarkCyan : \t {} \n", "■")).with(Color::DarkCyan) + "Cyan : \t\t {} ■ {}\n", + Colored::Fg(Color::Cyan), + Attribute::Reset ); println!( - "{}", - style(format!("DarkRed : \t {} \n", "■")).with(Color::DarkRed) + "DarkCyan : \t\t {} ■ {}\n", + Colored::Fg(Color::DarkCyan), + Attribute::Reset ); println!( - "{}", - style(format!("Green : \t {} \n", "■")).with(Color::Green) + "Green : \t\t {} ■ {}\n", + Colored::Fg(Color::Green), + Attribute::Reset ); println!( - "{}", - style(format!("DarkGreen : \t {} \n", "■")).with(Color::DarkGreen) + "DarkGreen : \t\t {} ■ {}\n", + Colored::Fg(Color::DarkGreen), + Attribute::Reset ); println!( - "{}", - style(format!("Blue : \t\t {} \n", "■")).with(Color::Blue) + "Blue : \t\t {} ■ {}\n", + Colored::Fg(Color::Blue), + Attribute::Reset ); println!( - "{}", - style(format!("DarkBlue : \t {} \n", "■")).with(Color::DarkBlue) + "DarkBlue : \t\t {} ■ {}\n", + Colored::Fg(Color::DarkBlue), + Attribute::Reset ); println!( - "{}", - style(format!("Magenta : \t {} \n", "■")).with(Color::Magenta) + "Magenta : \t\t {} ■ {}\n", + Colored::Fg(Color::Magenta), + Attribute::Reset ); println!( - "{}", - style(format!("DarkMagenta : \t {} \n", "■")).with(Color::DarkMagenta) + "DarkMagenta : \t\t{} ■ {}\n", + Colored::Fg(Color::DarkMagenta), + Attribute::Reset ); println!( - "{}", - style(format!("Yellow : \t {} \n", "■")).with(Color::Yellow) + "Yellow : \t\t {} ■ {}\n", + Colored::Fg(Color::Yellow), + Attribute::Reset ); println!( - "{}", - style(format!("DarkYellow : \t {} \n", "■")).with(Color::DarkYellow) + "DarkYellow : \t\t {} ■ {}\n", + Colored::Fg(Color::DarkYellow), + Attribute::Reset ); println!( - "{}", - style(format!("Grey : \t\t {} \n", "■")).with(Color::Grey) + "Grey : \t\t {} ■ {}\n", + Colored::Fg(Color::Grey), + Attribute::Reset ); println!( - "{}", - style(format!("White : \t {} \n", "■")).with(Color::White) + "White : \t\t {} ■ {}\n", + Colored::Fg(Color::White), + Attribute::Reset ); - #[cfg(unix)] + // custom rgb value (Windows 10 and UNIX systems) println!( - "{}", - style("RGB color (10,10,10) ").with(Color::Rgb { + "{} some colored text", + Colored::Fg(Color::Rgb { r: 10, g: 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!( - "{}", - style("RGB color (10,10,10) ").with(Color::AnsiValue(50)) + "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 + ); + 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. -pub fn print_all_background_colors() { +pub fn print_all_background_colors_with_enum() { println!( - "{}", - style(format!("Black : \t {} \n", "■")).on(Color::Black) + "Black : \t\t {} ■ {}\n", + Colored::Bg(Color::Black), + Attribute::Reset ); println!( - "{}", - style(format!("Red : \t\t {} \n", "■")).on(Color::Red) + "Red : \t\t {} ■ {}\n", + Colored::Bg(Color::Red), + Attribute::Reset ); println!( - "{}", - style(format!("Cyan : \t\t {} \n", "■")).on(Color::Cyan) + "DarkRed : \t\t {} ■ {}\n", + Colored::Bg(Color::DarkRed), + Attribute::Reset ); println!( - "{}", - style(format!("DarkCyan : \t {} \n", "■")).on(Color::DarkCyan) + "Cyan : \t\t {} ■ {}\n", + Colored::Bg(Color::Cyan), + Attribute::Reset ); println!( - "{}", - style(format!("DarkRed : \t {} \n", "■")).on(Color::DarkRed) + "DarkCyan : \t\t {} ■ {}\n", + Colored::Bg(Color::DarkCyan), + Attribute::Reset ); println!( - "{}", - style(format!("Green : \t {} \n", "■")).on(Color::Green) + "Green : \t\t {} ■ {}\n", + Colored::Bg(Color::Green), + Attribute::Reset ); println!( - "{}", - style(format!("DarkGreen : \t {} \n", "■")).on(Color::DarkGreen) + "DarkGreen : \t\t {} ■ {}\n", + Colored::Bg(Color::DarkGreen), + Attribute::Reset ); println!( - "{}", - style(format!("Blue : \t\t {} \n", "■")).on(Color::Blue) + "Blue : \t\t {} ■ {}\n", + Colored::Bg(Color::Blue), + Attribute::Reset ); println!( - "{}", - style(format!("DarkBlue : \t {} \n", "■")).on(Color::DarkBlue) + "DarkBlue : \t\t {} ■ {}\n", + Colored::Bg(Color::DarkBlue), + Attribute::Reset ); println!( - "{}", - style(format!("Magenta : \t {} \n", "■")).on(Color::Magenta) + "Magenta : \t\t {} ■ {}\n", + Colored::Bg(Color::Magenta), + Attribute::Reset ); println!( - "{}", - style(format!("DarkMagenta : \t {} \n", "■")).on(Color::DarkMagenta) + "DarkMagenta : \t\t{} ■ {}\n", + Colored::Bg(Color::DarkMagenta), + Attribute::Reset ); println!( - "{}", - style(format!("Yellow : \t {} \n", "■")).on(Color::Yellow) + "Yellow : \t\t {} ■ {}\n", + Colored::Bg(Color::Yellow), + Attribute::Reset ); println!( - "{}", - style(format!("DarkYellow : \t {} \n", "■")).on(Color::DarkYellow) + "DarkYellow : \t\t {} ■ {}\n", + Colored::Bg(Color::DarkYellow), + Attribute::Reset ); println!( - "{}", - style(format!("Grey : \t\t {} \n", "■")).on(Color::Grey) + "Grey : \t\t {} ■ {}\n", + Colored::Bg(Color::Grey), + Attribute::Reset ); println!( - "{}", - style(format!("White : \t {} \n", "■")).on(Color::White) + "White : \t\t {} ■ {}\n", + Colored::Bg(Color::White), + Attribute::Reset ); - #[cfg(unix)] + // custom rgb value (Windows 10 and UNIX systems) println!( - "{}", - style("RGB color (10,10,10) ").on(Color::Rgb { - r: 10, + "{} some colored text", + Colored::Bg(Color::Rgb { + r: 80, g: 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!( - "{}", - style("RGB color (10,10,10) ").on(Color::AnsiValue(50)) + "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 ); } /// 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()); + 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 + ); + // ... } -/// 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)] 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()); + println!("{}", "Normal text"); + println!("{}", "Bold text".bold()); + println!("{}", "Underlined text".underlined()); + println!("{}", "Negative text".negative()); } -/// Print all supported RGB colors | demonstration. -#[cfg(unix)] +/// Print all supported RGB colors, not supported for Windows systems < 10 | demonstration. 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)) - ); + println!("Test {}", Colored::Bg(Color::AnsiValue(i as u8))); } } fn main() { - print_all_background_colors(); - print_all_foreground_colors(); - print_font_with_attributes(); + print_all_background_colors_with_method(); + print_all_foreground_colors_with_method(); } diff --git a/src/lib.rs b/src/lib.rs index 4414d7d6..f6326978 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,8 @@ pub use self::crossterm_input::{input, AsyncReader, KeyEvent, TerminalInput}; pub use self::crossterm_screen::{AlternateScreen, Screen}; #[cfg(feature = "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")] pub use self::crossterm_terminal::*;