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

Color constification #61

Closed
wants to merge 3 commits into from
Closed
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
78 changes: 39 additions & 39 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct Style {
/// Whether this style is hidden.
pub is_hidden: bool,

/// Whether this style is struckthrough.
/// Whether this style is strikethrough.
pub is_strikethrough: bool,

/// Wether this style is always displayed starting with a reset code to clear any remaining style artifacts
Expand All @@ -60,8 +60,20 @@ impl Style {
/// let style = Style::new();
/// println!("{}", style.paint("hi"));
/// ```
pub fn new() -> Style {
Style::default()
pub const fn new() -> Style {
Style {
foreground: None,
background: None,
is_bold: false,
is_dimmed: false,
is_italic: false,
is_underline: false,
is_blink: false,
is_reverse: false,
is_hidden: false,
is_strikethrough: false,
prefix_with_reset: false,
}
}

/// Returns a [`Style`] with the `Style.prefix_with_reset` property set.
Expand Down Expand Up @@ -262,7 +274,7 @@ impl Style {
/// assert_eq!(false, Style::default().bold().is_plain());
/// ```
pub fn is_plain(self) -> bool {
self == Style::default()
self == Style::new()
}
}

Expand All @@ -278,19 +290,7 @@ impl Default for Style {
/// assert_eq!("txt", Style::default().paint("txt").to_string());
/// ```
fn default() -> Style {
Style {
foreground: None,
background: None,
is_bold: false,
is_dimmed: false,
is_italic: false,
is_underline: false,
is_blink: false,
is_reverse: false,
is_hidden: false,
is_strikethrough: false,
prefix_with_reset: false,
}
Style::new()
}
}

Expand Down Expand Up @@ -404,10 +404,10 @@ impl Color {
/// let style = Color::Red.normal();
/// println!("{}", style.paint("hi"));
/// ```
pub fn normal(self) -> Style {
pub const fn normal(self) -> Style {
Style {
foreground: Some(self),
..Style::default()
..Style::new()
}
}

Expand All @@ -422,11 +422,11 @@ impl Color {
/// let style = Color::Green.bold();
/// println!("{}", style.paint("hey"));
/// ```
pub fn bold(self) -> Style {
pub const fn bold(self) -> Style {
Style {
foreground: Some(self),
is_bold: true,
..Style::default()
..Style::new()
}
}

Expand All @@ -441,11 +441,11 @@ impl Color {
/// let style = Color::Yellow.dimmed();
/// println!("{}", style.paint("sup"));
/// ```
pub fn dimmed(self) -> Style {
pub const fn dimmed(self) -> Style {
Style {
foreground: Some(self),
is_dimmed: true,
..Style::default()
..Style::new()
}
}

Expand All @@ -460,11 +460,11 @@ impl Color {
/// let style = Color::Blue.italic();
/// println!("{}", style.paint("greetings"));
/// ```
pub fn italic(self) -> Style {
pub const fn italic(self) -> Style {
Style {
foreground: Some(self),
is_italic: true,
..Style::default()
..Style::new()
}
}

Expand All @@ -479,11 +479,11 @@ impl Color {
/// let style = Color::Purple.underline();
/// println!("{}", style.paint("salutations"));
/// ```
pub fn underline(self) -> Style {
pub const fn underline(self) -> Style {
Style {
foreground: Some(self),
is_underline: true,
..Style::default()
..Style::new()
}
}

Expand All @@ -498,11 +498,11 @@ impl Color {
/// let style = Color::Cyan.blink();
/// println!("{}", style.paint("wazzup"));
/// ```
pub fn blink(self) -> Style {
pub const fn blink(self) -> Style {
Style {
foreground: Some(self),
is_blink: true,
..Style::default()
..Style::new()
}
}

Expand All @@ -517,11 +517,11 @@ impl Color {
/// let style = Color::Black.reverse();
/// println!("{}", style.paint("aloha"));
/// ```
pub fn reverse(self) -> Style {
pub const fn reverse(self) -> Style {
Style {
foreground: Some(self),
is_reverse: true,
..Style::default()
..Style::new()
}
}

Expand All @@ -536,11 +536,11 @@ impl Color {
/// let style = Color::White.hidden();
/// println!("{}", style.paint("ahoy"));
/// ```
pub fn hidden(self) -> Style {
pub const fn hidden(self) -> Style {
Style {
foreground: Some(self),
is_hidden: true,
..Style::default()
..Style::new()
}
}

Expand All @@ -555,11 +555,11 @@ impl Color {
/// let style = Color::Fixed(244).strikethrough();
/// println!("{}", style.paint("yo"));
/// ```
pub fn strikethrough(self) -> Style {
pub const fn strikethrough(self) -> Style {
Style {
foreground: Some(self),
is_strikethrough: true,
..Style::default()
..Style::new()
}
}

Expand All @@ -574,11 +574,11 @@ impl Color {
/// let style = Color::Fixed(244).reset_before_style();
/// println!("{}", style.paint("yo"));
/// ```
pub fn reset_before_style(self) -> Style {
pub const fn reset_before_style(self) -> Style {
Style {
foreground: Some(self),
prefix_with_reset: true,
..Style::default()
..Style::new()
}
}

Expand All @@ -593,11 +593,11 @@ impl Color {
/// let style = Color::Rgb(31, 31, 31).on(Color::White);
/// println!("{}", style.paint("eyyyy"));
/// ```
pub fn on(self, background: Color) -> Style {
pub const fn on(self, background: Color) -> Style {
Style {
foreground: Some(self),
background: Some(background),
..Style::default()
..Style::new()
}
}
}
Expand Down
Loading