Added resetting color support

This commit is contained in:
Timon_Post 2019-05-04 12:29:30 +02:00
parent 0bfebb338d
commit 51c0ab6012
8 changed files with 98 additions and 64 deletions

View File

@ -387,3 +387,11 @@ pub fn print_supported_colors() {
println!("Test {}", Colored::Bg(Color::AnsiValue(i as u8))); println!("Test {}", Colored::Bg(Color::AnsiValue(i as u8)));
} }
} }
fn main() {
println!("{}", Colored::Bg(Color::Red));
println!("{}", Colored::Fg(Color::Blue));
println!("{}", Colored::Fg(Color::Reset));
println!("{}", Colored::Bg(Color::Reset));
}

View File

@ -45,14 +45,24 @@ impl ITerminalColor for AnsiColor {
match colored { match colored {
Colored::Fg(new_color) => { Colored::Fg(new_color) => {
if new_color == Color::Reset {
ansi_value.push_str("39");
return ansi_value;
}else {
ansi_value.push_str("38;"); ansi_value.push_str("38;");
color = new_color; color = new_color;
} }
}
Colored::Bg(new_color) => { Colored::Bg(new_color) => {
if new_color == Color::Reset {
ansi_value.push_str("49");
return ansi_value;
}else {
ansi_value.push_str("48;"); ansi_value.push_str("48;");
color = new_color; color = new_color;
} }
} }
}
let rgb_val: String; let rgb_val: String;
@ -73,7 +83,6 @@ impl ITerminalColor for AnsiColor {
Color::DarkCyan => "5;6", Color::DarkCyan => "5;6",
Color::White => "5;15", Color::White => "5;15",
Color::Grey => "5;7", Color::Grey => "5;7",
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()
@ -82,6 +91,7 @@ impl ITerminalColor for AnsiColor {
rgb_val = format!("5;{}", val); rgb_val = format!("5;{}", val);
rgb_val.as_str() rgb_val.as_str()
} }
_ => ""
}; };
ansi_value.push_str(color_val); ansi_value.push_str(color_val);

View File

