mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Use ColorType::InheritParent in more places
This commit is contained in:
parent
c3c3d4096f
commit
2b4ee4df4f
@ -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,
|
||||
);
|
||||
|
@ -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<Color> for ColorStyle {
|
||||
fn from(color: Color) -> Self {
|
||||
Self::new(color, PaletteColor::View)
|
||||
Self::front(color)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BaseColor> for ColorStyle {
|
||||
fn from(color: BaseColor) -> Self {
|
||||
Self::new(Color::Dark(color), PaletteColor::View)
|
||||
Self::front(Color::Dark(color))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PaletteColor> for ColorStyle {
|
||||
fn from(color: PaletteColor) -> Self {
|
||||
Self::new(color, PaletteColor::View)
|
||||
Self::front(color)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ColorType> 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<Color> for ColorType {
|
||||
|
@ -16,7 +16,7 @@ pub struct Style {
|
||||
/// Color style to apply.
|
||||
///
|
||||
/// `None` to keep using the previous colors.
|
||||
pub color: Option<ColorStyle>,
|
||||
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<Effect> for Style {
|
||||
fn from(effect: Effect) -> Self {
|
||||
Style {
|
||||
effects: enum_set!(effect),
|
||||
color: None,
|
||||
color: ColorStyle::inherit_parent(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -63,7 +63,7 @@ impl From<ColorStyle> for Style {
|
||||
fn from(color: ColorStyle) -> Self {
|
||||
Style {
|
||||
effects: EnumSet::new(),
|
||||
color: Some(color),
|
||||
color,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -91,14 +91,11 @@ impl From<ColorType> for Style {
|
||||
/// Will use the last non-`None` color, and will combine all effects.
|
||||
impl<'a> FromIterator<&'a Style> for Style {
|
||||
fn from_iter<I: IntoIterator<Item = &'a Style>>(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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user