mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Make Palette directly an EnumMap
Removes the "colors" intermediate member.
This commit is contained in:
parent
b0f4dfbc17
commit
431285135f
@ -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
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,46 @@
|
||||
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<PaletteColor, Color>,
|
||||
}
|
||||
///
|
||||
/// 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<PaletteColor, Color>;
|
||||
|
||||
impl Default for Palette {
|
||||
fn default() -> Self {
|
||||
/// 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::Color::*;
|
||||
use theme::BaseColor::*;
|
||||
use theme::Color::*;
|
||||
|
||||
Palette {
|
||||
colors: enum_map!{
|
||||
enum_map!{
|
||||
Background => Dark(Blue),
|
||||
Shadow => Dark(Black),
|
||||
View => Dark(White),
|
||||
@ -30,53 +52,40 @@ impl Default for Palette {
|
||||
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) {
|
||||
/// 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 self.colors[PaletteColor::Background],
|
||||
&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 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],
|
||||
&mut palette[PaletteColor::Secondary],
|
||||
table.get("secondary"),
|
||||
);
|
||||
load_color(&mut palette[PaletteColor::Tertiary], table.get("tertiary"));
|
||||
load_color(
|
||||
&mut self.colors[PaletteColor::Tertiary],
|
||||
table.get("tertiary"),
|
||||
);
|
||||
load_color(
|
||||
&mut self.colors[PaletteColor::TitlePrimary],
|
||||
&mut palette[PaletteColor::TitlePrimary],
|
||||
table.get("title_primary"),
|
||||
);
|
||||
load_color(
|
||||
&mut self.colors[PaletteColor::TitleSecondary],
|
||||
&mut palette[PaletteColor::TitleSecondary],
|
||||
table.get("title_secondary"),
|
||||
);
|
||||
load_color(
|
||||
&mut self.colors[PaletteColor::Highlight],
|
||||
&mut palette[PaletteColor::Highlight],
|
||||
table.get("highlight"),
|
||||
);
|
||||
load_color(
|
||||
&mut self.colors[PaletteColor::HighlightInactive],
|
||||
&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]
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user