Skip to content

Commit

Permalink
Parse hex colors in themes (#647)
Browse files Browse the repository at this point in the history
Just delegate to the default deserializer of crossterm which learned
dealing with hex color in version 0.27.0.
  • Loading branch information
zappolowski committed Aug 29, 2023
1 parent 9b8ed0e commit a87bc9a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
33 changes: 21 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ clap_complete = "4.1"
version_check = "0.9.*"

[dependencies]
crossterm = { version = "0.24.0", features = ["serde"] }
crossterm = { version = "0.27.0", features = ["serde"] }
dirs = "3.0.*"
libc = "0.2.*"
human-sort = "0.2.2"
Expand Down
20 changes: 17 additions & 3 deletions src/theme/color.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///! This module provides methods to create theme from files and operations related to
///! this.
use crossterm::style::Color;
use serde::Deserialize;
use serde::{de::IntoDeserializer, Deserialize};
use std::fmt;

// Custom color deserialize
Expand All @@ -23,8 +23,7 @@ where
where
E: serde::de::Error,
{
Color::try_from(value)
.map_err(|_| E::invalid_value(serde::de::Unexpected::Str(value), &self))
Color::deserialize(value.into_deserializer())
}

fn visit_u64<E>(self, value: u64) -> Result<Color, E>
Expand Down Expand Up @@ -470,6 +469,21 @@ tree-edge: 245
assert_eq!(empty_theme, theme);
}

#[test]
fn test_hexadecimal_colors() {
// Must contain one field at least
// ref https://github.com/dtolnay/serde-yaml/issues/86
let empty_theme: ColorTheme = Theme::with_yaml("user: \"#ff007f\"").unwrap();
assert_eq!(
empty_theme.user,
crossterm::style::Color::Rgb {
r: 255,
g: 0,
b: 127
}
);
}

#[test]
fn test_second_level_theme_return_default_but_changed() {
// Must contain one field at least
Expand Down

0 comments on commit a87bc9a

Please sign in to comment.