@ -31,16 +31,17 @@ pub struct TerminalColor {
impl TerminalColor { impl TerminalColor {
/// Create new instance whereon color related actions can be performed. /// Create new instance whereon color related actions can be performed.
pub fn new() -> TerminalColor { pub fn new() -> TerminalColor {
#[cfg(windows)] // #[cfg(windows)]
let color = if supports_ansi() { // let color = if supports_ansi() {
Box::from(AnsiColor::new()) as Box<(dyn ITerminalColor + Sync + Send)> // Box::from(AnsiColor::new()) as Box<(dyn ITerminalColor + Sync + Send)>
} else { // } else {
WinApiColor::new() as Box<(dyn ITerminalColor + Sync + Send)> // WinApiColor::new() as Box<(dyn ITerminalColor + Sync + Send)>
}; // };
//
#[cfg(unix)] // #[cfg(unix)]
let color = AnsiColor::new(); // let color = AnsiColor::new();
let color = WinApiColor::new() as Box<(dyn ITerminalColor + Sync + Send)>;
TerminalColor { color } TerminalColor { color }
} }

View File

@ -112,14 +112,6 @@ pub enum Attribute {
/// [info]: Opposite of `CrossedOut`(9) /// [info]: Opposite of `CrossedOut`(9)
/// [Supportability]: Not widely supported /// [Supportability]: Not widely supported
NotCrossedOut = 29, 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. /// Framed font.
/// [Supportability]: Not widely supported /// [Supportability]: Not widely supported
Framed = 51, Framed = 51,

View File

@ -2,8 +2,11 @@ use std::convert::AsRef;
use std::str::FromStr; use std::str::FromStr;
/// Colors that are available for coloring the terminal font. /// Colors that are available for coloring the terminal font.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
pub enum Color { pub enum Color {
// This resets the color.
Reset,
Black, Black,
DarkGrey, DarkGrey,

View File

@ -8,6 +8,16 @@ use std::io;
use std::sync::{Once, ONCE_INIT}; use std::sync::{Once, ONCE_INIT};
use winapi::um::wincon; use winapi::um::wincon;
const FG_GREEN: u16 = wincon::FOREGROUND_GREEN;
const FG_RED: u16 = wincon::FOREGROUND_RED;
const FG_BLUE: u16 = wincon::FOREGROUND_BLUE;
const FG_INTENSITY: u16 = wincon::FOREGROUND_INTENSITY;
const BG_GREEN: u16 = wincon::BACKGROUND_GREEN;
const BG_RED: u16 = wincon::BACKGROUND_RED;
const BG_BLUE: u16 = wincon::BACKGROUND_BLUE;
const BG_INTENSITY: u16 = wincon::BACKGROUND_INTENSITY;
/// This struct is a WinApi implementation for color related actions. /// This struct is a WinApi implementation for color related actions.
pub struct WinApiColor; pub struct WinApiColor;
@ -85,35 +95,35 @@ impl ITerminalColor for WinApiColor {
fn color_value(&self, color: Colored) -> String { fn color_value(&self, color: Colored) -> String {
let winapi_color: u16; let winapi_color: u16;
let fg_green = wincon::FOREGROUND_GREEN;
let fg_red = wincon::FOREGROUND_RED;
let fg_blue = wincon::FOREGROUND_BLUE;
let fg_intensity = wincon::FOREGROUND_INTENSITY;
let bg_green = wincon::BACKGROUND_GREEN;
let bg_red = wincon::BACKGROUND_RED;
let bg_blue = wincon::BACKGROUND_BLUE;
let bg_intensity = wincon::BACKGROUND_INTENSITY;
match color { match color {
Colored::Fg(color) => { Colored::Fg(color) => {
winapi_color = match color { winapi_color = match color {
Color::Black => 0, Color::Black => 0,
Color::DarkGrey => fg_intensity, Color::DarkGrey => FG_INTENSITY,
Color::Red => fg_intensity | fg_red, Color::Red => FG_INTENSITY | FG_RED,
Color::DarkRed => fg_red, Color::DarkRed => FG_RED,
Color::Green => fg_intensity | fg_green, Color::Green => FG_INTENSITY | FG_GREEN,
Color::DarkGreen => fg_green, Color::DarkGreen => FG_GREEN,
Color::Yellow => fg_intensity | fg_green | fg_red, Color::Yellow => FG_INTENSITY | FG_GREEN | FG_RED,
Color::DarkYellow => fg_green | fg_red, Color::DarkYellow => FG_GREEN | FG_RED,
Color::Blue => fg_intensity | fg_blue, Color::Blue => FG_INTENSITY | FG_BLUE,
Color::DarkBlue => fg_blue, Color::DarkBlue => FG_BLUE,
Color::Magenta => fg_intensity | fg_red | fg_blue, Color::Magenta => FG_INTENSITY | FG_RED | FG_BLUE,
Color::DarkMagenta => fg_red | fg_blue, Color::DarkMagenta => FG_RED | FG_BLUE,
Color::Cyan => fg_intensity | fg_green | fg_blue, Color::Cyan => FG_INTENSITY | FG_GREEN | FG_BLUE,
Color::DarkCyan => fg_green | fg_blue, Color::DarkCyan => FG_GREEN | FG_BLUE,
Color::White => fg_red | fg_green | fg_blue, Color::White => FG_RED | FG_GREEN | FG_BLUE,
Color::Grey => fg_intensity | fg_red | fg_green | fg_blue, Color::Grey => FG_INTENSITY | FG_RED | FG_GREEN | FG_BLUE,
Color::Reset => {
// init the original color in case it is not set.
let mut original_color = original_console_color();
const mask: u16 = BG_INTENSITY | BG_RED | BG_GREEN | BG_BLUE;
original_color &= !(mask);
original_color
}
/* 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.*/ /* 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::Rgb { r: _, g: _, b: _ } => 0,
@ -123,22 +133,30 @@ impl ITerminalColor for WinApiColor {
Colored::Bg(color) => { Colored::Bg(color) => {
winapi_color = match color { winapi_color = match color {
Color::Black => 0, Color::Black => 0,
Color::DarkGrey => bg_intensity, Color::DarkGrey => BG_INTENSITY,
Color::Red => bg_intensity | bg_red, Color::Red => BG_INTENSITY | BG_RED,
Color::DarkRed => bg_red, Color::DarkRed => BG_RED,
Color::Green => bg_intensity | bg_green, Color::Green => BG_INTENSITY | BG_GREEN,
Color::DarkGreen => bg_green, Color::DarkGreen => BG_GREEN,
Color::Yellow => bg_intensity | bg_green | bg_red, Color::Yellow => BG_INTENSITY | BG_GREEN | BG_RED,
Color::DarkYellow => bg_green | bg_red, Color::DarkYellow => BG_GREEN | BG_RED,
Color::Blue => bg_intensity | bg_blue, Color::Blue => BG_INTENSITY | BG_BLUE,
Color::DarkBlue => bg_blue, Color::DarkBlue => BG_BLUE,
Color::Magenta => bg_intensity | bg_red | bg_blue, Color::Magenta => BG_INTENSITY | BG_RED | BG_BLUE,
Color::DarkMagenta => bg_red | bg_blue, Color::DarkMagenta => BG_RED | BG_BLUE,
Color::Cyan => bg_intensity | bg_green | bg_blue, Color::Cyan => BG_INTENSITY | BG_GREEN | BG_BLUE,
Color::DarkCyan => bg_green | bg_blue, Color::DarkCyan => BG_GREEN | BG_BLUE,
Color::White => bg_intensity | bg_red | bg_green | bg_blue, Color::White => BG_INTENSITY | BG_RED | BG_GREEN | BG_BLUE,
Color::Grey => bg_red | bg_green | bg_blue, Color::Grey => BG_RED | BG_GREEN | BG_BLUE,
Color::Reset => {
// init the original color in case it is not set.
let mut original_color = original_console_color();
const mask: u16 = FG_INTENSITY | FG_RED | FG_GREEN | FG_BLUE;
original_color &= !(mask);
original_color
},
/* 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.*/ /* 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::Rgb { r: _, g: _, b: _ } => 0,
Color::AnsiValue(_val) => 0, Color::AnsiValue(_val) => 0,

View File

@ -3,6 +3,8 @@ And I don't think it has to do anything with crossterm but it has to do whit how
# Winapi-problems # Winapi-problems
- Power shell does not interpreter 'DarkYellow' and is instead using gray instead, cmd is working perfectly fine. - Power shell does not interpreter 'DarkYellow' and is instead using gray instead, cmd is working perfectly fine.
- Power shell inserts an '\n' (enter) when the program starts, this enter is the one you pressed when running the command.
- After the program ran, power shell will reset the background color.
# UNIX-terminals # UNIX-terminals
The Arc and Manjaro KDE Konsole's are not seeming to resize the terminal instead they are resizing the buffer. The Arc and Manjaro KDE Konsole's are not seeming to resize the terminal instead they are resizing the buffer.

View File

@ -146,5 +146,5 @@ pub fn exit() {
} }
fn main() { fn main() {
print_terminal_size(); clear_until_new_line();
} }