From 6327166c2dafb8e7a20e635d0cddc3defb36cec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9lanie=20Chauvel?= Date: Thu, 14 Sep 2023 19:37:04 +0200 Subject: [PATCH] feat: add overlay function Extracted from: https://github.com/ogham/exa/blob/fb05c421ae98e076989eb6e8b1bcf42c07c1d0fe/src/theme/mod.rs#L339 --- src/style.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/style.rs b/src/style.rs index f79e0ce..42ab20f 100644 --- a/src/style.rs +++ b/src/style.rs @@ -199,6 +199,36 @@ impl Style { Style { background: Some(background), .. *self } } + /// Returns a `Style` with `overlay` applied on top of it. + /// + /// The properties from the `overlay` with non-default values (`Some(…)` or `true`) + /// override the values of `self`. + /// + /// # Examples + /// + /// ``` + /// use ansiterm::{Style, Colour}; + /// + /// let style = Style::new().fg(Colour::Blue).bold(); + /// let overlay = Style::new().fg(Colour::Red).underline(); + /// + /// assert_eq!(Style::new().fg(Colour::Red).bold().underline(), style.overlay(&overlay)); + /// ``` + pub fn overlay(&self, overlay: &Style) -> Style { + Style { + foreground: overlay.foreground.or(self.foreground), + background: overlay.background.or(self.background), + is_bold: overlay.is_bold || self.is_bold, + is_dimmed: overlay.is_dimmed || self.is_dimmed, + is_italic: overlay.is_bold || self.is_italic, + is_underline: overlay.is_underline || self.is_underline, + is_blink: overlay.is_blink || self.is_blink, + is_reverse: overlay.is_reverse || self.is_reverse, + is_hidden: overlay.is_hidden || self.is_hidden, + is_strikethrough: overlay.is_strikethrough || self.is_strikethrough, + } + } + /// Return true if this `Style` has no actual styles, and can be written /// without any control characters. ///