mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Add PaletteColor: FromStr
This commit is contained in:
parent
7d77786317
commit
11d4c865c6
@ -418,6 +418,16 @@ impl Cursive {
|
||||
self.clear();
|
||||
}
|
||||
|
||||
/// Updates the current theme.
|
||||
pub fn update_theme(&mut self, f: impl FnOnce(&mut theme::Theme)) {
|
||||
// We don't just expose a `current_theme_mut` because we may want to
|
||||
// run some logic _after_ setting the theme.
|
||||
// Though right now, it's only clearing the screen, so...
|
||||
let mut theme = self.theme.clone();
|
||||
f(&mut theme);
|
||||
self.set_theme(theme);
|
||||
}
|
||||
|
||||
/// Clears the screen.
|
||||
///
|
||||
/// Users rarely have to call this directly.
|
||||
|
@ -4,6 +4,7 @@ use enum_map::{enum_map, Enum, EnumMap};
|
||||
use log::warn;
|
||||
|
||||
use std::ops::{Index, IndexMut};
|
||||
use std::str::FromStr;
|
||||
|
||||
// Use AHash instead of the slower SipHash
|
||||
type HashMap<K, V> = std::collections::HashMap<K, V, ahash::ABuildHasher>;
|
||||
@ -14,18 +15,25 @@ type HashMap<K, V> = std::collections::HashMap<K, V, ahash::ABuildHasher>;
|
||||
///
|
||||
/// It implements `Index` and `IndexMut` to access and modify this mapping:
|
||||
///
|
||||
/// It also implements [`Extend`] to update a batch of colors at
|
||||
/// once.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// # use cursive::theme::Palette;
|
||||
/// use cursive::theme::PaletteColor::*;
|
||||
/// use cursive::theme::Color::*;
|
||||
/// use cursive::theme::BaseColor::*;
|
||||
/// use cursive::theme::{BaseColor::*, Color::*, PaletteColor::*};
|
||||
///
|
||||
/// let mut palette = Palette::default();
|
||||
///
|
||||
/// assert_eq!(palette[Background], Dark(Blue));
|
||||
/// palette[Shadow] = Light(Red);
|
||||
/// assert_eq!(palette[Shadow], Light(Red));
|
||||
///
|
||||
/// let colors = vec![(Shadow, Dark(Green)), (Primary, Light(Blue))];
|
||||
/// palette.extend(colors);
|
||||
/// assert_eq!(palette[Shadow], Dark(Green));
|
||||
/// assert_eq!(palette[Primary], Light(Blue));
|
||||
/// ```
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct Palette {
|
||||
@ -105,24 +113,21 @@ impl Palette {
|
||||
///
|
||||
/// This will update either the basic palette or the custom values.
|
||||
pub fn set_color(&mut self, key: &str, color: Color) {
|
||||
use crate::theme::PaletteColor::*;
|
||||
|
||||
match key {
|
||||
"background" => self.basic[Background] = color,
|
||||
"shadow" => self.basic[Shadow] = color,
|
||||
"view" => self.basic[View] = color,
|
||||
"primary" => self.basic[Primary] = color,
|
||||
"secondary" => self.basic[Secondary] = color,
|
||||
"tertiary" => self.basic[Tertiary] = color,
|
||||
"title_primary" => self.basic[TitlePrimary] = color,
|
||||
"title_secondary" => self.basic[TitleSecondary] = color,
|
||||
"highlight" => self.basic[Highlight] = color,
|
||||
"highlight_inactive" => self.basic[HighlightInactive] = color,
|
||||
other => {
|
||||
if self.set_basic_color(key, color).is_err() {
|
||||
self.custom
|
||||
.insert(other.to_string(), PaletteNode::Color(color));
|
||||
.insert(key.to_string(), PaletteNode::Color(color));
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets a basic color from its name.
|
||||
///
|
||||
/// Returns `Err(())` if `key` is not a known `PaletteColor`.
|
||||
pub fn set_basic_color(
|
||||
&mut self,
|
||||
key: &str,
|
||||
color: Color,
|
||||
) -> Result<(), ()> {
|
||||
PaletteColor::from_str(key).map(|c| self.basic[c] = color)
|
||||
}
|
||||
|
||||
/// Adds a color namespace to this palette.
|
||||
@ -136,6 +141,17 @@ impl Palette {
|
||||
}
|
||||
}
|
||||
|
||||
impl Extend<(PaletteColor, Color)> for Palette {
|
||||
fn extend<T>(&mut self, iter: T)
|
||||
where
|
||||
T: IntoIterator<Item = (PaletteColor, Color)>,
|
||||
{
|
||||
for (k, v) in iter {
|
||||
(*self)[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the default palette for a cursive application.
|
||||
///
|
||||
/// * `Background` => `Dark(Blue)`
|
||||
@ -263,3 +279,25 @@ impl PaletteColor {
|
||||
palette[self]
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for PaletteColor {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, ()> {
|
||||
use PaletteColor::*;
|
||||
|
||||
Ok(match s {
|
||||
"Background" | "background" => Background,
|
||||
"Shadow" | "shadow" => Shadow,
|
||||
"View" | "view" => View,
|
||||
"Primary" | "primary" => Primary,
|
||||
"Secondary" | "secondary" => Secondary,
|
||||
"Tertiary" | "tertiary" => Tertiary,
|
||||
"TitlePrimary" | "title_primary" => TitlePrimary,
|
||||
"TitleSecondary" | "title_secondary" => TitleSecondary,
|
||||
"Highlight" | "highlight" => Highlight,
|
||||
"HighlightInactive" | "highlight_inactive" => HighlightInactive,
|
||||
_ => return Err(()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user