Skip to content

Commit

Permalink
Implement support attributes support for the windows terminal. (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jezza authored and TimonPost committed Dec 30, 2018
1 parent ddcda09 commit 14bd60a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
9 changes: 9 additions & 0 deletions examples/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
15 changes: 14 additions & 1 deletion src/modules/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions src/modules/style/objectstyle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -13,7 +12,6 @@ pub struct ObjectStyle {
pub fg_color: Option<Color>,
pub bg_color: Option<Color>,

#[cfg(unix)]
pub attrs: Vec<Attribute>,
}

Expand All @@ -22,7 +20,6 @@ impl Default for ObjectStyle {
ObjectStyle {
fg_color: Some(Color::White),
bg_color: Some(Color::Black),
#[cfg(unix)]
attrs: Vec::new(),
}
}
Expand All @@ -42,7 +39,6 @@ impl ObjectStyle {
ObjectStyle {
fg_color: None,
bg_color: None,
#[cfg(unix)]
attrs: Vec::new(),
}
}
Expand All @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/modules/style/styledobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -72,14 +71,12 @@ impl<'a, D: Display + 'a> StyledObject<D> {
///
/// println!("{}", style("Some bold text").attr(Attribute::Bold);
/// ```
#[cfg(unix)]
pub fn attr(mut self, attr: Attribute) -> StyledObject<D> {
self.object_style.add_attr(attr);
self
}

/// Increase the font intensity.
#[cfg(unix)]
#[inline(always)]
pub fn bold(self) -> StyledObject<D> {
self.attr(Attribute::Bold)
Expand All @@ -97,11 +94,16 @@ impl<'a, D: Display + 'a> StyledObject<D> {
self.attr(Attribute::Italic)
}
/// Underline font.
#[cfg(unix)]
#[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)]
Expand Down Expand Up @@ -158,7 +160,6 @@ impl<'a, D: Display + 'a> StyledObject<D> {
reset = true;
}

#[cfg(unix)]
for attr in self.object_style.attrs.iter() {
screen
.stdout
Expand Down Expand Up @@ -210,7 +211,6 @@ impl<D: Display> Display for StyledObject<D> {
reset = true;
}

#[cfg(unix)]
for attr in self.object_style.attrs.iter() {
write!(f, "{}", format!(csi!("{}m"), *attr as i16));
reset = true;
Expand Down

0 comments on commit 14bd60a

Please sign in to comment.