Improved Styling (#92)
This commit is contained in:
parent
8ffdb00b10
commit
c37a480f78
12
Cargo.toml
12
Cargo.toml
@ -32,12 +32,12 @@ members = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
crossterm_screen = { path = "./crossterm_screen", optional = true, version = "0.1.0" }
|
crossterm_screen = { optional = true, version = "0.1.0" }
|
||||||
crossterm_cursor = { path = "./crossterm_cursor", optional = true, version = "0.1.0" }
|
crossterm_cursor = { optional = true, version = "0.1.0" }
|
||||||
crossterm_terminal = { path = "./crossterm_terminal", optional = true, version = "0.1.0" }
|
crossterm_terminal = { optional = true, version = "0.1.0" }
|
||||||
crossterm_style = { path = "./crossterm_style", optional = true, version = "0.1.0" }
|
crossterm_style = { optional = true, version = "0.1.0" }
|
||||||
crossterm_input = { path = "./crossterm_input", optional = true, version = "0.1.0" }
|
crossterm_input = { optional = true, version = "0.1.0" }
|
||||||
crossterm_utils = { path = "./crossterm_utils", version = "0.1.0" }
|
crossterm_utils = { version = "0.1.0" }
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "crossterm"
|
name = "crossterm"
|
||||||
|
@ -123,6 +123,9 @@ 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.
|
||||||
|
If you only use the styling you might want to use that crate.**
|
||||||
```rust
|
```rust
|
||||||
use crossterm::{Color, style};
|
use crossterm::{Color, style};
|
||||||
|
|
||||||
@ -258,7 +261,7 @@ If you have used this library for a terminal other than the above list without i
|
|||||||
|
|
||||||
## Notice
|
## Notice
|
||||||
This library is average stable now but I don't expect it to not to change that much.
|
This library is average stable now but I don't expect it to not to change that much.
|
||||||
If there are any changes that will affect previous versions I will [describe](https://github.com/TimonPost/crossterm/blob/master/docs/UpgradeManual.md) what to change to upgrade.
|
If there are any changes that will affect previous versions I will [describe](https://github.com/TimonPost/crossterm/blob/master/docs/UPGRADE.md) what to change to upgrade.
|
||||||
|
|
||||||
## Todo
|
## Todo
|
||||||
I still have some things in mind to implement.
|
I still have some things in mind to implement.
|
||||||
|
@ -13,10 +13,10 @@ edition = "2018"
|
|||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = { version = "0.3.5", features = ["wincon","winnt","minwindef"] }
|
winapi = { version = "0.3.5", features = ["wincon","winnt","minwindef"] }
|
||||||
crossterm_winapi = { path = "../crossterm_winapi" }
|
crossterm_winapi = "0.1.1"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
crossterm_utils = { path = "../crossterm_utils" }
|
crossterm_utils = "0.1.0"
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "cursor"
|
name = "cursor"
|
||||||
|
@ -13,13 +13,13 @@ edition = "2018"
|
|||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = { version = "0.3.5", features = ["winnt"] }
|
winapi = { version = "0.3.5", features = ["winnt"] }
|
||||||
crossterm_winapi = { path = "../crossterm_winapi" }
|
crossterm_winapi = "0.1.1"
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
libc = "0.2.43"
|
libc = "0.2.43"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
crossterm_utils = { path = "../crossterm_utils" }
|
crossterm_utils = "0.1.0"
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "input"
|
name = "input"
|
||||||
|
@ -12,8 +12,8 @@ readme = "README.md"
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
crossterm_utils = { path = "../crossterm_utils" }
|
crossterm_utils = "0.1.0"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = { version = "0.3.5", features = ["minwindef", "wincon"] }
|
winapi = { version = "0.3.5", features = ["minwindef", "wincon"] }
|
||||||
crossterm_winapi = { path = "../crossterm_winapi" }
|
crossterm_winapi = "0.1.1"
|
7
crossterm_style/CHANGELOG.md
Normal file
7
crossterm_style/CHANGELOG.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# Changes crossterm_style 0.2
|
||||||
|
- Introduced more `Attributes`
|
||||||
|
- Introduced easier ways to style text [issue 87](https://github.com/TimonPost/crossterm/issues/87).
|
||||||
|
- Removed `ColorType` since it was unnecessary.
|
||||||
|
|
||||||
|
# Changes crossterm_style 0.1
|
||||||
|
- Moved out of `crossterm` 5.4 crate.
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "crossterm_style"
|
name = "crossterm_style"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
authors = ["T. Post"]
|
authors = ["T. Post"]
|
||||||
description = "A cross-platform library styling the terminal output."
|
description = "A cross-platform library styling the terminal output."
|
||||||
repository = "https://github.com/TimonPost/crossterm"
|
repository = "https://github.com/TimonPost/crossterm"
|
||||||
@ -13,10 +13,10 @@ edition = "2018"
|
|||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = { version = "0.3.5", features = ["wincon"] }
|
winapi = { version = "0.3.5", features = ["wincon"] }
|
||||||
crossterm_winapi = { path = "../crossterm_winapi" }
|
crossterm_winapi = "0.1.1"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
crossterm_utils = { path = "../crossterm_utils" }
|
crossterm_utils = "0.1.0"
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "style"
|
name = "style"
|
||||||
|
@ -80,46 +80,50 @@ These are the features of this crate:
|
|||||||
- RGB support (Windows 10 and UNIX only)
|
- RGB support (Windows 10 and UNIX only)
|
||||||
- Text Attributes like: bold, italic, underscore and crossed word ect (Windows 10 and UNIX only)
|
- Text Attributes like: bold, italic, underscore and crossed word ect (Windows 10 and UNIX only)
|
||||||
|
|
||||||
Planned features:
|
|
||||||
- Easier usage; e.g. `println!("{}Bold{}Blue", Attribute::Bold, Color::Blue)`
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
Check out the [examples](/examples/) for more information about how to use this crate.
|
Check out the [examples](/examples/) for more information about how to use this crate.
|
||||||
|
|
||||||
|
_style font with attributes_
|
||||||
```rust
|
```rust
|
||||||
use crossterm::style::{Color, style};
|
use crossterm_style::{Colored, Color, Colorize, Styler, Attribute};
|
||||||
|
|
||||||
// store objcets so it could be painted later to the screen.
|
// pass any `Attribute` value to the formatting braces.
|
||||||
let style1 = style("Some Blue font on Black background").with(Color::Blue).on(Color::Black);
|
println!("{} Underlined {} No Underline", Attribute::Underlined, Attribute::NoUnderline);
|
||||||
let style2 = style("Some Red font on Yellow background").with(Color::Red).on(Color::Yellow);
|
|
||||||
|
|
||||||
// syling font with (Windows 10 and UNIX systems)
|
// you could also call different attribute methods on a `&str` and keep on chaining if needed.
|
||||||
let normal = style("Normal text");
|
let styled_text = "Bold Underlined".bold().underlined();
|
||||||
let bold = style("Bold text").bold();
|
println!("{}", styled_text);
|
||||||
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)
|
// old-way but still usable
|
||||||
println!("{}", style1);
|
let styled_text = style("Bold Underlined").bold().underlined();
|
||||||
println!("{}", style2);
|
```
|
||||||
println!("{}", bold);
|
|
||||||
println!("{}", hidden);
|
|
||||||
...
|
|
||||||
|
|
||||||
// cursom rgb value (Windows 10 and UNIX systems)
|
_style font with colors_
|
||||||
style("RGB color (10,10,10) ").with(Color::Rgb {
|
```rust
|
||||||
|
use crossterm_style::{Colored, Color, Colorize};
|
||||||
|
|
||||||
|
|
||||||
|
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,
|
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)));
|
||||||
|
|
||||||
```
|
```
|
||||||
## Tested terminals
|
## Tested terminals
|
||||||
|
@ -1,220 +1,382 @@
|
|||||||
//!
|
//!
|
||||||
//! Examples of coloring the terminal.
|
//! Examples of coloring the terminal.
|
||||||
//!
|
//!
|
||||||
|
#[macro_use]
|
||||||
extern crate crossterm_style;
|
extern crate crossterm_style;
|
||||||
|
|
||||||
use self::crossterm_style::{color, style, 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
|
||||||
);
|
);
|
||||||
|
|
||||||
// supported by Unix and < Windows 10 terminals
|
// 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
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// supported by Unix and < Windows 10 terminals
|
// 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
|
||||||
);
|
);
|
||||||
|
|
||||||
// supported by Unix and < Windows 10 terminals
|
// 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
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
// supported by Unix and < Windows 10 terminals
|
|
||||||
|
// 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, not supported for Windows systems < 10 | demonstration.
|
/// Print all supported RGB colors, not supported for Windows systems < 10 | demonstration.
|
||||||
@ -222,15 +384,6 @@ 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() {
|
|
||||||
print_all_background_colors();
|
|
||||||
print_all_foreground_colors();
|
|
||||||
print_font_with_attributes();
|
|
||||||
}
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
//! This is a ANSI specific implementation for styling related action.
|
//! This is a ANSI specific implementation for styling related action.
|
||||||
//! This module is used for Windows 10 terminals and Unix terminals by default.
|
//! This module is used for Windows 10 terminals and Unix terminals by default.
|
||||||
|
|
||||||
use crate::{Color, ColorType, ITerminalColor};
|
use crate::{Color, ITerminalColor};
|
||||||
use crossterm_utils::{write, write_str, Result, TerminalOutput};
|
use crossterm_utils::{write, write_str, Result, TerminalOutput};
|
||||||
|
|
||||||
|
use crate::Colored;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
/// This struct is an ANSI escape code implementation for color related actions.
|
/// This struct is an ANSI escape code implementation for color related actions.
|
||||||
@ -19,10 +20,7 @@ impl ITerminalColor for AnsiColor {
|
|||||||
fn set_fg(&self, fg_color: Color, stdout: &Option<&Arc<TerminalOutput>>) -> Result<()> {
|
fn set_fg(&self, fg_color: Color, stdout: &Option<&Arc<TerminalOutput>>) -> Result<()> {
|
||||||
write(
|
write(
|
||||||
stdout,
|
stdout,
|
||||||
format!(
|
format!(csi!("{}m"), self.color_value(Colored::Fg(fg_color))),
|
||||||
csi!("{}m"),
|
|
||||||
self.color_value(fg_color, ColorType::Foreground)
|
|
||||||
),
|
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -30,10 +28,7 @@ impl ITerminalColor for AnsiColor {
|
|||||||
fn set_bg(&self, bg_color: Color, stdout: &Option<&Arc<TerminalOutput>>) -> Result<()> {
|
fn set_bg(&self, bg_color: Color, stdout: &Option<&Arc<TerminalOutput>>) -> Result<()> {
|
||||||
write(
|
write(
|
||||||
stdout,
|
stdout,
|
||||||
format!(
|
format!(csi!("{}m"), self.color_value(Colored::Bg(bg_color))),
|
||||||
csi!("{}m"),
|
|
||||||
self.color_value(bg_color, ColorType::Background)
|
|
||||||
),
|
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -43,12 +38,20 @@ impl ITerminalColor for AnsiColor {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn color_value(&self, color: Color, color_type: ColorType) -> String {
|
fn color_value(&self, colored: Colored) -> String {
|
||||||
let mut ansi_value = String::new();
|
let mut ansi_value = String::new();
|
||||||
|
|
||||||
match color_type {
|
let mut color = Color::White;
|
||||||
ColorType::Foreground => ansi_value.push_str("38;"),
|
|
||||||
ColorType::Background => ansi_value.push_str("48;"),
|
match colored {
|
||||||
|
Colored::Fg(new_color) => {
|
||||||
|
ansi_value.push_str("38;");
|
||||||
|
color = new_color;
|
||||||
|
}
|
||||||
|
Colored::Bg(new_color) => {
|
||||||
|
ansi_value.push_str("48;");
|
||||||
|
color = new_color;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let rgb_val: String;
|
let rgb_val: String;
|
||||||
|
@ -102,9 +102,9 @@ impl<'stdout> TerminalColor<'stdout> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get available color count.
|
/// Get available color count.
|
||||||
|
/// (This does not always provide a good result.)
|
||||||
pub fn get_available_color_count(&self) -> io::Result<u16> {
|
pub fn get_available_color_count(&self) -> io::Result<u16> {
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
Ok(match env::var_os("TERM") {
|
Ok(match env::var_os("TERM") {
|
||||||
Some(val) => {
|
Some(val) => {
|
||||||
if val.to_str().unwrap_or("").contains("256color") {
|
if val.to_str().unwrap_or("").contains("256color") {
|
||||||
|
143
crossterm_style/src/enums/attribute.rs
Normal file
143
crossterm_style/src/enums/attribute.rs
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
use std::fmt::Display;
|
||||||
|
use std::io::stdout;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
/// These are all the attributes which **could** be apply to font.
|
||||||
|
/// There are few things to note
|
||||||
|
/// - Not all attributes are supported, some of them are only supported on Windows some only on Unix,
|
||||||
|
/// and some are only very rarely supported.
|
||||||
|
/// - I got those attributes, descriptions, supportability from here: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
|
||||||
|
/// - Take note of the fact that when running your program cross-platform that some attributes might not work because of their support.
|
||||||
|
/// - When an attribute is not supported nothing will happen with the terminal state.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// You can use an attribute in a write statement to apply the attribute to the terminal output.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// println!(
|
||||||
|
/// "{} Underlined {} No Underline",
|
||||||
|
/// Attribute::Underlined,
|
||||||
|
/// Attribute::NoUnderline
|
||||||
|
/// );
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// You can also call attribute functions on a `&'static str`:
|
||||||
|
/// ```rust
|
||||||
|
/// use crossterm(_style)::Colorizer;
|
||||||
|
///
|
||||||
|
/// println!("{}", style("Bold text").bold());
|
||||||
|
/// println!("{}", style("Underlined text").underlined());
|
||||||
|
/// println!("{}", style("Negative text").negative());
|
||||||
|
/// ```
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
|
||||||
|
pub enum Attribute {
|
||||||
|
/// All attributes off
|
||||||
|
/// [info]: This will reset all current set attributes.
|
||||||
|
/// [Supportability]: Windows, UNIX.
|
||||||
|
Reset = 0,
|
||||||
|
/// Increased Intensity
|
||||||
|
/// [info]: This will increase the font sensitivity also known as bold.
|
||||||
|
/// [Supportability]: Windows, UNIX.
|
||||||
|
Bold = 1,
|
||||||
|
/// Decreased Intensity
|
||||||
|
/// [info]: This will decrease the font sensitivity also known as bold.
|
||||||
|
/// [Supportability]: Windows, UNIX.
|
||||||
|
Dim = 2,
|
||||||
|
/// Italic Text
|
||||||
|
/// [info]: This will make the font italic.
|
||||||
|
/// [Supportability]: Not widely supported, sometimes treated as inverse.
|
||||||
|
Italic = 3,
|
||||||
|
/// This will draw a line under the font.
|
||||||
|
/// [info]: An line under a word, especially in order to show its importance.
|
||||||
|
/// [Supportability]: Windows, UNIX
|
||||||
|
Underlined = 4,
|
||||||
|
/// Slow Blinking Text
|
||||||
|
/// [info]: Blink Less than 150 per minute.
|
||||||
|
/// [Supportability]: UNIX
|
||||||
|
SlowBlink = 5,
|
||||||
|
/// Slow Blinking Text
|
||||||
|
/// [info]: MS-DOS ANSI.SYS; 150+ per minute;
|
||||||
|
/// [Supportability]: Not widely supported
|
||||||
|
RapidBlink = 6,
|
||||||
|
/// Swap foreground and background colors
|
||||||
|
/// [info]: swap foreground and background colors
|
||||||
|
/// [Supportability]: Windows, UNIX
|
||||||
|
Reverse = 7,
|
||||||
|
/// Hide font
|
||||||
|
/// [info]:
|
||||||
|
/// - This will make the font hidden.
|
||||||
|
/// - Also known as 'Conceal'
|
||||||
|
/// [Supportability]: Windows, UNIX
|
||||||
|
Hidden = 8,
|
||||||
|
/// Cross-out font
|
||||||
|
/// [info]: Characters legible, but marked for deletion.
|
||||||
|
/// [Supportability]: UNIX
|
||||||
|
CrossedOut = 9,
|
||||||
|
/// The Fraktur is a typeface belonging to the group of Gothic typefaces.
|
||||||
|
/// [info]: https://nl.wikipedia.org/wiki/Fraktur
|
||||||
|
/// [Supportability]: Rarely supported
|
||||||
|
Fraktur = 20,
|
||||||
|
/// This will turn off the bold attribute.
|
||||||
|
/// [info]:
|
||||||
|
/// - Double-underline per ECMA-48.
|
||||||
|
/// - WikiPedia: https://en.wikipedia.org/wiki/Talk:ANSI_escape_code#SGR_21%E2%80%94%60Bold_off%60_not_widely_supported
|
||||||
|
/// - Opposite of `Bold`(1)
|
||||||
|
/// [Supportability]: not widely supported
|
||||||
|
NoBold = 21,
|
||||||
|
/// This will turn off the underline attribute.
|
||||||
|
/// [info]:
|
||||||
|
/// - Not singly or doubly underlined will be turned off.
|
||||||
|
/// - Opposite of `Underlined.`(4)
|
||||||
|
/// [Supportability]: Windows, UNIX
|
||||||
|
NoUnderline = 24,
|
||||||
|
/// This will turn off the blinking attribute
|
||||||
|
/// [info]: Opposite of `Slow and Rapid blink.`(5,6)
|
||||||
|
/// [Supportability]: Unknown
|
||||||
|
NoBlink = 25,
|
||||||
|
/// This will turn off the reverse attribute.
|
||||||
|
/// [info]: Opposite of `Reverse`(7)
|
||||||
|
/// [Supportability]: Windows, unknown
|
||||||
|
NoInverse = 27,
|
||||||
|
/// This will make the font visible.
|
||||||
|
/// [info]: Opposite of `Hidden`(8)
|
||||||
|
/// [Supportability]: Unknown
|
||||||
|
NoHidden = 28,
|
||||||
|
/// This will turn off the crossed out attribute.
|
||||||
|
/// [info]: Opposite of `CrossedOut`(9)
|
||||||
|
/// [Supportability]: Not widely supported
|
||||||
|
NotCrossedOut = 29,
|
||||||
|
/// This will reset the foreground color to default.
|
||||||
|
/// [info]: Implementation defined (according to standard)
|
||||||
|
/// [Supportability]: Unknown
|
||||||
|
DefaultForegroundColor = 48,
|
||||||
|
/// This will reset the background color to default.
|
||||||
|
/// [info]: Implementation defined (according to standard)
|
||||||
|
/// [Supportability]: Unknown
|
||||||
|
DefaultBackgroundColor = 49,
|
||||||
|
/// Framed font.
|
||||||
|
/// [Supportability]: Not widely supported
|
||||||
|
Framed = 51,
|
||||||
|
/// This will turn on the encircled attribute.
|
||||||
|
Encircled = 52,
|
||||||
|
/// This will draw a line at the top of the font.
|
||||||
|
/// [info]: Implementation defined (according to standard)
|
||||||
|
/// [Supportability]: Unknown
|
||||||
|
OverLined = 53,
|
||||||
|
/// This will turn off the framed or encircled attribute.
|
||||||
|
NotFramedOrEncircled = 54,
|
||||||
|
/// This will turn off the overLined attribute.
|
||||||
|
/// [info]: Opposite of `OverLined`(7)
|
||||||
|
/// [Supportability]: Windows, unknown
|
||||||
|
NotOverLined = 55,
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
__Nonexhaustive,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Attribute {
|
||||||
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
|
||||||
|
write!(f, "{}", format!(csi!("{}m"), *self as i16))?;
|
||||||
|
stdout().flush();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
81
crossterm_style/src/enums/color.rs
Normal file
81
crossterm_style/src/enums/color.rs
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
use std::convert::AsRef;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
/// Colors that are available for coloring the terminal font.
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
pub enum Color {
|
||||||
|
Black,
|
||||||
|
|
||||||
|
Red,
|
||||||
|
DarkRed,
|
||||||
|
|
||||||
|
Green,
|
||||||
|
DarkGreen,
|
||||||
|
|
||||||
|
Yellow,
|
||||||
|
DarkYellow,
|
||||||
|
|
||||||
|
Blue,
|
||||||
|
DarkBlue,
|
||||||
|
|
||||||
|
Magenta,
|
||||||
|
DarkMagenta,
|
||||||
|
|
||||||
|
Cyan,
|
||||||
|
DarkCyan,
|
||||||
|
|
||||||
|
Grey,
|
||||||
|
White,
|
||||||
|
/// Color representing RGB-colors;
|
||||||
|
/// r = red
|
||||||
|
/// g = green
|
||||||
|
/// b = blue
|
||||||
|
Rgb {
|
||||||
|
r: u8,
|
||||||
|
g: u8,
|
||||||
|
b: u8,
|
||||||
|
},
|
||||||
|
AnsiValue(u8),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a str> for Color {
|
||||||
|
/// Get an color from an &str like `Color::from("blue")`.
|
||||||
|
fn from(src: &str) -> Self {
|
||||||
|
src.parse().unwrap_or(Color::White)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<String> for Color {
|
||||||
|
/// Get an color from an &str like `Color::from(String::from(blue))`.
|
||||||
|
fn from(src: String) -> Self {
|
||||||
|
src.parse().unwrap_or(Color::White)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for Color {
|
||||||
|
type Err = ();
|
||||||
|
|
||||||
|
/// Convert a string to an Color value
|
||||||
|
fn from_str(src: &str) -> ::std::result::Result<Self, Self::Err> {
|
||||||
|
let src = src.to_lowercase();
|
||||||
|
|
||||||
|
match src.as_ref() {
|
||||||
|
"black" => Ok(Color::Black),
|
||||||
|
"red" => Ok(Color::Red),
|
||||||
|
"dark_red" => Ok(Color::DarkRed),
|
||||||
|
"green" => Ok(Color::Green),
|
||||||
|
"dark_green" => Ok(Color::DarkGreen),
|
||||||
|
"yellow" => Ok(Color::Yellow),
|
||||||
|
"dark_yellow" => Ok(Color::DarkYellow),
|
||||||
|
"blue" => Ok(Color::Blue),
|
||||||
|
"dark_blue" => Ok(Color::DarkBlue),
|
||||||
|
"magenta" => Ok(Color::Magenta),
|
||||||
|
"dark_magenta" => Ok(Color::DarkMagenta),
|
||||||
|
"cyan" => Ok(Color::Cyan),
|
||||||
|
"dark_cyan" => Ok(Color::DarkCyan),
|
||||||
|
"grey" => Ok(Color::Grey),
|
||||||
|
"white" => Ok(Color::White),
|
||||||
|
_ => Ok(Color::White),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
45
crossterm_style/src/enums/colored.rs
Normal file
45
crossterm_style/src/enums/colored.rs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
use crate::color::color;
|
||||||
|
use crate::enums::Color;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
/// Could be used to color the foreground or background color.
|
||||||
|
///
|
||||||
|
/// `Colored::Fg` represents the foreground color.
|
||||||
|
/// `Color::Bg` represents the background color.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// You can use `Colored` in a write statement to apply the attribute to the terminal output.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// println!("{} Red foreground color", Colored::Fg(Color::Red));
|
||||||
|
/// println!("{} Blue background color", Colored::Bg(Color::Blue));
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// You can also call coloring functions on a `&'static str`:
|
||||||
|
/// ```rust
|
||||||
|
/// use crossterm(_style)::Colorizer;
|
||||||
|
/// let styled_text = "Red forground color on blue background.".red().on_blue();
|
||||||
|
/// println!("{}", styled_text);
|
||||||
|
/// ```
|
||||||
|
pub enum Colored {
|
||||||
|
Fg(Color),
|
||||||
|
Bg(Color),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Colored {
|
||||||
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
|
||||||
|
let colored_terminal = color();
|
||||||
|
|
||||||
|
match *self {
|
||||||
|
Colored::Fg(color) => {
|
||||||
|
colored_terminal.set_fg(color);
|
||||||
|
}
|
||||||
|
Colored::Bg(color) => {
|
||||||
|
colored_terminal.set_bg(color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
5
crossterm_style/src/enums/mod.rs
Normal file
5
crossterm_style/src/enums/mod.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
mod attribute;
|
||||||
|
mod color;
|
||||||
|
mod colored;
|
||||||
|
|
||||||
|
pub use self::{attribute::Attribute, color::Color, colored::Colored};
|
@ -6,9 +6,13 @@ extern crate crossterm_utils;
|
|||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
extern crate crossterm_winapi;
|
extern crate crossterm_winapi;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod macros;
|
||||||
mod color;
|
mod color;
|
||||||
|
mod enums;
|
||||||
pub mod objectstyle;
|
pub mod objectstyle;
|
||||||
pub mod styledobject;
|
pub mod styledobject;
|
||||||
|
mod traits;
|
||||||
|
|
||||||
mod ansi_color;
|
mod ansi_color;
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
@ -24,10 +28,14 @@ use std::str::FromStr;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub use self::color::{color, TerminalColor};
|
pub use self::color::{color, TerminalColor};
|
||||||
|
pub use self::enums::{Attribute, Color, Colored};
|
||||||
pub use self::objectstyle::ObjectStyle;
|
pub use self::objectstyle::ObjectStyle;
|
||||||
pub use self::styledobject::DisplayableObject;
|
pub use self::styledobject::DisplayableObject;
|
||||||
pub use self::styledobject::StyledObject;
|
pub use self::styledobject::StyledObject;
|
||||||
|
pub use self::traits::{Colorize, Styler};
|
||||||
use crossterm_utils::{Result, TerminalOutput};
|
use crossterm_utils::{Result, TerminalOutput};
|
||||||
|
use std::io::stdout;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
/// This trait defines the actions that can be preformed with terminal color.
|
/// This trait defines the actions that can be preformed with terminal color.
|
||||||
/// This trait can be implemented so that a concrete implementation of the ITerminalColor can fulfill
|
/// This trait can be implemented so that a concrete implementation of the ITerminalColor can fulfill
|
||||||
@ -45,7 +53,7 @@ trait ITerminalColor {
|
|||||||
/// Reset the terminal color to default.
|
/// Reset the terminal color to default.
|
||||||
fn reset(&self, stdout: &Option<&Arc<TerminalOutput>>) -> Result<()>;
|
fn reset(&self, stdout: &Option<&Arc<TerminalOutput>>) -> Result<()>;
|
||||||
/// Gets an value that represents an color from the given `Color` and `ColorType`.
|
/// Gets an value that represents an color from the given `Color` and `ColorType`.
|
||||||
fn color_value(&self, color: Color, color_type: ColorType) -> String;
|
fn color_value(&self, cored: Colored) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This could be used to style a type who is implementing `Display` with colors and attributes.
|
/// This could be used to style a type who is implementing `Display` with colors and attributes.
|
||||||
@ -63,6 +71,11 @@ trait ITerminalColor {
|
|||||||
/// println!("{}", styled_object);
|
/// println!("{}", styled_object);
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// # Important Remark
|
||||||
|
///
|
||||||
|
/// - Please checkout the documentation for `Colorizer` or `Styler`.
|
||||||
|
/// Those types will make it a bit easier to style a string.
|
||||||
pub fn style<'a, D: 'a>(val: D) -> StyledObject<D>
|
pub fn style<'a, D: 'a>(val: D) -> StyledObject<D>
|
||||||
where
|
where
|
||||||
D: Display,
|
D: Display,
|
||||||
@ -70,115 +83,52 @@ where
|
|||||||
ObjectStyle::new().apply_to(val)
|
ObjectStyle::new().apply_to(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attributes that could be applied on some text. (*nix values)
|
impl Colorize<&'static str> for &'static str {
|
||||||
#[cfg(unix)]
|
// foreground colors
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
|
def_str_color!(fg_color: black => Color::Black);
|
||||||
pub enum Attribute {
|
def_str_color!(fg_color: red => Color::Red);
|
||||||
Bold = 1,
|
def_str_color!(fg_color: dark_red => Color::DarkRed);
|
||||||
Dim = 2,
|
def_str_color!(fg_color: green => Color::Green);
|
||||||
Italic = 3,
|
def_str_color!(fg_color: dark_green => Color::DarkGreen);
|
||||||
Underlined = 4,
|
def_str_color!(fg_color: yellow => Color::Yellow);
|
||||||
SlowBlink = 5,
|
def_str_color!(fg_color: dark_yellow => Color::DarkYellow);
|
||||||
RapidBlink = 6,
|
def_str_color!(fg_color: blue => Color::Blue);
|
||||||
Reverse = 7,
|
def_str_color!(fg_color: dark_blue => Color::DarkBlue);
|
||||||
Hidden = 8,
|
def_str_color!(fg_color: magenta => Color::Magenta);
|
||||||
CrossedOut = 9,
|
def_str_color!(fg_color: dark_magenta => Color::DarkMagenta);
|
||||||
|
def_str_color!(fg_color: cyan => Color::Cyan);
|
||||||
|
def_str_color!(fg_color: dark_cyan => Color::DarkCyan);
|
||||||
|
def_str_color!(fg_color: white => Color::White);
|
||||||
|
def_str_color!(fg_color: grey => Color::Grey);
|
||||||
|
|
||||||
|
// background colors
|
||||||
|
def_str_color!(bg_color: on_black => Color::Black);
|
||||||
|
def_str_color!(bg_color: on_red => Color::Red);
|
||||||
|
def_str_color!(bg_color: on_dark_red => Color::DarkRed);
|
||||||
|
def_str_color!(bg_color: on_green => Color::Green);
|
||||||
|
def_str_color!(bg_color: on_dark_green => Color::DarkGreen);
|
||||||
|
def_str_color!(bg_color: on_yellow => Color::Yellow);
|
||||||
|
def_str_color!(bg_color: on_dark_yellow => Color::DarkYellow);
|
||||||
|
def_str_color!(bg_color: on_blue => Color::Blue);
|
||||||
|
def_str_color!(bg_color: on_dark_blue => Color::DarkBlue);
|
||||||
|
def_str_color!(bg_color: on_magenta => Color::Magenta);
|
||||||
|
def_str_color!(bg_color: on_dark_magenta => Color::DarkMagenta);
|
||||||
|
def_str_color!(bg_color: on_cyan => Color::Cyan);
|
||||||
|
def_str_color!(bg_color: on_dark_cyan => Color::DarkCyan);
|
||||||
|
def_str_color!(bg_color: on_white => Color::White);
|
||||||
|
def_str_color!(bg_color: on_grey => Color::Grey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attributes that could be applied on some text. (Windows specific)
|
impl Styler<&'static str> for &'static str {
|
||||||
#[cfg(windows)]
|
def_str_attr!(reset => Attribute::Reset);
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
|
def_str_attr!(bold => Attribute::Bold);
|
||||||
pub enum Attribute {
|
def_str_attr!(underlined => Attribute::Underlined);
|
||||||
Reset = 0,
|
def_str_attr!(reverse => Attribute::Reverse);
|
||||||
Bold = 1,
|
def_str_attr!(dim => Attribute::Dim);
|
||||||
Underlined = 4,
|
def_str_attr!(italic => Attribute::Italic);
|
||||||
NoUnderline = 24,
|
def_str_attr!(negative => Attribute::Reverse);
|
||||||
Negative = 7,
|
def_str_attr!(slow_blink => Attribute::SlowBlink);
|
||||||
Positive = 27,
|
def_str_attr!(rapid_blink => Attribute::RapidBlink);
|
||||||
}
|
def_str_attr!(hidden => Attribute::Hidden);
|
||||||
|
def_str_attr!(crossed_out => Attribute::CrossedOut);
|
||||||
/// Colors that are available for coloring the terminal font.
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
|
||||||
pub enum Color {
|
|
||||||
Black,
|
|
||||||
|
|
||||||
Red,
|
|
||||||
DarkRed,
|
|
||||||
|
|
||||||
Green,
|
|
||||||
DarkGreen,
|
|
||||||
|
|
||||||
Yellow,
|
|
||||||
DarkYellow,
|
|
||||||
|
|
||||||
Blue,
|
|
||||||
DarkBlue,
|
|
||||||
|
|
||||||
Magenta,
|
|
||||||
DarkMagenta,
|
|
||||||
|
|
||||||
Cyan,
|
|
||||||
DarkCyan,
|
|
||||||
|
|
||||||
Grey,
|
|
||||||
White,
|
|
||||||
/// Color representing RGB-colors;
|
|
||||||
/// r = red
|
|
||||||
/// g = green
|
|
||||||
/// b = blue
|
|
||||||
Rgb {
|
|
||||||
r: u8,
|
|
||||||
g: u8,
|
|
||||||
b: u8,
|
|
||||||
},
|
|
||||||
AnsiValue(u8),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Color types that can be used to determine if the Color enum is a Fore- or Background Color.
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
|
||||||
pub enum ColorType {
|
|
||||||
Background,
|
|
||||||
Foreground,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a str> for Color {
|
|
||||||
/// Get an color from an &str like `Color::from("blue")`.
|
|
||||||
fn from(src: &str) -> Self {
|
|
||||||
src.parse().unwrap_or(Color::White)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<String> for Color {
|
|
||||||
/// Get an color from an &str like `Color::from(String::from(blue))`.
|
|
||||||
fn from(src: String) -> Self {
|
|
||||||
src.parse().unwrap_or(Color::White)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for Color {
|
|
||||||
type Err = ();
|
|
||||||
|
|
||||||
/// Convert a string to an Color value
|
|
||||||
fn from_str(src: &str) -> ::std::result::Result<Self, Self::Err> {
|
|
||||||
let src = src.to_lowercase();
|
|
||||||
|
|
||||||
match src.as_ref() {
|
|
||||||
"black" => Ok(Color::Black),
|
|
||||||
"red" => Ok(Color::Red),
|
|
||||||
"dark_red" => Ok(Color::DarkRed),
|
|
||||||
"green" => Ok(Color::Green),
|
|
||||||
"dark_green" => Ok(Color::DarkGreen),
|
|
||||||
"yellow" => Ok(Color::Yellow),
|
|
||||||
"dark_yellow" => Ok(Color::DarkYellow),
|
|
||||||
"blue" => Ok(Color::Blue),
|
|
||||||
"dark_blue" => Ok(Color::DarkBlue),
|
|
||||||
"magenta" => Ok(Color::Magenta),
|
|
||||||
"dark_magenta" => Ok(Color::DarkMagenta),
|
|
||||||
"cyan" => Ok(Color::Cyan),
|
|
||||||
"dark_cyan" => Ok(Color::DarkCyan),
|
|
||||||
"grey" => Ok(Color::Grey),
|
|
||||||
"white" => Ok(Color::White),
|
|
||||||
_ => Ok(Color::White),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
50
crossterm_style/src/macros.rs
Normal file
50
crossterm_style/src/macros.rs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
use crate::{ObjectStyle, StyledObject};
|
||||||
|
|
||||||
|
macro_rules! def_attr {
|
||||||
|
($name: ident => $attr: path) => {
|
||||||
|
fn $name(self) -> StyledObject<D> {
|
||||||
|
let mut so = self;
|
||||||
|
|
||||||
|
so.attr($attr)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! def_color {
|
||||||
|
($side:ident: $name: ident => $color: path) => {
|
||||||
|
fn $name(self) -> StyledObject<D> {
|
||||||
|
StyledObject {
|
||||||
|
object_style: ObjectStyle {
|
||||||
|
$side: Some($color),
|
||||||
|
.. self.object_style
|
||||||
|
},
|
||||||
|
.. self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! def_str_color {
|
||||||
|
($side:ident: $name: ident => $color: path) => {
|
||||||
|
fn $name(self) -> StyledObject< &'static str> {
|
||||||
|
StyledObject {
|
||||||
|
object_style: ObjectStyle {
|
||||||
|
$side: Some($color),
|
||||||
|
.. ObjectStyle::default()
|
||||||
|
},
|
||||||
|
content: self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! def_str_attr {
|
||||||
|
($name: ident => $color: path) => {
|
||||||
|
fn $name(self) -> StyledObject<&'static str> {
|
||||||
|
StyledObject {
|
||||||
|
object_style: ObjectStyle::default(),
|
||||||
|
content: self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,8 @@ use std::result;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use super::Attribute;
|
use super::Attribute;
|
||||||
|
use crate::Colorize;
|
||||||
|
use crate::Styler;
|
||||||
|
|
||||||
/// Struct that contains both the style and the content wits can be styled.
|
/// Struct that contains both the style and the content wits can be styled.
|
||||||
pub struct StyledObject<D: Display> {
|
pub struct StyledObject<D: Display> {
|
||||||
@ -50,65 +52,6 @@ impl<'a, D: Display + 'a> StyledObject<D> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Increase the font intensity.
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn bold(self) -> StyledObject<D> {
|
|
||||||
self.attr(Attribute::Bold)
|
|
||||||
}
|
|
||||||
/// Faint (decreased intensity) (Not widely supported).
|
|
||||||
#[cfg(unix)]
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn dim(self) -> StyledObject<D> {
|
|
||||||
self.attr(Attribute::Dim)
|
|
||||||
}
|
|
||||||
/// Make the font italic (Not widely supported; Sometimes treated as inverse).
|
|
||||||
#[cfg(unix)]
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn italic(self) -> StyledObject<D> {
|
|
||||||
self.attr(Attribute::Italic)
|
|
||||||
}
|
|
||||||
/// Underline font.
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn underlined(self) -> StyledObject<D> {
|
|
||||||
self.attr(Attribute::Underlined)
|
|
||||||
}
|
|
||||||
/// Invert colours.
|
|
||||||
#[cfg(windows)]
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn negative(self) -> StyledObject<D> {
|
|
||||||
self.attr(Attribute::Negative)
|
|
||||||
}
|
|
||||||
/// Slow Blink (less than 150 per minute; not widely supported).
|
|
||||||
#[cfg(unix)]
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn slow_blink(self) -> StyledObject<D> {
|
|
||||||
self.attr(Attribute::SlowBlink)
|
|
||||||
}
|
|
||||||
/// Rapid Blink (MS-DOS ANSI.SYS; 150+ per minute; not widely supported).
|
|
||||||
#[cfg(unix)]
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn rapid_blink(self) -> StyledObject<D> {
|
|
||||||
self.attr(Attribute::RapidBlink)
|
|
||||||
}
|
|
||||||
/// Swap foreground and background colors.
|
|
||||||
#[cfg(unix)]
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn reverse(self) -> StyledObject<D> {
|
|
||||||
self.attr(Attribute::Reverse)
|
|
||||||
}
|
|
||||||
/// Hide text (Not widely supported).
|
|
||||||
#[cfg(unix)]
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn hidden(self) -> StyledObject<D> {
|
|
||||||
self.attr(Attribute::Hidden)
|
|
||||||
}
|
|
||||||
/// Characters legible, but marked for deletion. Not widely supported.
|
|
||||||
#[cfg(unix)]
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn crossed_out(self) -> StyledObject<D> {
|
|
||||||
self.attr(Attribute::CrossedOut)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This converts an styled object into an `DisplayableObject` witch implements: `Display` and could be used inside the write function of the standard library.
|
/// This converts an styled object into an `DisplayableObject` witch implements: `Display` and could be used inside the write function of the standard library.
|
||||||
///
|
///
|
||||||
/// _StyledObject already implements `Display` right?_
|
/// _StyledObject already implements `Display` right?_
|
||||||
@ -199,6 +142,56 @@ impl<D: Display> Display for StyledObject<D> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<D: Display> Colorize<D> for StyledObject<D> {
|
||||||
|
// foreground colors
|
||||||
|
def_color!(fg_color: black => Color::Black);
|
||||||
|
def_color!(fg_color: red => Color::Red);
|
||||||
|
def_color!(fg_color: dark_red => Color::DarkRed);
|
||||||
|
def_color!(fg_color: green => Color::Green);
|
||||||
|
def_color!(fg_color: dark_green => Color::DarkGreen);
|
||||||
|
def_color!(fg_color: yellow => Color::Yellow);
|
||||||
|
def_color!(fg_color: dark_yellow => Color::DarkYellow);
|
||||||
|
def_color!(fg_color: blue => Color::Blue);
|
||||||
|
def_color!(fg_color: dark_blue => Color::DarkBlue);
|
||||||
|
def_color!(fg_color: magenta => Color::Magenta);
|
||||||
|
def_color!(fg_color: dark_magenta => Color::DarkMagenta);
|
||||||
|
def_color!(fg_color: cyan => Color::Cyan);
|
||||||
|
def_color!(fg_color: dark_cyan => Color::DarkCyan);
|
||||||
|
def_color!(fg_color: white => Color::White);
|
||||||
|
def_color!(fg_color: grey => Color::Grey);
|
||||||
|
|
||||||
|
// background colors
|
||||||
|
def_color!(bg_color: on_black => Color::Black);
|
||||||
|
def_color!(bg_color: on_red => Color::Red);
|
||||||
|
def_color!(bg_color: on_dark_red => Color::DarkRed);
|
||||||
|
def_color!(bg_color: on_green => Color::Green);
|
||||||
|
def_color!(bg_color: on_dark_green => Color::DarkGreen);
|
||||||
|
def_color!(bg_color: on_yellow => Color::Yellow);
|
||||||
|
def_color!(bg_color: on_dark_yellow => Color::DarkYellow);
|
||||||
|
def_color!(bg_color: on_blue => Color::Blue);
|
||||||
|
def_color!(bg_color: on_dark_blue => Color::DarkBlue);
|
||||||
|
def_color!(bg_color: on_magenta => Color::Magenta);
|
||||||
|
def_color!(bg_color: on_dark_magenta => Color::DarkMagenta);
|
||||||
|
def_color!(bg_color: on_cyan => Color::Cyan);
|
||||||
|
def_color!(bg_color: on_dark_cyan => Color::DarkCyan);
|
||||||
|
def_color!(bg_color: on_white => Color::White);
|
||||||
|
def_color!(bg_color: on_grey => Color::Grey);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D: Display> Styler<D> for StyledObject<D> {
|
||||||
|
def_attr!(reset => Attribute::Reset);
|
||||||
|
def_attr!(bold => Attribute::Bold);
|
||||||
|
def_attr!(underlined => Attribute::Underlined);
|
||||||
|
def_attr!(reverse => Attribute::Reverse);
|
||||||
|
def_attr!(dim => Attribute::Dim);
|
||||||
|
def_attr!(italic => Attribute::Italic);
|
||||||
|
def_attr!(negative => Attribute::Reverse);
|
||||||
|
def_attr!(slow_blink => Attribute::SlowBlink);
|
||||||
|
def_attr!(rapid_blink => Attribute::RapidBlink);
|
||||||
|
def_attr!(hidden => Attribute::Hidden);
|
||||||
|
def_attr!(crossed_out => Attribute::CrossedOut);
|
||||||
|
}
|
||||||
|
|
||||||
/// This is a wrapper for a styled object on 'alternate screen' so that the styled object could be printed on the 'alternate screen' with the standard write functions in rust.
|
/// This is a wrapper for a styled object on 'alternate screen' so that the styled object could be printed on the 'alternate screen' with the standard write functions in rust.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
72
crossterm_style/src/traits.rs
Normal file
72
crossterm_style/src/traits.rs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
use crate::{ObjectStyle, StyledObject};
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
/// Provides a set of methods to color any type implementing `Display` with attributes.
|
||||||
|
///
|
||||||
|
/// This trait is implemented for `&static str` and `StyledObject` and thus the methods of this trait could be called on them.
|
||||||
|
///
|
||||||
|
/// ``` rust
|
||||||
|
/// use Colorizer;
|
||||||
|
///
|
||||||
|
/// let styled_text = "Red forground color on blue background.".red().on_blue();
|
||||||
|
/// println!("{}", styled_text);
|
||||||
|
/// ```
|
||||||
|
pub trait Colorize<D: Display> {
|
||||||
|
fn black(self) -> StyledObject<D>;
|
||||||
|
fn red(self) -> StyledObject<D>;
|
||||||
|
fn dark_red(self) -> StyledObject<D>;
|
||||||
|
fn green(self) -> StyledObject<D>;
|
||||||
|
fn dark_green(self) -> StyledObject<D>;
|
||||||
|
fn yellow(self) -> StyledObject<D>;
|
||||||
|
fn dark_yellow(self) -> StyledObject<D>;
|
||||||
|
fn blue(self) -> StyledObject<D>;
|
||||||
|
fn dark_blue(self) -> StyledObject<D>;
|
||||||
|
fn magenta(self) -> StyledObject<D>;
|
||||||
|
fn dark_magenta(self) -> StyledObject<D>;
|
||||||
|
fn cyan(self) -> StyledObject<D>;
|
||||||
|
fn dark_cyan(self) -> StyledObject<D>;
|
||||||
|
fn white(self) -> StyledObject<D>;
|
||||||
|
fn grey(self) -> StyledObject<D>;
|
||||||
|
|
||||||
|
fn on_black(self) -> StyledObject<D>;
|
||||||
|
fn on_red(self) -> StyledObject<D>;
|
||||||
|
fn on_dark_red(self) -> StyledObject<D>;
|
||||||
|
fn on_green(self) -> StyledObject<D>;
|
||||||
|
fn on_dark_green(self) -> StyledObject<D>;
|
||||||
|
fn on_yellow(self) -> StyledObject<D>;
|
||||||
|
fn on_dark_yellow(self) -> StyledObject<D>;
|
||||||
|
fn on_blue(self) -> StyledObject<D>;
|
||||||
|
fn on_dark_blue(self) -> StyledObject<D>;
|
||||||
|
fn on_magenta(self) -> StyledObject<D>;
|
||||||
|
fn on_dark_magenta(self) -> StyledObject<D>;
|
||||||
|
fn on_cyan(self) -> StyledObject<D>;
|
||||||
|
fn on_dark_cyan(self) -> StyledObject<D>;
|
||||||
|
fn on_white(self) -> StyledObject<D>;
|
||||||
|
fn on_grey(self) -> StyledObject<D>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Provides a set of methods to style any type implementing `Display` with attributes.
|
||||||
|
///
|
||||||
|
/// This trait is implemented for `&static str` and `StyledObject` and thus the methods of this trait could be called on them.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ``` rust
|
||||||
|
/// use Colorizer;
|
||||||
|
///
|
||||||
|
/// println!("{}", "Bold text".bold();
|
||||||
|
/// println!("{}", "Underlined text".underlined();
|
||||||
|
/// println!("{}", "Negative text".negative();
|
||||||
|
/// ```
|
||||||
|
pub trait Styler<D: Display> {
|
||||||
|
fn reset(self) -> StyledObject<D>;
|
||||||
|
fn bold(self) -> StyledObject<D>;
|
||||||
|
fn underlined(self) -> StyledObject<D>;
|
||||||
|
fn reverse(self) -> StyledObject<D>;
|
||||||
|
fn dim(self) -> StyledObject<D>;
|
||||||
|
fn italic(self) -> StyledObject<D>;
|
||||||
|
fn negative(self) -> StyledObject<D>;
|
||||||
|
fn slow_blink(self) -> StyledObject<D>;
|
||||||
|
fn rapid_blink(self) -> StyledObject<D>;
|
||||||
|
fn hidden(self) -> StyledObject<D>;
|
||||||
|
fn crossed_out(self) -> StyledObject<D>;
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
//! This is an `WinApi` specific implementation for styling related action.
|
//! This is an `WinApi` specific implementation for styling related action.
|
||||||
//! This module is used for non supporting `ANSI` Windows terminals.
|
//! This module is used for non supporting `ANSI` Windows terminals.
|
||||||
|
|
||||||
use crate::{Color, ColorType, ITerminalColor};
|
use crate::{Color, Colored, ITerminalColor};
|
||||||
use crossterm_utils::{Result, TerminalOutput};
|
use crossterm_utils::{Result, TerminalOutput};
|
||||||
use crossterm_winapi::{Console, Handle, HandleType, ScreenBuffer};
|
use crossterm_winapi::{Console, Handle, HandleType, ScreenBuffer};
|
||||||
use std::io;
|
use std::io;
|
||||||
@ -23,7 +23,7 @@ impl ITerminalColor for WinApiColor {
|
|||||||
// init the original color in case it is not set.
|
// init the original color in case it is not set.
|
||||||
let _ = init_console_color()?;
|
let _ = init_console_color()?;
|
||||||
|
|
||||||
let color_value = &self.color_value(fg_color, ColorType::Foreground);
|
let color_value = &self.color_value(Colored::Fg(fg_color));
|
||||||
|
|
||||||
let screen_buffer = ScreenBuffer::current()?;
|
let screen_buffer = ScreenBuffer::current()?;
|
||||||
let csbi = screen_buffer.info()?;
|
let csbi = screen_buffer.info()?;
|
||||||
@ -50,7 +50,7 @@ impl ITerminalColor for WinApiColor {
|
|||||||
// init the original color in case it is not set.
|
// init the original color in case it is not set.
|
||||||
let _ = init_console_color()?;
|
let _ = init_console_color()?;
|
||||||
|
|
||||||
let color_value = &self.color_value(bg_color, ColorType::Background);
|
let color_value = &self.color_value(Colored::Bg(bg_color));
|
||||||
|
|
||||||
let screen_buffer = ScreenBuffer::current()?;
|
let screen_buffer = ScreenBuffer::current()?;
|
||||||
let csbi = screen_buffer.info()?;
|
let csbi = screen_buffer.info()?;
|
||||||
@ -83,7 +83,7 @@ impl ITerminalColor for WinApiColor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// This will get the winapi color value from the Color and ColorType struct
|
/// This will get the winapi color value from the Color and ColorType struct
|
||||||
fn color_value(&self, color: Color, color_type: ColorType) -> String {
|
fn color_value(&self, color: Colored) -> String {
|
||||||
let winapi_color: u16;
|
let winapi_color: u16;
|
||||||
|
|
||||||
let fg_green = wincon::FOREGROUND_GREEN;
|
let fg_green = wincon::FOREGROUND_GREEN;
|
||||||
@ -96,8 +96,8 @@ impl ITerminalColor for WinApiColor {
|
|||||||
let bg_blue = wincon::BACKGROUND_BLUE;
|
let bg_blue = wincon::BACKGROUND_BLUE;
|
||||||
let bg_intensity = wincon::BACKGROUND_INTENSITY;
|
let bg_intensity = wincon::BACKGROUND_INTENSITY;
|
||||||
|
|
||||||
match color_type {
|
match color {
|
||||||
ColorType::Foreground => {
|
Colored::Fg(color) => {
|
||||||
winapi_color = match color {
|
winapi_color = match color {
|
||||||
Color::Black => 0,
|
Color::Black => 0,
|
||||||
Color::Red => fg_intensity | fg_red,
|
Color::Red => fg_intensity | fg_red,
|
||||||
@ -120,7 +120,7 @@ impl ITerminalColor for WinApiColor {
|
|||||||
Color::AnsiValue(_val) => 0,
|
Color::AnsiValue(_val) => 0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
ColorType::Background => {
|
Colored::Bg(color) => {
|
||||||
winapi_color = match color {
|
winapi_color = match color {
|
||||||
Color::Black => 0,
|
Color::Black => 0,
|
||||||
Color::Red => bg_intensity | bg_red,
|
Color::Red => bg_intensity | bg_red,
|
||||||
|
@ -12,14 +12,14 @@ readme = "README.md"
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
crossterm_winapi = { path = "../crossterm_winapi" }
|
crossterm_winapi = "0.1.1"
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
libc = "0.2.43"
|
libc = "0.2.43"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
crossterm_utils = { path = "../crossterm_utils" }
|
crossterm_utils = "0.1.0"
|
||||||
crossterm_cursor = { path = "../crossterm_cursor" }
|
crossterm_cursor = "0.1.0"
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "terminal"
|
name = "terminal"
|
||||||
|
@ -5,7 +5,7 @@ authors = ["Timon Post <timonpost@hotmail.nl>"]
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
crossterm_winapi = { path = "../crossterm_winapi" }
|
crossterm_winapi = "0.1.1"
|
||||||
winapi = { version = "0.3.5", features = ["wincon"] }
|
winapi = { version = "0.3.5", features = ["wincon"] }
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
|
# Styling Module
|
||||||
|
|
||||||
Crossterm provides options for you to style your font and terminal. Take for example coloring output and applying attributes.
|
Crossterm provides options for you to style your font and terminal. Take for example coloring output and applying attributes.
|
||||||
|
|
||||||
**Color support**
|
**Color support**
|
||||||
Windows systems with versions less than 10 only have support for 16 colors and don't have any support for attributes.
|
Windows systems with versions less than 10 will only have support for 16 colors and don't have any support for attributes. Most UNIX-terminal is supporting lots of colors and attributes.
|
||||||
|
|
||||||
## Colors
|
## Colors
|
||||||
There are 16 base colors which available for almost all terminals even windows 7 and 8.
|
There are 16 base colors which available for almost all terminals even windows 7 and 8.
|
||||||
@ -17,36 +19,34 @@ There are 16 base colors which available for almost all terminals even windows 7
|
|||||||
| Cyan | DarkCyan |
|
| Cyan | DarkCyan |
|
||||||
| White | DarkWhite |
|
| White | DarkWhite |
|
||||||
|
|
||||||
In addition to 16 colours, most UNIX terminals and Windows 10 consoles are also supporting more colors.
|
In addition to 16 colors, most UNIX terminals and Windows 10 consoles are also supporting more colors.
|
||||||
Those colors could be: [True color (24-bit)](https://en.wikipedia.org/wiki/Color_depth#True_color_(24-bit)) coloring scheme, which allows you to use [RGB](https://nl.wikipedia.org/wiki/RGB-kleursysteem), and [256 (Xterm, 8-bit)](https://jonasjacek.github.io/colors/) colors.
|
Those colors could be: [True color (24-bit)](https://en.wikipedia.org/wiki/Color_depth#True_color_(24-bit)) coloring scheme, which allows you to use [RGB](https://nl.wikipedia.org/wiki/RGB-kleursysteem), and [256 (Xterm, 8-bit)](https://jonasjacek.github.io/colors/) colors.
|
||||||
|
|
||||||
## Attributes
|
## Attributes
|
||||||
Only UNIX and Windows 10 terminals are supporting attributes on top of text. Crossterm allows you to add attributes to the text.
|
Only UNIX and Windows 10 terminals are supporting attributes on top of the text. Crossterm allows you to add attributes to the text.
|
||||||
Not all attributes are widely supported for all terminals, keep that in mind when working with this.
|
Not all attributes are widely supported for all terminals, keep that in mind when working with this.
|
||||||
|
|
||||||
**Unix Attributes**
|
Crossterm implements almost all attributes shown in this [Wikipedia-list](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters).
|
||||||
|
|
||||||
| Attribute | Note |
|
| Attribute | Support | Note |
|
||||||
| :-------------: | :-------------: |
|
| :-------------: | :-------------: | :-------------: |
|
||||||
| Bold | _ |
|
| Reset | Windows, UNIX | This will reset all current set attributes. |
|
||||||
| Underlined | _|
|
| Bold | Windows, UNIX | This will increase the font sensitivity also known as bold. |
|
||||||
| Dim | _|
|
| Dim | Windows, UNIX | This will decrease the font sensitivity also known as bold. |
|
||||||
| SlowBlink | less than 150 per minute |
|
| Italic | Not widely supported, sometimes treated as inverse. | This will make the font italic. |
|
||||||
| CrosseOut | characters legible, but marked for deletion. |
|
| Underlined | Windows, UNIX | An line under a word, especially in order to show its importance. |
|
||||||
| Italic | not widely supported; Sometimes treated as inverse |
|
| SlowBlink | Not widely supported, sometimes treated as inverse. | less than 150 per minute |
|
||||||
| RapidBlink | not widely supported; MS-DOS ANSI.SYS; 150+ per minute |
|
| RapidBlink | Not widely supported | MS-DOS ANSI.SYS; 150+ per minute; |
|
||||||
| Reverse | not widely supported |
|
| Reverse | Windows, UNIX | foreground and background colors |
|
||||||
| Hidden | not widely supported |
|
| Hidden | Windows, UNIX | | Also known as 'Conceal'
|
||||||
|
| Fraktur | UNIX | characters legible, but marked for deletion. |
|
||||||
|
| DefaultForegroundColor | Unknown | Implementation defined (according to standard) |
|
||||||
|
| DefaultBackgroundColor | Unknown | Implementation defined (according to standard) |
|
||||||
|
| Framed | Not widely supported | Framed font.
|
||||||
|
| Encircled | Unknown | This will turn on the encircled attribute. |
|
||||||
|
| OverLined | Unknown | This will draw a line at the top of the font. |
|
||||||
|
|
||||||
**Windows Attributes**
|
(There are a few attributes who disable one of the above, I did not write those down in the above scheme).
|
||||||
|
|
||||||
| Attribute | Note |
|
|
||||||
| :-------------: | :-------------: |
|
|
||||||
| Reset | _ |
|
|
||||||
| Underlined | _ |
|
|
||||||
| NoUnderline | _ |
|
|
||||||
| Negative | _ |
|
|
||||||
| Positive | _ |
|
|
||||||
|
|
||||||
Now we have covered the basics of styling lets go some [examples](styling_example.md).
|
Now we have covered the basics of styling lets go some [examples](styling_example.md).
|
||||||
|
|
||||||
|
@ -4,48 +4,55 @@ _setup the basics_
|
|||||||
```rust
|
```rust
|
||||||
extern crate crossterm;
|
extern crate crossterm;
|
||||||
|
|
||||||
use crossterm::{style, Color, Attribute};
|
use crossterm::{Colored, Color, Attribute, Styler, Colorize};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
/* your code here */
|
/* your code here */
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Let's dive into styling with crossterm. The easiest way to do this is by making use of the `style()` function.
|
There are a couple of ways to style the terminal output with crossterm. The most important part of the styling module is `StyledObject`.
|
||||||
|
|
||||||
```rust
|
|
||||||
let styled_object = style("This is some text converted into a styled object");
|
|
||||||
```
|
|
||||||
|
|
||||||
The function `style()` takes in any type that implement `Display`
|
|
||||||
and returns a `StyledObject`.
|
|
||||||
A `StyledObject` is just a wrapper crossterm uses to store the text and style together.
|
A `StyledObject` is just a wrapper crossterm uses to store the text and style together.
|
||||||
|
A `StyledObject` implements `Display` and thus you could use it inside `print!`, `println!` etc.
|
||||||
|
|
||||||
The above code will not do any coloring magic yet. Lets play around with some colors to see it in working.
|
Without further ado let's get straight into it.
|
||||||
|
|
||||||
## Coloring
|
## Coloring
|
||||||
```rust
|
|
||||||
let styled_object = style("'Red' text on 'White' background")
|
|
||||||
.with(Color::Red)
|
|
||||||
.on(Color::White);
|
|
||||||
|
|
||||||
println!("{}", styled_object);
|
There are a few ways to do the coloring, the first one is by using the `Colored` enum.
|
||||||
|
|
||||||
|
### Using Enum
|
||||||
|
```rust
|
||||||
|
println!("{} Red foreground color", Colored::Fg(Color::Red));
|
||||||
|
println!("{} Blue background color", Colored::Bg(Color::Blue));
|
||||||
|
```
|
||||||
|
`Colored::Bg` will set the background color, and `Colored::Fg` will set the foreground color to the provided color.
|
||||||
|
The provided color is of type `Color` and has a bunch of enum values you could choose out.
|
||||||
|
|
||||||
|
Because `Colored` implements `Display` you are able to use it inside any write statement.
|
||||||
|
|
||||||
|
### Using Methods
|
||||||
|
You can do the same as the above in a slightly different way. Instead of enabling it for all text you could also color the only piece of text.
|
||||||
|
(Make sure to include the `crossterm::Coloring` trait).
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let styled_text = "Red forground color on blue background.".red().on_blue();
|
||||||
|
println!("{}", styled_text);
|
||||||
```
|
```
|
||||||
|
|
||||||
With the function `with()` you can decide the foreground color and with the function `on()` you can decide the background color of the text.
|
As you see in the above example you could call coloring methods on a string. How is this possible you might ask..?
|
||||||
Because `StyledObject` you got from `style()` implements `Display` you are allowed to print it with: `print!, println!, write` etc.
|
Well, the trait `Coloring`, who you need to include, is implemented for `&'static str`.
|
||||||
When running the above code you are supposed to see colored text with foreground color 'red' and with the background color 'white'.
|
When calling a method on this string crossterm transforms it into a `StyledObject` who you could use in your write statements.
|
||||||
|
|
||||||
_note: you don't have to color both backround an foreground, if not specified they remain as they are_.
|
|
||||||
|
|
||||||
### RGB
|
### RGB
|
||||||
Most UNIX terminals and all Windows 10 consoles are supporting [True color(24-bit)](https://en.wikipedia.org/wiki/Color_depth#True_color_(24-bit)) coloring scheme.
|
Most UNIX terminals and all Windows 10 consoles are supporting [True color(24-bit)](https://en.wikipedia.org/wiki/Color_depth#True_color_(24-bit)) coloring scheme.
|
||||||
You can set the color of the terminal by using `Color::RGB(r,g,b)`.
|
You can set the color of the terminal by using `Color::RGB(r,g,b)`.
|
||||||
|
|
||||||
```
|
```
|
||||||
let styled_object = style("'Light green' text on 'Black' background")
|
// custom rgb value (Windows 10 and UNIX systems)
|
||||||
.with(Color::Rgb {r: 0, g: 255, b: 128})
|
println!("{}{} 'Light green' text on 'Black' background", Colored::Fg(Color::Rgb { r: 0, g: 255, b: 128 }), Colored::Bg(Color::Rgb {r: 0, g: 0, b: 0}));
|
||||||
.on(Color::Rgb {r: 0, g: 0, b: 0});
|
|
||||||
```
|
```
|
||||||
This will print some light green text on black background.
|
This will print some light green text on black background.
|
||||||
|
|
||||||
@ -54,25 +61,40 @@ When working on UNIX or Windows 10 you could also specify a custom ANSI value ra
|
|||||||
See [256 (Xterm, 8-bit) colors](https://jonasjacek.github.io/colors/) for more information.
|
See [256 (Xterm, 8-bit) colors](https://jonasjacek.github.io/colors/) for more information.
|
||||||
|
|
||||||
```
|
```
|
||||||
let styled_object = style("'Red' text on 'White' background")
|
// custom ansi color value (Windows 10 and UNIX systems)
|
||||||
.with(Color::AnsiValue(9))
|
println!("{} some colored text", Colored::Fg(Color::AnsiValue(10)));
|
||||||
.on(Color::AnsiValue(15));
|
|
||||||
|
|
||||||
|
|
||||||
println!("{}", styled_object);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Attributes
|
## Attributes
|
||||||
When working with UNIX or Windows 10 terminals you could also use attributes to style your font. For example you could cross your text with a line and make it bold.
|
When working with UNIX or Windows 10 terminals you could also use attributes to style your font. For example, you could cross your text with a line and make it bold.
|
||||||
See [this](styling.md#Attributes) for more information.
|
See [this](styling.md#Attributes) for more information.
|
||||||
|
|
||||||
```
|
### Using Enum
|
||||||
let styled_object = style("'Red' text on 'White' background")
|
You could use the `Attribute` enum for styling text with attributes.
|
||||||
.attr(Attribute::CrossedOut)
|
`Attribute` implements `Display`, thus crossterm will enable the attribute style when using it in any writing operation.
|
||||||
.attr(Attribute::Bold);
|
|
||||||
|
|
||||||
println!("{}", styled_object);
|
|
||||||
```
|
```
|
||||||
|
println!(
|
||||||
|
"{} Underlined {} No Underline",
|
||||||
|
Attribute::Underlined,
|
||||||
|
Attribute::NoUnderline
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Using Method
|
||||||
|
|
||||||
|
You can do the same as the above in a slightly different way. Instead of enabling it for all text you could also style only one piece of text.
|
||||||
|
(Make sure to include the `crossterm::Styler` trait).
|
||||||
|
|
||||||
|
```
|
||||||
|
println!("{}", "Bold text".bold();
|
||||||
|
println!("{}", "Underlined text".underlined();
|
||||||
|
println!("{}", "Negative text".negative();
|
||||||
|
```
|
||||||
|
|
||||||
|
As you see in the above example you could call attributes methods on a string. How is this possible you might ask..?
|
||||||
|
Well, the trait `Styling`, who you need to include, is implemented for `&'static str`.
|
||||||
|
When calling a method on any string crossterm transforms will transform it into a `StyledObject` who you could use in your write statements.
|
||||||
|
|
||||||
---------------------------------------------------------------------------------------------------------------------------------------------
|
---------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
More examples could be found at this [link](https://github.com/TimonPost/crossterm/blob/master/examples/style.rs).
|
More examples could be found at this [link](https://github.com/TimonPost/crossterm/blob/master/examples/style.rs).
|
@ -230,9 +230,3 @@ pub fn print_supported_colors() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
|
||||||
print_all_background_colors();
|
|
||||||
print_all_foreground_colors();
|
|
||||||
print_font_with_attributes();
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user