Make Palette directly an EnumMap

Removes the "colors" intermediate member.
This commit is contained in:
Alexandre Bury 2018-01-21 11:06:33 -08:00
parent b0f4dfbc17
commit 431285135f
6 changed files with 88 additions and 75 deletions

View File

@ -30,7 +30,7 @@ fn custom_theme_from_cursive(siv: &Cursive) -> Theme {
// We'll return the current theme with a small modification. // We'll return the current theme with a small modification.
let mut theme = siv.current_theme().clone(); let mut theme = siv.current_theme().clone();
theme.palette.colors[PaletteColor::Background] = Color::TerminalDefault; theme.palette[PaletteColor::Background] = Color::TerminalDefault;
theme theme
} }

View File

@ -168,7 +168,7 @@ impl Cursive {
/// Users rarely have to call this directly. /// Users rarely have to call this directly.
pub fn clear(&self) { pub fn clear(&self) {
self.backend self.backend
.clear(self.theme.palette.colors[theme::PaletteColor::Background]); .clear(self.theme.palette[theme::PaletteColor::Background]);
} }
/// Loads a theme from the given file. /// Loads a theme from the given file.

View File

@ -51,7 +51,7 @@ impl<'a> Printer<'a> {
/// ///
/// Users rarely need to call this directly. /// Users rarely need to call this directly.
pub fn clear(&self) { pub fn clear(&self) {
self.backend.clear(self.theme.palette.colors[PaletteColor::Background]); self.backend.clear(self.theme.palette[PaletteColor::Background]);
} }
/// Returns `true` if nothing has been printed yet. /// Returns `true` if nothing has been printed yet.

View File

@ -58,9 +58,13 @@ pub enum Color {
TerminalDefault, TerminalDefault,
/// One of the 8 base colors. /// One of the 8 base colors.
///
/// Note: the actual color used depends on the terminal configuration.
Dark(BaseColor), Dark(BaseColor),
/// Lighter version of a base color. /// Lighter version of a base color.
///
/// Note: the actual color used depends on the terminal configuration.
Light(BaseColor), Light(BaseColor),
/// True-color, 24-bit. /// True-color, 24-bit.

View File

@ -167,7 +167,7 @@ pub use self::color::{BaseColor, Color};
pub use self::color_pair::ColorPair; pub use self::color_pair::ColorPair;
pub use self::color_style::{ColorStyle, ColorType}; pub use self::color_style::{ColorStyle, ColorType};
pub use self::effect::Effect; pub use self::effect::Effect;
pub use self::palette::{Palette, PaletteColor}; pub use self::palette::{Palette, PaletteColor, default_palette};
pub use self::style::Style; pub use self::style::Style;
use std::fs::File; use std::fs::File;
use std::io; use std::io;
@ -191,7 +191,7 @@ impl Default for Theme {
Theme { Theme {
shadow: true, shadow: true,
borders: BorderStyle::Simple, borders: BorderStyle::Simple,
palette: Palette::default(), palette: default_palette(),
} }
} }
} }
@ -207,7 +207,7 @@ impl Theme {
} }
if let Some(&toml::Value::Table(ref table)) = table.get("colors") { if let Some(&toml::Value::Table(ref table)) = table.get("colors") {
self.palette.load(table); palette::load_table(&mut self.palette, table);
} }
} }
} }

View File

