Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for some basic attributes for the windows terminal. #62

Merged
merged 1 commit into from
Dec 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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