diff --git a/crates/hyfetch/src/models.rs b/crates/hyfetch/src/models.rs index 6487acf8..ad6962d6 100644 --- a/crates/hyfetch/src/models.rs +++ b/crates/hyfetch/src/models.rs @@ -1,10 +1,10 @@ -use std::collections::{HashMap}; +use std::collections::HashMap; use anyhow::{Context as _, Result}; use serde::{Deserialize, Serialize}; -use crate::color_util::Lightness; use crate::color_profile::ColorProfile; +use crate::color_util::Lightness; use crate::neofetch_util::ColorAlignment; use crate::types::{AnsiMode, Backend, TerminalTheme}; @@ -26,8 +26,6 @@ pub struct Config { pub custom_presets: Option>>, } - - impl Config { pub fn default_lightness(theme: TerminalTheme) -> Lightness { match theme { @@ -44,6 +42,9 @@ impl Config { let mut profiles = HashMap::new(); if let Some(custom_presets) = &self.custom_presets { for (preset_name, colors) in custom_presets { + if preset_name == "random" { + return Err(anyhow::anyhow!("custom preset key `random` is reserved")); + } let color_profile = build_hex_color_profile(colors).with_context(|| { format!("failed to validate custom preset key `{preset_name}`") })?; @@ -55,10 +56,15 @@ impl Config { } pub fn build_hex_color_profile(hex_colors: &[String]) -> Result { + if hex_colors.is_empty() { + return Err(anyhow::anyhow!("hex color list cannot be empty")); + } + for color in hex_colors { - if !color.starts_with('#') || - (color.len() != 4 && color.len() != 7) || - !color[1..].chars().all(|c| c.is_ascii_hexdigit()) { + if !color.starts_with('#') + || (color.len() != 4 && color.len() != 7) + || !color[1..].chars().all(|c| c.is_ascii_hexdigit()) + { return Err(anyhow::anyhow!("invalid hex color: {color}")); } } diff --git a/hyfetch/main.py b/hyfetch/main.py index 6052f650..83d0b4f3 100755 --- a/hyfetch/main.py +++ b/hyfetch/main.py @@ -491,7 +491,7 @@ def run(): if '#' in preset_string: colors = [s.strip() for s in preset_string.split(',')] return build_hex_color_profile(colors) - + preset_profiles: dict[str, ColorProfile] = { name: preset for name, preset in PRESETS.items() } @@ -511,7 +511,7 @@ def run(): raise ValueError( "PRESET should be comma-separated hex colors or one of " f"{{{','.join(available)}}}" - ) + ) try: preset = parse_preset_string(config.preset, config) diff --git a/hyfetch/models.py b/hyfetch/models.py index 5a55e537..34da3d07 100644 --- a/hyfetch/models.py +++ b/hyfetch/models.py @@ -10,6 +10,9 @@ from .types import AnsiMode, LightDark, BackendLiteral def build_hex_color_profile(hex_colors: list[str]) -> ColorProfile: + if not hex_colors: + raise ValueError('hex color list cannot be empty') + for color in hex_colors: if not ( color.startswith('#') @@ -48,6 +51,8 @@ class Config: profiles: dict[str, ColorProfile] = {} if self.custom_presets: for preset_name, colors in self.custom_presets.items(): + if preset_name == 'random': + raise ValueError('custom preset key "random" is reserved') try: profiles[preset_name] = build_hex_color_profile(colors) except ValueError as err: