[F] Resolve Copilot issues

This commit is contained in:
thea 2026-03-23 14:52:08 +10:00
parent 81ed5e9d13
commit 46abf10af1
No known key found for this signature in database
3 changed files with 20 additions and 9 deletions

View File

@ -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<HashMap<String, Vec<String>>>,
}
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<ColorProfile> {
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}"));
}
}

View File

@ -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)

View File

@ -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: