1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 23:21:08 +03:00

Deserialize indexed keys into 'u8' instead of 'String'.

This commit is contained in:
Jonas Belouadi 2021-08-21 20:03:32 +02:00 committed by Wez Furlong
parent 46ea5d73e7
commit 4565fc4ebd

View File

@ -24,6 +24,26 @@ impl Default for HsbTransform {
} }
} }
fn de_indexed<'de, D>(deserializer: D) -> Result<HashMap<u8, RgbColor>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Wrap(HashMap<String, RgbColor>);
let Wrap(map) = Wrap::deserialize(deserializer)?;
Ok(map
.into_iter()
.filter_map(|(k, v)| match k.parse::<u8>() {
Ok(n) if n >= 16 => Some((n, v)),
_ => {
log::warn!("Ignoring invalid color key {}", k);
None
}
})
.collect())
}
#[derive(Default, Debug, Deserialize, Serialize, Clone)] #[derive(Default, Debug, Deserialize, Serialize, Clone)]
pub struct Palette { pub struct Palette {
/// The text color to use when the attributes are reset to default /// The text color to use when the attributes are reset to default
@ -44,7 +64,8 @@ pub struct Palette {
pub brights: Option<[RgbColor; 8]>, pub brights: Option<[RgbColor; 8]>,
/// A map for setting arbitrary colors ranging from 16 to 256 in the color /// A map for setting arbitrary colors ranging from 16 to 256 in the color
/// palette /// palette
pub indexed: Option<HashMap<String, RgbColor>>, #[serde(default, deserialize_with = "de_indexed")]
pub indexed: HashMap<u8, RgbColor>,
/// Configure the colors and styling of the tab bar /// Configure the colors and styling of the tab bar
pub tab_bar: Option<TabBarColors>, pub tab_bar: Option<TabBarColors>,
/// The color of the "thumb" of the scrollbar; the segment that /// The color of the "thumb" of the scrollbar; the segment that
@ -85,14 +106,8 @@ impl From<Palette> for wezterm_term::color::ColorPalette {
p.colors.0[idx + 8] = *col; p.colors.0[idx + 8] = *col;
} }
} }
if let Some(indexed) = cfg.indexed { for (idx, col) in cfg.indexed.iter() {
for (idx, col) in indexed p.colors.0[*idx as usize] = *col;
.iter()
.filter_map(|(k, v)| Some((k.parse::<usize>().ok()?, v)))
.filter(|&(k, _)| 15 < k && k < 256)
{
p.colors.0[idx] = *col;
}
} }
p p
} }