@ -1,24 +1,46 @@
use super::Color; use super::Color;
use toml;
use enum_map::EnumMap; use enum_map::EnumMap;
use toml;
/// Color configuration for the application. /// Color configuration for the application.
/// ///
/// Assign each color role an actual color. /// Assign each color role an actual color.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] ///
pub struct Palette { /// It implements `Index` and `IndexMut` to access and modify this mapping:
/// Color map for this palette ///
pub colors: EnumMap<PaletteColor, Color>, /// # Example
} ///
/// ```rust
/// # use cursive::theme;
/// use cursive::theme::PaletteColor::*;
/// use cursive::theme::Color::*;
/// use cursive::theme::BaseColor::*;
///
/// let mut palette = theme::default_palette();
///
/// assert_eq!(palette[Background], Dark(Blue));
/// palette[Shadow] = Light(Red);
/// ```
pub type Palette = EnumMap<PaletteColor, Color>;
impl Default for Palette { /// Returns the default palette for a cursive application.
fn default() -> Self { ///
/// * `Background` => `Dark(Blue)`
/// * `Shadow` => `Dark(Black)`
/// * `View` => `Dark(White)`
/// * `Primary` => `Dark(Black)`
/// * `Secondary` => `Dark(Blue)`
/// * `Tertiary` => `Dark(White)`
/// * `TitlePrimary` => `Dark(Red)`
/// * `TitleSecondary` => `Dark(Yellow)`
/// * `Highlight` => `Dark(Red)`
/// * `HighlightInactive` => `Dark(Blue)`
pub fn default_palette() -> Palette {
use self::PaletteColor::*; use self::PaletteColor::*;
use theme::Color::*;
use theme::BaseColor::*; use theme::BaseColor::*;
use theme::Color::*;
Palette { enum_map!{
colors: enum_map!{
Background => Dark(Blue), Background => Dark(Blue),
Shadow => Dark(Black), Shadow => Dark(Black),
View => Dark(White), View => Dark(White),
@ -31,53 +53,40 @@ impl Default for Palette {
HighlightInactive => Dark(Blue), HighlightInactive => Dark(Blue),
} }
} }
}
}
impl Palette { /// Fills `palette` with the colors from the given `table`.
/// Fills `self` with the colors from the given `table`. pub(crate) fn load_table(palette: &mut Palette, table: &toml::value::Table) {
pub(crate) fn load(&mut self, table: &toml::value::Table) {
// TODO: use serde for that? // TODO: use serde for that?
// Problem: toml-rs doesn't do well with Enums... // Problem: toml-rs doesn't do well with Enums...
load_color( load_color(
&mut self.colors[PaletteColor::Background], &mut palette[PaletteColor::Background],
table.get("background"), table.get("background"),
); );
load_color(&mut palette[PaletteColor::Shadow], table.get("shadow"));
load_color(&mut palette[PaletteColor::View], table.get("view"));
load_color(&mut palette[PaletteColor::Primary], table.get("primary"));
load_color( load_color(
&mut self.colors[PaletteColor::Shadow], &mut palette[PaletteColor::Secondary],
table.get("shadow"),
);
load_color(&mut self.colors[PaletteColor::View], table.get("view"));
load_color(
&mut self.colors[PaletteColor::Primary],
table.get("primary"),
);
load_color(
&mut self.colors[PaletteColor::Secondary],
table.get("secondary"), table.get("secondary"),
); );
load_color(&mut palette[PaletteColor::Tertiary], table.get("tertiary"));
load_color( load_color(
&mut self.colors[PaletteColor::Tertiary], &mut palette[PaletteColor::TitlePrimary],
table.get("tertiary"),
);
load_color(
&mut self.colors[PaletteColor::TitlePrimary],
table.get("title_primary"), table.get("title_primary"),
); );
load_color( load_color(
&mut self.colors[PaletteColor::TitleSecondary], &mut palette[PaletteColor::TitleSecondary],
table.get("title_secondary"), table.get("title_secondary"),
); );
load_color( load_color(
&mut self.colors[PaletteColor::Highlight], &mut palette[PaletteColor::Highlight],
table.get("highlight"), table.get("highlight"),
); );
load_color( load_color(
&mut self.colors[PaletteColor::HighlightInactive], &mut palette[PaletteColor::HighlightInactive],
table.get("highlight_inactive"), table.get("highlight_inactive"),
); );
} }
}
/// Color entry in a palette. /// Color entry in a palette.
/// ///
@ -109,7 +118,7 @@ pub enum PaletteColor {
impl PaletteColor { impl PaletteColor {
/// Given a palette, resolve `self` to a concrete color. /// Given a palette, resolve `self` to a concrete color.
pub fn resolve(self, palette: &Palette) -> Color { pub fn resolve(self, palette: &Palette) -> Color {
palette.colors[self] palette[self]
} }
} }