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 Apr 23, 2022
1 parent ddb4a0c commit 46c0557
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,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 @@ -1633,6 +1639,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 @@ -1648,6 +1660,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 @@ -1663,6 +1683,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 @@ -1678,6 +1706,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 @@ -1693,6 +1729,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 reset is enabled or not.
///
/// reset is enabled by default. When disabled and using ANSI escape
Expand Down Expand Up @@ -1720,6 +1764,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 @@ -1745,6 +1797,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 @@ -2259,4 +2319,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 46c0557

Please sign in to comment.