Skip to content

Commit

Permalink
Add const 'with_' setters to ColorSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
Property404 committed Jan 16, 2023
1 parent 4458887 commit 34b6100
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,12 @@ impl ColorSpec {
self
}

/// Set the foreground color.
pub const fn with_fg(mut self, color: Option<Color>) -> ColorSpec {
self.fg_color = color;
self
}

/// Get the background color.
// TODO: Make this const when MSVR is 1.48
pub fn bg(&self) -> Option<&Color> {
Expand All @@ -1702,6 +1708,12 @@ impl ColorSpec {
self
}

/// Set the background color.
pub const fn with_bg(mut self, color: Option<Color>) -> ColorSpec {
self.bg_color = color;
self
}

/// Get whether this is bold or not.
///
/// Note that the bold setting has no effect in a Windows console.
Expand All @@ -1717,6 +1729,14 @@ impl ColorSpec {
self
}

/// Set whether the text is bolded or not.
///
/// Note that the bold setting has no effect in a Windows console.
pub const fn with_bold(mut self, yes: bool) -> ColorSpec {
self.bold = yes;
self
}

/// Get whether this is dimmed or not.
///
/// Note that the dimmed setting has no effect in a Windows console.
Expand All @@ -1732,6 +1752,14 @@ impl ColorSpec {
self
}

/// Set whether the text is dimmed or not.
///
/// Note that the dimmed setting has no effect in a Windows console.
pub const fn with_dimmed(mut self, yes: bool) -> ColorSpec {
self.dimmed = yes;
self
}

/// Get whether this is italic or not.
///
/// Note that the italic setting has no effect in a Windows console.
Expand All @@ -1747,6 +1775,14 @@ impl ColorSpec {
self
}

/// Set whether the text is italicized or not.
///
/// Note that the italic setting has no effect in a Windows console.
pub const fn with_italic(mut self, yes: bool) -> ColorSpec {
self.italic = yes;
self
}

/// Get whether this is underline or not.
///
/// Note that the underline setting has no effect in a Windows console.
Expand All @@ -1762,6 +1798,14 @@ impl ColorSpec {
self
}

/// Set whether the text is underlined or not.
///
/// Note that the underline setting has no effect in a Windows console.
pub const fn with_underline(mut self, yes: bool) -> ColorSpec {
self.underline = yes;
self
}

/// Get whether this is strikethrough or not.
///
/// Note that the strikethrough setting has no effect in a Windows console.
Expand All @@ -1777,6 +1821,14 @@ impl ColorSpec {
self
}

/// Set whether the text is strikethrough or not.
///
/// Note that the strikethrough setting has no effect in a Windows console.
pub const fn with_strikethrough(mut self, yes: bool) -> ColorSpec {
self.strikethrough = yes;
self
}

/// Get whether reset is enabled or not.
///
/// reset is enabled by default. When disabled and using ANSI escape
Expand Down Expand Up @@ -1804,6 +1856,14 @@ impl ColorSpec {
self
}

/// Set whether to reset the terminal whenever color settings are applied.
///
/// See [ColorSpec::set_reset].
pub const fn with_reset(mut self, yes: bool) -> ColorSpec {
self.reset = yes;
self
}

/// Get whether this is intense or not.
///
/// On Unix-like systems, this will output the ANSI escape sequence
Expand All @@ -1829,6 +1889,14 @@ impl ColorSpec {
self
}

/// Set whether the text is intense or not.
///
/// See [ColorSpec::set_intense].
pub const fn with_intense(mut self, yes: bool) -> ColorSpec {
self.intense = yes;
self
}

/// Returns true if this color specification has no colors or styles.
pub fn is_none(&self) -> bool {
self.fg_color.is_none()
Expand Down Expand Up @@ -2349,4 +2417,16 @@ mod tests {
assert!(color1.is_none(), "{:?} => {:?}", color, color1);
}
}

#[test]
fn const_colorspec() {
const BOLD_RED: ColorSpec = ColorSpec::new()
.with_fg(Some(Color::Red))
.with_bold(true)
.with_intense(true);
const IS_BOLD: bool = BOLD_RED.bold();
const IS_INTENSE: bool = BOLD_RED.intense();
assert!(IS_BOLD);
assert!(IS_INTENSE);
}
}

0 comments on commit 34b6100

Please sign in to comment.