From c518b86a35be82b0f5023198722d29bdd12a6f94 Mon Sep 17 00:00:00 2001 From: Theo Haines <91698052+TheoHaines@users.noreply.github.com> Date: Fri, 3 Jul 2026 01:53:45 +0100 Subject: [PATCH] Enhance to_ansi method with mode validation Add error handling for unsupported color modes in to_ansi method. --- hyfetch/color_util.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/hyfetch/color_util.py b/hyfetch/color_util.py index 7eebaaa5..1d711b4e 100644 --- a/hyfetch/color_util.py +++ b/hyfetch/color_util.py @@ -201,13 +201,24 @@ class RGB: def to_ansi(self, mode: AnsiMode | None = None, foreground: bool = True) -> str: if not mode: mode = GLOBAL_CFG.color_mode + + # If the mode is none, raise an error + if mode is None: + raise ValueError("GLOBAL_CFG.color_mode is not set") + if mode == 'rgb': return self.to_ansi_rgb(foreground) if mode == '8bit': return self.to_ansi_8bit(foreground) - # 'ansi' (16-color) is not yet implemented; fall back to 8bit which always works. - # 'default' and any unknown mode also fall through here as a safe degradation. - return self.to_ansi_8bit(foreground) + if mode == 'default': + # treat 'default' as 8bit (256 colors) + return self.to_ansi_8bit(foreground) + if mode == 'ansi': + # 16-color 'ansi' mode is not implemented yet. + raise NotImplementedError("'ansi' (16-color) mode is not implemented") + + # Unknown / misspelled mode in config + raise ValueError(f"Unknown color mode: {mode!r}") def lighten(self, multiplier: float) -> 'RGB': """