Skip to content

Commit

Permalink
api: add strikethrough support to ColorSpec
Browse files Browse the repository at this point in the history
Closes #46, Closes #67
  • Loading branch information
tgross35 authored and BurntSushi committed Jan 15, 2023
1 parent 5b524b6 commit c13ddd4
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,9 @@ impl<W: io::Write> WriteColor for Ansi<W> {
if spec.underline {
self.write_str("\x1B[4m")?;
}
if spec.strikethrough {
self.write_str("\x1B[9m")?;
}
if let Some(ref c) = spec.fg_color {
self.write_color(true, c, spec.intense)?;
}
Expand Down Expand Up @@ -1640,6 +1643,7 @@ pub struct ColorSpec {
dimmed: bool,
italic: bool,
reset: bool,
strikethrough: bool,
}

impl Default for ColorSpec {
Expand All @@ -1653,6 +1657,7 @@ impl Default for ColorSpec {
dimmed: false,
italic: false,
reset: true,
strikethrough: false,
}
}
}
Expand Down Expand Up @@ -1745,6 +1750,21 @@ impl ColorSpec {
self
}

/// Get whether this is strikethrough or not.
///
/// Note that the strikethrough setting has no effect in a Windows console.
pub fn strikethrough(&self) -> bool {
self.strikethrough
}

/// Set whether the text is strikethrough or not.
///
/// Note that the strikethrough setting has no effect in a Windows console.
pub fn set_strikethrough(&mut self, yes: bool) -> &mut 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 @@ -1806,6 +1826,7 @@ impl ColorSpec {
&& !self.dimmed
&& !self.italic
&& !self.intense
&& !self.strikethrough
}

/// Clears this color specification so that it has no color/style settings.
Expand All @@ -1817,6 +1838,7 @@ impl ColorSpec {
self.intense = false;
self.dimmed = false;
self.italic = false;
self.strikethrough = false;
}

/// Writes this color spec to the given Windows console.
Expand Down Expand Up @@ -2271,16 +2293,19 @@ mod tests {
for underline in vec![false, true] {
for intense in vec![false, true] {
for italic in vec![false, true] {
for dimmed in vec![false, true] {
let mut color = ColorSpec::new();
color.set_fg(fg);
color.set_bg(bg);
color.set_bold(bold);
color.set_underline(underline);
color.set_intense(intense);
color.set_dimmed(dimmed);
color.set_italic(italic);
result.push(color);
for strikethrough in vec![false, true] {
for dimmed in vec![false, true] {
let mut color = ColorSpec::new();
color.set_fg(fg);
color.set_bg(bg);
color.set_bold(bold);
color.set_underline(underline);
color.set_intense(intense);
color.set_italic(italic);
color.set_dimmed(dimmed);
color.set_strikethrough(strikethrough);
result.push(color);
}
}
}
}
Expand Down

0 comments on commit c13ddd4

Please sign in to comment.