diff --git a/cursive-core/src/printer.rs b/cursive-core/src/printer.rs index ab0c7ce..38e4edb 100644 --- a/cursive-core/src/printer.rs +++ b/cursive-core/src/printer.rs @@ -340,13 +340,9 @@ impl<'a, 'b> Printer<'a, 'b> { let color = style.color; let effects = style.effects; - if let Some(color) = color { - self.with_color(color, |printer| { - printer.with_effects(effects, f); - }); - } else { - self.with_effects(effects, f); - } + self.with_color(color, |printer| { + printer.with_effects(effects, f); + }); } /// Call the given closure with a modified printer @@ -496,7 +492,7 @@ impl<'a, 'b> Printer<'a, 'b> { ColorStyle::highlight_inactive() } } else { - ColorStyle::primary() + ColorStyle::inherit_parent() }, f, ); diff --git a/cursive-core/src/theme/color_style.rs b/cursive-core/src/theme/color_style.rs index a5e96de..899949e 100644 --- a/cursive-core/src/theme/color_style.rs +++ b/cursive-core/src/theme/color_style.rs @@ -5,7 +5,9 @@ use super::{BaseColor, Color, ColorPair, Palette, PaletteColor}; /// Represents a color pair role to use when printing something. /// /// The current theme will assign each role a foreground and background color. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +/// +/// The `Default` value is to inherit the parent's colors. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)] pub struct ColorStyle { /// Color used for the foreground (the text itself). pub front: ColorType, @@ -42,6 +44,11 @@ impl ColorStyle { Self::new(ColorType::InheritParent, back) } + /// Uses `ColorType::InheritParent` for both front and background. + pub fn inherit_parent() -> Self { + Self::new(ColorType::InheritParent, ColorType::InheritParent) + } + /// Style set by terminal before entering a Cursive program. pub fn terminal_default() -> Self { Self::new(Color::TerminalDefault, Color::TerminalDefault) @@ -92,6 +99,16 @@ impl ColorStyle { Self::new(PaletteColor::HighlightText, PaletteColor::HighlightInactive) } + /// Merge the style `b` over style `a`. + /// + /// This merges the front and back color types of `a` and `b`. + pub fn merge(a: Self, b: Self) -> Self { + ColorStyle { + front: ColorType::merge(a.front, b.front), + back: ColorType::merge(a.back, b.back), + } + } + /// Return the color pair that this style represents. pub fn resolve( &self, @@ -107,25 +124,25 @@ impl ColorStyle { impl From for ColorStyle { fn from(color: Color) -> Self { - Self::new(color, PaletteColor::View) + Self::front(color) } } impl From for ColorStyle { fn from(color: BaseColor) -> Self { - Self::new(Color::Dark(color), PaletteColor::View) + Self::front(Color::Dark(color)) } } impl From for ColorStyle { fn from(color: PaletteColor) -> Self { - Self::new(color, PaletteColor::View) + Self::front(color) } } impl From for ColorStyle { fn from(color: ColorType) -> Self { - Self::new(color, PaletteColor::View) + Self::front(color) } } @@ -140,6 +157,8 @@ where } /// Either a color from the palette, or a direct color. +/// +/// The `Default` implementation returns `InheritParent`. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum ColorType { /// Uses a color from the application palette. @@ -152,6 +171,12 @@ pub enum ColorType { InheritParent, } +impl Default for ColorType { + fn default() -> Self { + ColorType::InheritParent + } +} + impl ColorType { /// Given a palette, resolve `self` to a concrete color. pub fn resolve(self, palette: &Palette, previous: Color) -> Color { @@ -161,6 +186,17 @@ impl ColorType { ColorType::InheritParent => previous, } } + + /// Merge the color type `b` over the color type `a`. + /// + /// This returns `b`, unless `b = ColorType::InheritParent`, + /// in which case it returns `a`. + pub fn merge(a: ColorType, b: ColorType) -> ColorType { + match b { + ColorType::InheritParent => a, + b => b, + } + } } impl From for ColorType { diff --git a/cursive-core/src/theme/style.rs b/cursive-core/src/theme/style.rs index 97d1e3e..775412d 100644 --- a/cursive-core/src/theme/style.rs +++ b/cursive-core/src/theme/style.rs @@ -16,7 +16,7 @@ pub struct Style { /// Color style to apply. /// /// `None` to keep using the previous colors. - pub color: Option, + pub color: ColorStyle, } impl Default for Style { @@ -30,7 +30,7 @@ impl Style { pub fn none() -> Self { Style { effects: EnumSet::new(), - color: None, + color: ColorStyle::inherit_parent(), } } @@ -54,7 +54,7 @@ impl From for Style { fn from(effect: Effect) -> Self { Style { effects: enum_set!(effect), - color: None, + color: ColorStyle::inherit_parent(), } } } @@ -63,7 +63,7 @@ impl From for Style { fn from(color: ColorStyle) -> Self { Style { effects: EnumSet::new(), - color: Some(color), + color, } } } @@ -91,14 +91,11 @@ impl From for Style { /// Will use the last non-`None` color, and will combine all effects. impl<'a> FromIterator<&'a Style> for Style { fn from_iter>(iter: I) -> Style { - let mut color = None; + let mut color = ColorStyle::inherit_parent(); let mut effects = EnumSet::new(); for style in iter { - if style.color.is_some() { - color = style.color; - } - + color = ColorStyle::merge(color, style.color); effects.insert_all(style.effects); }