diff --git a/examples/terminal_default.rs b/examples/terminal_default.rs index 2d9304a..af1fb42 100644 --- a/examples/terminal_default.rs +++ b/examples/terminal_default.rs @@ -30,7 +30,7 @@ fn custom_theme_from_cursive(siv: &Cursive) -> Theme { // We'll return the current theme with a small modification. let mut theme = siv.current_theme().clone(); - theme.palette.colors[PaletteColor::Background] = Color::TerminalDefault; + theme.palette[PaletteColor::Background] = Color::TerminalDefault; theme } diff --git a/src/cursive.rs b/src/cursive.rs index 6ba61ea..e317498 100644 --- a/src/cursive.rs +++ b/src/cursive.rs @@ -168,7 +168,7 @@ impl Cursive { /// Users rarely have to call this directly. pub fn clear(&self) { self.backend - .clear(self.theme.palette.colors[theme::PaletteColor::Background]); + .clear(self.theme.palette[theme::PaletteColor::Background]); } /// Loads a theme from the given file. diff --git a/src/printer.rs b/src/printer.rs index b7ffe44..5f24c5f 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -51,7 +51,7 @@ impl<'a> Printer<'a> { /// /// Users rarely need to call this directly. 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. diff --git a/src/theme/color.rs b/src/theme/color.rs index 0c5b82a..6828f01 100644 --- a/src/theme/color.rs +++ b/src/theme/color.rs @@ -58,9 +58,13 @@ pub enum Color { TerminalDefault, /// One of the 8 base colors. + /// + /// Note: the actual color used depends on the terminal configuration. Dark(BaseColor), /// Lighter version of a base color. + /// + /// Note: the actual color used depends on the terminal configuration. Light(BaseColor), /// True-color, 24-bit. diff --git a/src/theme/mod.rs b/src/theme/mod.rs index bd0aad8..965be8a 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -167,7 +167,7 @@ pub use self::color::{BaseColor, Color}; pub use self::color_pair::ColorPair; pub use self::color_style::{ColorStyle, ColorType}; pub use self::effect::Effect; -pub use self::palette::{Palette, PaletteColor}; +pub use self::palette::{Palette, PaletteColor, default_palette}; pub use self::style::Style; use std::fs::File; use std::io; @@ -191,7 +191,7 @@ impl Default for Theme { Theme { shadow: true, 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") { - self.palette.load(table); + palette::load_table(&mut self.palette, table); } } } diff --git a/src/theme/palette.rs b/src/theme/palette.rs index b9cbd23..a2915d6 100644 --- a/src/theme/palette.rs +++ b/src/theme/palette.rs @@ -1,82 +1,91 @@ use super::Color; -use toml; use enum_map::EnumMap; +use toml; /// Color configuration for the application. /// /// Assign each color role an actual color. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct Palette { - /// Color map for this palette - pub colors: EnumMap, -} +/// +/// It implements `Index` and `IndexMut` to access and modify this mapping: +/// +/// # 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; -impl Default for Palette { - fn default() -> Self { - use self::PaletteColor::*; - use theme::Color::*; - use theme::BaseColor::*; +/// Returns the default palette for a cursive application. +/// +/// * `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 theme::BaseColor::*; + use theme::Color::*; - Palette { - colors: enum_map!{ - 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), - } - } + enum_map!{ + 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), } } -impl Palette { - /// Fills `self` with the colors from the given `table`. - pub(crate) fn load(&mut self, table: &toml::value::Table) { - // TODO: use serde for that? - // Problem: toml-rs doesn't do well with Enums... - load_color( - &mut self.colors[PaletteColor::Background], - table.get("background"), - ); - load_color( - &mut self.colors[PaletteColor::Shadow], - 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"), - ); - load_color( - &mut self.colors[PaletteColor::Tertiary], - table.get("tertiary"), - ); - load_color( - &mut self.colors[PaletteColor::TitlePrimary], - table.get("title_primary"), - ); - load_color( - &mut self.colors[PaletteColor::TitleSecondary], - table.get("title_secondary"), - ); - load_color( - &mut self.colors[PaletteColor::Highlight], - table.get("highlight"), - ); - load_color( - &mut self.colors[PaletteColor::HighlightInactive], - table.get("highlight_inactive"), - ); - } +/// Fills `palette` with the colors from the given `table`. +pub(crate) fn load_table(palette: &mut Palette, table: &toml::value::Table) { + // TODO: use serde for that? + // Problem: toml-rs doesn't do well with Enums... + load_color( + &mut palette[PaletteColor::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( + &mut palette[PaletteColor::Secondary], + table.get("secondary"), + ); + load_color(&mut palette[PaletteColor::Tertiary], table.get("tertiary")); + load_color( + &mut palette[PaletteColor::TitlePrimary], + table.get("title_primary"), + ); + load_color( + &mut palette[PaletteColor::TitleSecondary], + table.get("title_secondary"), + ); + load_color( + &mut palette[PaletteColor::Highlight], + table.get("highlight"), + ); + load_color( + &mut palette[PaletteColor::HighlightInactive], + table.get("highlight_inactive"), + ); } /// Color entry in a palette. @@ -109,7 +118,7 @@ pub enum PaletteColor { impl PaletteColor { /// Given a palette, resolve `self` to a concrete color. pub fn resolve(self, palette: &Palette) -> Color { - palette.colors[self] + palette[self] } }