RGB and ANSI color for windows 10 suppport. (#51)

This commit is contained in:
Timon 2018-11-25 05:46:08 -08:00 committed by GitHub
parent 4d2fba2c0d
commit ee782cc7a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 23 additions and 18 deletions

View File

@ -92,7 +92,8 @@ These are the features from this crate:
- Styled output - Styled output
- Foreground color (16 base colors) - Foreground color (16 base colors)
- Background color (16 base colors) - Background color (16 base colors)
- 256 color support (unix only) - 256 color support (Windows 10 and UNIX only)
- RGB support (Windows 10 and UNIX only)
- Text Attributes like: bold, italic, underscore and crossed word ect (unix only) - Text Attributes like: bold, italic, underscore and crossed word ect (unix only)
- Custom ANSI color code input to set fore- and background color (unix only) - Custom ANSI color code input to set fore- and background color (unix only)
- Terminal - Terminal

View File

@ -1,7 +1,9 @@
# Changes crossterm 0.5.0 # Changes crossterm 0.5.0
- Implemented Display for styled object.
- More convenient API, no need to care about `Screen` unless working with when working with alternate or raw screen [PR](https://github.com/TimonPost/crossterm/pull/44)
- Added ability to pause the terminal [issue](https://github.com/TimonPost/crossterm/issues/39) - Added ability to pause the terminal [issue](https://github.com/TimonPost/crossterm/issues/39)
- RGB support for Windows 10 systems
- ANSI color value (255) color support
- More convenient API, no need to care about `Screen` unless working with when working with alternate or raw screen [PR](https://github.com/TimonPost/crossterm/pull/44)
- Implemented Display for styled object
# Changes crossterm to 0.4.3 # Changes crossterm to 0.4.3
- Fixed bug [issue 41](https://github.com/TimonPost/crossterm/issues/41) - Fixed bug [issue 41](https://github.com/TimonPost/crossterm/issues/41)

View File

@ -17,12 +17,11 @@ 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 also support more colors. In addition to 16 colours, most UNIX terminals and Windows 10 consoles are also supporting more colors.
For example, GNOME-terminals are supporting the [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) for setting the terminal color. 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.
All xterm terminals are at least supporting the [256 (Xterm, 8-bit)](https://jonasjacek.github.io/colors/) colors.
## Attributes ## Attributes
UNIX terminals are supporting attributes on top of text. Crossterm allows you to add attributes to the text. Only UNIX terminals are supporting attributes on top of 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.
| Attribute | Note | | Attribute | Note |

View File

@ -38,7 +38,7 @@ When running the above code you are supposed to see colored text with foreground
_note: you don't have to color both backround an foreground, if not specified they remain as they are_. _note: you don't have to color both backround an foreground, if not specified they remain as they are_.
### RGB ### RGB
Most UNIX terminals 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)`.
``` ```
@ -49,7 +49,7 @@ let styled_object = style("'Light green' text on 'Black' background")
This will print some light green text on black background. This will print some light green text on black background.
### Custom ANSI color value ### Custom ANSI color value
When working on unix you could also specify a custom ANSI value ranging up from 0 to 256. When working on UNIX or Windows 10 you could also specify a custom ANSI value ranging up from 0 to 256.
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.
``` ```
@ -62,7 +62,7 @@ println!("{}", styled_object);
``` ```
## Attributes ## Attributes
When working in Linux 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 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 [above](styling.md#Attributes) for more information. See [above](styling.md#Attributes) for more information.
``` ```

View File

@ -18,8 +18,8 @@ use crossterm::style::{style, Color, Attribute};
fn main() { fn main() {
let styled_object = style("'Red' text on 'White' background") let styled_object = style("'Red' text on 'White' background")
.with(Color::AnsiValue(9)) .with(Color::Rgb { r: 34, g: 80, b: 23 })
.on(Color::AnsiValue(15)); .on(Color::Rgb { r: 34, g: 80, b: 23 });
println!("{}", styled_object); println!("{}", styled_object);
} }

View File

@ -45,7 +45,6 @@ impl ITerminalColor for AnsiColor {
ColorType::Background => ansi_value.push_str("48;"), ColorType::Background => ansi_value.push_str("48;"),
} }
#[cfg(unix)]
let rgb_val: String; let rgb_val: String;
let color_val = match color { let color_val = match color {
@ -64,12 +63,11 @@ impl ITerminalColor for AnsiColor {
Color::DarkCyan => "5;6", Color::DarkCyan => "5;6",
Color::Grey => "5;15", Color::Grey => "5;15",
Color::White => "5;7", Color::White => "5;7",
#[cfg(unix)]
Color::Rgb { r, g, b } => { Color::Rgb { r, g, b } => {
rgb_val = format!("2;{};{};{}", r, g, b); rgb_val = format!("2;{};{};{}", r, g, b);
rgb_val.as_str() rgb_val.as_str()
} }
#[cfg(unix)]
Color::AnsiValue(val) => { Color::AnsiValue(val) => {
rgb_val = format!("5;{}", val); rgb_val = format!("5;{}", val);
rgb_val.as_str() rgb_val.as_str()

View File

@ -110,13 +110,11 @@ pub enum Color {
Grey, Grey,
White, White,
#[cfg(unix)]
Rgb { Rgb {
r: u8, r: u8,
g: u8, g: u8,
b: u8, b: u8,
}, },
#[cfg(unix)]
AnsiValue(u8), AnsiValue(u8),
} }

View File

@ -220,7 +220,6 @@ impl<D: Display> Display for StyledObject<D> {
std::io::stdout().flush().expect("Flush stdout failed"); std::io::stdout().flush().expect("Flush stdout failed");
if reset { if reset {
// write!(f, "\x1b[0m")?;
colored_terminal.reset(); colored_terminal.reset();
std::io::stdout().flush().expect("Flush stdout failed"); std::io::stdout().flush().expect("Flush stdout failed");
} }

View File

@ -101,6 +101,10 @@ impl ITerminalColor for WinApiColor {
Color::DarkCyan => fg_green | fg_blue, Color::DarkCyan => fg_green | fg_blue,
Color::Grey => fg_intensity, Color::Grey => fg_intensity,
Color::White => fg_intensity | fg_red | fg_green | fg_blue, Color::White => fg_intensity | fg_red | fg_green | fg_blue,
/* WinApi will be used for systems that do not support ANSI, those are windows version less then 10. RGB and 255 (AnsiBValue) colors are not supported in that case.*/
Color::Rgb{ r, g, b } => { 0 }
Color::AnsiValue(val) => { 0 }
}; };
} }
ColorType::Background => { ColorType::Background => {
@ -120,6 +124,10 @@ impl ITerminalColor for WinApiColor {
Color::DarkCyan => bg_green | bg_blue, Color::DarkCyan => bg_green | bg_blue,
Color::Grey => bg_intensity, Color::Grey => bg_intensity,
Color::White => bg_intensity | bg_red | bg_green | bg_blue, Color::White => bg_intensity | bg_red | bg_green | bg_blue,
/* WinApi will be used for systems that do not support ANSI, those are windows version less then 10. RGB and 255 (AnsiBValue) colors are not supported in that case.*/
Color::Rgb{ r, g, b } => { 0 }
Color::AnsiValue(val) => { 0 }
}; };
} }
}; };