From 6d54991df836235ce59317bb18018a825c1f5363 Mon Sep 17 00:00:00 2001 From: Jezza Date: Sun, 16 Dec 2018 23:16:40 +0100 Subject: [PATCH] Implement support for some basic attributes for the windows terminal. --- examples/color/mod.rs | 9 +++++++++ src/modules/style/mod.rs | 15 ++++++++++++++- src/modules/style/objectstyle.rs | 5 ----- src/modules/style/styledobject.rs | 12 ++++++------ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/examples/color/mod.rs b/examples/color/mod.rs index 223bd5e65..5eec6e165 100644 --- a/examples/color/mod.rs +++ b/examples/color/mod.rs @@ -207,6 +207,15 @@ pub fn print_font_with_attributes() { println!("{}", style("Crossed out font").crossed_out()); } +/// Print font with all available attributes. Note that this can only be used at unix systems and that some are not supported widely | demonstration.. +#[cfg(windows)] +pub fn print_font_with_attributes() { + println!("{}", style("Normal text")); + println!("{}", style("Bold text").bold()); + println!("{}", style("Underlined text").underlined()); + println!("{}", style("Negative text").negative()); +} + /// Print all supported RGB colors | demonstration. #[cfg(unix)] pub fn print_supported_colors() { diff --git a/src/modules/style/mod.rs b/src/modules/style/mod.rs index 4b499b3bb..01ac9e14a 100644 --- a/src/modules/style/mod.rs +++ b/src/modules/style/mod.rs @@ -71,7 +71,8 @@ where ObjectStyle::new().apply_to(val) } -/// Attributes that could be applied on some text. +/// Attributes that could be applied on some text. (*nix values) +#[cfg(unix)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)] pub enum Attribute { Bold = 1, @@ -85,6 +86,18 @@ pub enum Attribute { CrossedOut = 9, } +/// Attributes that could be applied on some text. (Windows specific) +#[cfg(windows)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)] +pub enum Attribute { + Reset = 0, + Bold = 1, + Underlined = 4, + NoUnderline = 24, + Negative = 7, + Positive = 27, +} + /// Colors that are available for coloring the terminal font. #[derive(Debug, Copy, Clone)] pub enum Color { diff --git a/src/modules/style/objectstyle.rs b/src/modules/style/objectstyle.rs index dabcbd8e6..8179ddaa4 100644 --- a/src/modules/style/objectstyle.rs +++ b/src/modules/style/objectstyle.rs @@ -4,7 +4,6 @@ use super::{Color, StyledObject}; use std::fmt::Display; -#[cfg(unix)] use super::Attribute; /// Struct that contains the style properties that can be applied to an displayable object. @@ -13,7 +12,6 @@ pub struct ObjectStyle { pub fg_color: Option, pub bg_color: Option, - #[cfg(unix)] pub attrs: Vec, } @@ -22,7 +20,6 @@ impl Default for ObjectStyle { ObjectStyle { fg_color: Some(Color::White), bg_color: Some(Color::Black), - #[cfg(unix)] attrs: Vec::new(), } } @@ -42,7 +39,6 @@ impl ObjectStyle { ObjectStyle { fg_color: None, bg_color: None, - #[cfg(unix)] attrs: Vec::new(), } } @@ -59,7 +55,6 @@ impl ObjectStyle { self } - #[cfg(unix)] /// Add an `Attribute` to the current text. Like italic or bold. pub fn add_attr(&mut self, attr: Attribute) { self.attrs.push(attr); diff --git a/src/modules/style/styledobject.rs b/src/modules/style/styledobject.rs index 1a8caa6a9..bcf352883 100644 --- a/src/modules/style/styledobject.rs +++ b/src/modules/style/styledobject.rs @@ -6,7 +6,6 @@ use Screen; use std::fmt::{self, Display, Formatter}; use std::io::Write; -#[cfg(unix)] use super::Attribute; /// Struct that contains both the style and the content wits can be styled. @@ -72,14 +71,12 @@ impl<'a, D: Display + 'a> StyledObject { /// /// println!("{}", style("Some bold text").attr(Attribute::Bold); /// ``` - #[cfg(unix)] pub fn attr(mut self, attr: Attribute) -> StyledObject { self.object_style.add_attr(attr); self } /// Increase the font intensity. - #[cfg(unix)] #[inline(always)] pub fn bold(self) -> StyledObject { self.attr(Attribute::Bold) @@ -97,11 +94,16 @@ impl<'a, D: Display + 'a> StyledObject { self.attr(Attribute::Italic) } /// Underline font. - #[cfg(unix)] #[inline(always)] pub fn underlined(self) -> StyledObject { self.attr(Attribute::Underlined) } + /// Invert colours. + #[cfg(windows)] + #[inline(always)] + pub fn negative(self) -> StyledObject { + self.attr(Attribute::Negative) + } /// Slow Blink (less than 150 per minute; not widely supported). #[cfg(unix)] #[inline(always)] @@ -158,7 +160,6 @@ impl<'a, D: Display + 'a> StyledObject { reset = true; } - #[cfg(unix)] for attr in self.object_style.attrs.iter() { screen .stdout @@ -210,7 +211,6 @@ impl Display for StyledObject { reset = true; } - #[cfg(unix)] for attr in self.object_style.attrs.iter() { write!(f, "{}", format!(csi!("{}m"), *attr as i16)); reset = true;