fix: Fix ColorParseError by ensuring hex colors have # prefix

This commit is contained in:
Paul Gauthier (aider) 2025-03-31 10:32:48 +13:00
parent 1c7db4da0d
commit 605d8fe59a
2 changed files with 29 additions and 1 deletions

View file

@ -963,6 +963,7 @@ class InputOutput:
if not isinstance(message, Text):
message = Text(message)
color = ensure_hash_prefix(color) if color else None
style = dict(style=color) if self.pretty and color else dict()
try:
self.console.print(message, **style)
@ -993,7 +994,7 @@ class InputOutput:
style = dict()
if self.pretty:
if self.tool_output_color:
style["color"] = self.tool_output_color
style["color"] = ensure_hash_prefix(self.tool_output_color)
style["reverse"] = bold
style = RichStyle(**style)

View file

@ -446,6 +446,33 @@ class TestInputOutputMultilineMode(unittest.TestCase):
self.assertEqual(ensure_hash_prefix("1234567"), "1234567") # Wrong length
self.assertEqual(ensure_hash_prefix("xyz"), "xyz") # Invalid hex chars
self.assertEqual(ensure_hash_prefix("12345g"), "12345g") # Invalid hex chars
def test_tool_output_color_handling(self):
"""Test that tool_output correctly handles hex colors without # prefix"""
from unittest.mock import patch
from rich.text import Text
# Create IO with hex color without # for tool_output_color
io = InputOutput(tool_output_color="FFA500", pretty=True)
# Patch console.print to avoid actual printing
with patch.object(io.console, "print") as mock_print:
# This would raise ColorParseError without the fix
io.tool_output("Test message")
# Verify the call was made without error
mock_print.assert_called_once()
# Verify the style was correctly created with # prefix
# The first argument is the message, second would be the style
kwargs = mock_print.call_args.kwargs
self.assertIn("style", kwargs)
# Test with other hex color
io = InputOutput(tool_output_color="00FF00", pretty=True)
with patch.object(io.console, "print") as mock_print:
io.tool_output("Test message")
mock_print.assert_called_once()
if __name__ == "__main__":