From 4fac2e8dc988c8c5823eed6e0a6f29de92fcdc30 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 18 May 2022 09:31:10 -0700 Subject: [PATCH] tidy up handling of index colors IndexedMap was a temporary measure while I was hacking through the dynamic crate stuff; we don't need it any more. --- config/src/color.rs | 60 +++++++++------------------------------------ 1 file changed, 11 insertions(+), 49 deletions(-) diff --git a/config/src/color.rs b/config/src/color.rs index e28b7890d..c90fc527c 100644 --- a/config/src/color.rs +++ b/config/src/color.rs @@ -5,7 +5,7 @@ use std::convert::TryFrom; use std::str::FromStr; use termwiz::cell::CellAttributes; pub use termwiz::color::{ColorSpec, RgbColor, SrgbaTuple}; -use wezterm_dynamic::{FromDynamic, FromDynamicOptions, ToDynamic, Value}; +use wezterm_dynamic::{FromDynamic, ToDynamic}; #[derive(Debug, Copy, Clone, FromDynamic, ToDynamic)] pub struct HsbTransform { @@ -27,52 +27,6 @@ impl Default for HsbTransform { } } -struct IndexedMap(HashMap); - -impl ToDynamic for IndexedMap { - fn to_dynamic(&self) -> Value { - self.0.to_dynamic() - } -} - -impl FromDynamic for IndexedMap { - fn from_dynamic( - value: &Value, - options: FromDynamicOptions, - ) -> Result { - let inner = >::from_dynamic(value, options)?; - Ok(Self(inner)) - } -} - -impl From<&HashMap> for IndexedMap { - fn from(map: &HashMap) -> IndexedMap { - IndexedMap( - map.iter() - .map(|(k, v)| (k.to_string(), v.clone())) - .collect(), - ) - } -} - -impl TryFrom for HashMap { - type Error = String; - - fn try_from(map: IndexedMap) -> Result, String> { - Ok(map - .0 - .into_iter() - .filter_map(|(k, v)| match k.parse::() { - Ok(n) if n >= 16 => Some((n, v)), - _ => { - log::warn!("Ignoring invalid color key {}", k); - None - } - }) - .collect()) - } -} - #[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, FromDynamic, ToDynamic)] #[dynamic(try_from = "String", into = "String")] pub struct RgbaColor { @@ -113,7 +67,7 @@ impl Into for RgbaColor { } } -impl std::convert::TryFrom for RgbaColor { +impl TryFrom for RgbaColor { type Error = anyhow::Error; fn try_from(s: String) -> anyhow::Result { Ok(RgbaColor { @@ -143,7 +97,7 @@ pub struct Palette { pub brights: Option<[RgbaColor; 8]>, /// A map for setting arbitrary colors ranging from 16 to 256 in the color /// palette - #[dynamic(default, try_from = "IndexedMap", into = "IndexedMap")] + #[dynamic(default)] pub indexed: HashMap, /// Configure the colors and styling of the tab bar pub tab_bar: Option, @@ -191,6 +145,14 @@ impl From for wezterm_term::color::ColorPalette { } } for (&idx, &col) in &cfg.indexed { + if idx < 16 { + log::warn!( + "Ignoring invalid colors.indexed index {}; \ + use `ansi` or `brights` to specify lower indices", + idx + ); + continue; + } p.colors.0[idx as usize] = col.into(); } p