use std::iter::FromIterator;
use super::{
Color, ColorPair, ColorStyle, ColorType, Effect, Palette, PaletteColor,
PaletteStyle,
};
use enumset::EnumSet;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Style {
pub effects: EnumSet<Effect>,
pub color: ColorStyle,
}
impl Default for Style {
fn default() -> Self {
Self::none()
}
}
impl Style {
pub fn none() -> Self {
Self::inherit_parent()
}
#[must_use]
pub fn merge(styles: &[Style]) -> Self {
styles.iter().collect()
}
#[must_use]
pub fn combine<S>(self, other: S) -> Self
where
S: Into<Style>,
{
Self::merge(&[self, other.into()])
}
pub fn inherit_parent() -> Self {
ColorStyle::inherit_parent().into()
}
pub fn terminal_default() -> Self {
ColorStyle::terminal_default().into()
}
pub fn background() -> Self {
ColorStyle::background().into()
}
pub fn shadow() -> Self {
ColorStyle::shadow().into()
}
pub fn view() -> Self {
ColorStyle::view().into()
}
pub fn primary() -> Self {
ColorStyle::primary().into()
}
pub fn secondary() -> Self {
ColorStyle::secondary().into()
}
pub fn tertiary() -> Self {
ColorStyle::tertiary().into()
}
pub fn title_primary() -> Self {
ColorStyle::title_primary().into()
}
pub fn title_secondary() -> Self {
ColorStyle::title_secondary().into()
}
pub fn highlight() -> Self {
Style {
color: ColorStyle::highlight().invert(),
effects: enumset::enum_set!(Effect::Reverse),
}
}
pub fn highlight_inactive() -> Self {
Style {
color: ColorStyle::highlight_inactive().invert(),
effects: enumset::enum_set!(Effect::Reverse),
}
}
#[cfg(feature = "toml")]
pub(crate) fn parse(table: &toml::Value) -> Option<Self> {
let table = table.as_table()?;
let mut effects: EnumSet<Effect> = EnumSet::new();
for effect in table.get("effects")?.as_array()? {
let effect = effect.as_str()?.parse().ok()?;
effects.insert(effect);
}
let color = ColorStyle::parse(table)?;
Some(Style { effects, color })
}
}
impl From<Effect> for Style {
fn from(effect: Effect) -> Self {
Style {
effects: EnumSet::only(effect),
color: ColorStyle::inherit_parent(),
}
}
}
impl From<ColorStyle> for Style {
fn from(color: ColorStyle) -> Self {
Style {
effects: EnumSet::new(),
color,
}
}
}
impl From<ColorPair> for Style {
fn from(color: ColorPair) -> Self {
ColorStyle::from(color).into()
}
}
impl From<Color> for Style {
fn from(color: Color) -> Self {
ColorStyle::from(color).into()
}
}
impl From<PaletteColor> for Style {
fn from(color: PaletteColor) -> Self {
ColorStyle::from(color).into()
}
}
impl From<ColorType> for Style {
fn from(color: ColorType) -> Self {
ColorStyle::from(color).into()
}
}
impl Default for StyleType {
fn default() -> Self {
Style::default().into()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum StyleType {
Palette(PaletteStyle),
Style(Style),
}
impl StyleType {
pub fn resolve(self, palette: &Palette) -> Style {
match self {
StyleType::Style(style) => style,
StyleType::Palette(style) => style.resolve(palette),
}
}
pub fn inherit_parent() -> Self {
Style::inherit_parent().into()
}
pub fn terminal_default() -> Self {
Style::terminal_default().into()
}
pub fn background() -> Self {
PaletteStyle::Background.into()
}
pub fn shadow() -> Self {
PaletteStyle::Shadow.into()
}
pub fn view() -> Self {
PaletteStyle::View.into()
}
pub fn primary() -> Self {
PaletteStyle::Primary.into()
}
pub fn secondary() -> Self {
PaletteStyle::Secondary.into()
}
pub fn tertiary() -> Self {
PaletteStyle::Tertiary.into()
}
pub fn title_primary() -> Self {
PaletteStyle::TitlePrimary.into()
}
pub fn title_secondary() -> Self {
PaletteStyle::TitleSecondary.into()
}
pub fn highlight() -> Self {
PaletteStyle::Highlight.into()
}
pub fn highlight_inactive() -> Self {
PaletteStyle::HighlightInactive.into()
}
}
impl From<Effect> for StyleType {
fn from(effect: Effect) -> Self {
StyleType::Style(effect.into())
}
}
impl From<ColorStyle> for StyleType {
fn from(color: ColorStyle) -> Self {
StyleType::Style(color.into())
}
}
impl From<ColorPair> for StyleType {
fn from(color: ColorPair) -> Self {
StyleType::Style(color.into())
}
}
impl From<Color> for StyleType {
fn from(color: Color) -> Self {
StyleType::Style(color.into())
}
}
impl From<PaletteColor> for StyleType {
fn from(color: PaletteColor) -> Self {
StyleType::Style(color.into())
}
}
impl From<ColorType> for StyleType {
fn from(color: ColorType) -> Self {
StyleType::Style(color.into())
}
}
impl From<Style> for StyleType {
fn from(style: Style) -> Self {
StyleType::Style(style)
}
}
impl From<PaletteStyle> for StyleType {
fn from(style: PaletteStyle) -> Self {
StyleType::Palette(style)
}
}
impl<'a> FromIterator<&'a Style> for Style {
fn from_iter<I: IntoIterator<Item = &'a Style>>(iter: I) -> Style {
let mut color = ColorStyle::inherit_parent();
let mut effects = EnumSet::new();
for style in iter {
color = ColorStyle::merge(color, style.color);
effects.insert_all(style.effects);
}
Style { effects, color }
}
}
impl<T: Into<Style>> FromIterator<T> for Style {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Style {
iter.into_iter().map(Into::into).collect()
}
}