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

make Style methods const fns #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 44 additions & 44 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,19 @@ 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,
}
}

/// Returns a `Style` with the bold property set.
Expand All @@ -70,7 +81,7 @@ impl Style {
/// let style = Style::new().bold();
/// println!("{}", style.paint("hey"));
/// ```
pub fn bold(&self) -> Style {
pub const fn bold(&self) -> Style {
Style { is_bold: true, .. *self }
}

Expand All @@ -84,7 +95,7 @@ impl Style {
/// let style = Style::new().dimmed();
/// println!("{}", style.paint("sup"));
/// ```
pub fn dimmed(&self) -> Style {
pub const fn dimmed(&self) -> Style {
Style { is_dimmed: true, .. *self }
}

Expand All @@ -98,7 +109,7 @@ impl Style {
/// let style = Style::new().italic();
/// println!("{}", style.paint("greetings"));
/// ```
pub fn italic(&self) -> Style {
pub const fn italic(&self) -> Style {
Style { is_italic: true, .. *self }
}

Expand All @@ -112,7 +123,7 @@ impl Style {
/// let style = Style::new().underline();
/// println!("{}", style.paint("salutations"));
/// ```
pub fn underline(&self) -> Style {
pub const fn underline(&self) -> Style {
Style { is_underline: true, .. *self }
}

Expand All @@ -125,7 +136,7 @@ impl Style {
/// let style = Style::new().blink();
/// println!("{}", style.paint("wazzup"));
/// ```
pub fn blink(&self) -> Style {
pub const fn blink(&self) -> Style {
Style { is_blink: true, .. *self }
}

Expand All @@ -139,7 +150,7 @@ impl Style {
/// let style = Style::new().reverse();
/// println!("{}", style.paint("aloha"));
/// ```
pub fn reverse(&self) -> Style {
pub const fn reverse(&self) -> Style {
Style { is_reverse: true, .. *self }
}

Expand All @@ -153,7 +164,7 @@ impl Style {
/// let style = Style::new().hidden();
/// println!("{}", style.paint("ahoy"));
/// ```
pub fn hidden(&self) -> Style {
pub const fn hidden(&self) -> Style {
Style { is_hidden: true, .. *self }
}

Expand All @@ -167,7 +178,7 @@ impl Style {
/// let style = Style::new().strikethrough();
/// println!("{}", style.paint("yo"));
/// ```
pub fn strikethrough(&self) -> Style {
pub const fn strikethrough(&self) -> Style {
Style { is_strikethrough: true, .. *self }
}

Expand All @@ -181,7 +192,7 @@ impl Style {
/// let style = Style::new().fg(Colour::Yellow);
/// println!("{}", style.paint("hi"));
/// ```
pub fn fg(&self, foreground: Colour) -> Style {
pub const fn fg(&self, foreground: Colour) -> Style {
Style { foreground: Some(foreground), .. *self }
}

Expand All @@ -195,7 +206,7 @@ impl Style {
/// let style = Style::new().on(Colour::Blue);
/// println!("{}", style.paint("eyyyy"));
/// ```
pub fn on(&self, background: Colour) -> Style {
pub const fn on(&self, background: Colour) -> Style {
Style { background: Some(background), .. *self }
}

Expand Down Expand Up @@ -228,18 +239,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,
}
Style::new()
}
}

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

/// Returns a `Style` with the foreground colour set to this colour and the
Expand All @@ -335,8 +335,8 @@ impl Colour {
/// let style = Colour::Green.bold();
/// println!("{}", style.paint("hey"));
/// ```
pub fn bold(self) -> Style {
Style { foreground: Some(self), is_bold: true, .. Style::default() }
pub const fn bold(self) -> Style {
Style { foreground: Some(self), is_bold: true, .. Style::new() }
}

/// Returns a `Style` with the foreground colour set to this colour and the
Expand All @@ -350,8 +350,8 @@ impl Colour {
/// let style = Colour::Yellow.dimmed();
/// println!("{}", style.paint("sup"));
/// ```
pub fn dimmed(self) -> Style {
Style { foreground: Some(self), is_dimmed: true, .. Style::default() }
pub const fn dimmed(self) -> Style {
Style { foreground: Some(self), is_dimmed: true, .. Style::new() }
}

/// Returns a `Style` with the foreground colour set to this colour and the
Expand All @@ -365,8 +365,8 @@ impl Colour {
/// let style = Colour::Blue.italic();
/// println!("{}", style.paint("greetings"));
/// ```
pub fn italic(self) -> Style {
Style { foreground: Some(self), is_italic: true, .. Style::default() }
pub const fn italic(self) -> Style {
Style { foreground: Some(self), is_italic: true, .. Style::new() }
}

/// Returns a `Style` with the foreground colour set to this colour and the
Expand All @@ -380,8 +380,8 @@ impl Colour {
/// let style = Colour::Purple.underline();
/// println!("{}", style.paint("salutations"));
/// ```
pub fn underline(self) -> Style {
Style { foreground: Some(self), is_underline: true, .. Style::default() }
pub const fn underline(self) -> Style {
Style { foreground: Some(self), is_underline: true, .. Style::new() }
}

/// Returns a `Style` with the foreground colour set to this colour and the
Expand All @@ -395,8 +395,8 @@ impl Colour {
/// let style = Colour::Cyan.blink();
/// println!("{}", style.paint("wazzup"));
/// ```
pub fn blink(self) -> Style {
Style { foreground: Some(self), is_blink: true, .. Style::default() }
pub const fn blink(self) -> Style {
Style { foreground: Some(self), is_blink: true, .. Style::new() }
}

/// Returns a `Style` with the foreground colour set to this colour and the
Expand All @@ -410,8 +410,8 @@ impl Colour {
/// let style = Colour::Black.reverse();
/// println!("{}", style.paint("aloha"));
/// ```
pub fn reverse(self) -> Style {
Style { foreground: Some(self), is_reverse: true, .. Style::default() }
pub const fn reverse(self) -> Style {
Style { foreground: Some(self), is_reverse: true, .. Style::new() }
}

/// Returns a `Style` with the foreground colour set to this colour and the
Expand All @@ -425,8 +425,8 @@ impl Colour {
/// let style = Colour::White.hidden();
/// println!("{}", style.paint("ahoy"));
/// ```
pub fn hidden(self) -> Style {
Style { foreground: Some(self), is_hidden: true, .. Style::default() }
pub const fn hidden(self) -> Style {
Style { foreground: Some(self), is_hidden: true, .. Style::new() }
}

/// Returns a `Style` with the foreground colour set to this colour and the
Expand All @@ -440,8 +440,8 @@ impl Colour {
/// let style = Colour::Fixed(244).strikethrough();
/// println!("{}", style.paint("yo"));
/// ```
pub fn strikethrough(self) -> Style {
Style { foreground: Some(self), is_strikethrough: true, .. Style::default() }
pub const fn strikethrough(self) -> Style {
Style { foreground: Some(self), is_strikethrough: true, .. Style::new() }
}

/// Returns a `Style` with the foreground colour set to this colour and the
Expand All @@ -455,8 +455,8 @@ impl Colour {
/// let style = Colour::RGB(31, 31, 31).on(Colour::White);
/// println!("{}", style.paint("eyyyy"));
/// ```
pub fn on(self, background: Colour) -> Style {
Style { foreground: Some(self), background: Some(background), .. Style::default() }
pub const fn on(self, background: Colour) -> Style {
Style { foreground: Some(self), background: Some(background), .. Style::new() }
}
}

Expand Down