Skip to content

Commit

Permalink
Create the SetStyle tuple struct (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
AhoyISki committed Jul 21, 2022
1 parent 0c20590 commit 4dcc6fc
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/style.rs
Expand Up @@ -340,6 +340,34 @@ impl Command for SetAttributes {
}
}

/// A command that sets a style (colors and attributes).
///
/// # Notes
///
/// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetStyle(pub ContentStyle);

impl Command for SetStyle {
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {

if let Some(bg) = self.0.background_color {
execute_fmt(f, SetBackgroundColor(bg)).map_err(|_| fmt::Error)?;
}
if let Some(fg) = self.0.foreground_color {
execute_fmt(f, SetForegroundColor(fg)).map_err(|_| fmt::Error)?;
}
if let Some(ul) = self.0.underline_color {
execute_fmt(f, SetUnderlineColor(ul)).map_err(|_| fmt::Error)?;
}
if !self.0.attributes.is_empty() {
execute_fmt(f, SetAttributes(self.0.attributes)).map_err(|_| fmt::Error)?;
}

Ok(())
}
}

/// A command that prints styled content.
///
/// See [`StyledContent`](struct.StyledContent.html) for more info.
Expand All @@ -366,7 +394,6 @@ impl<D: Display> Command for PrintStyledContent<D> {
execute_fmt(f, SetForegroundColor(fg)).map_err(|_| fmt::Error)?;
reset_foreground = true;
}

if let Some(ul) = style.underline_color {
execute_fmt(f, SetUnderlineColor(ul)).map_err(|_| fmt::Error)?;
reset_foreground = true;
Expand Down

0 comments on commit 4dcc6fc

Please sign in to comment.