From 88d30dca553dd972e1e2948b63fbec76b088519f Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Mon, 25 Apr 2022 10:51:39 +0100 Subject: [PATCH] Error messages for invalid colours in border property --- sandbox/uber.css | 3 +- sandbox/uber.py | 5 +-- src/textual/css/_help_text.py | 46 +++++++++++++++++++++++++++- src/textual/css/_style_properties.py | 12 ++++++-- src/textual/css/_styles_builder.py | 6 ++-- 5 files changed, 60 insertions(+), 12 deletions(-) diff --git a/sandbox/uber.css b/sandbox/uber.css index 423dfff63..fcd49c736 100644 --- a/sandbox/uber.css +++ b/sandbox/uber.css @@ -12,6 +12,5 @@ min-width: 80; background: dark_blue; padding: 2; - border-top: solid red ; - border-left: solid red ; + border: solid red; } diff --git a/sandbox/uber.py b/sandbox/uber.py index 4b9cb8dbe..04f5987e9 100644 --- a/sandbox/uber.py +++ b/sandbox/uber.py @@ -85,10 +85,7 @@ class BasicApp(App): def action_increase_margin(self): old_margin = self.focused.styles.margin - # new_margin = old_margin + (1,1,1) - # self.focused.styles.padding = (1, 1, 1) - self.focused.styles.color = "banana" - self.focused.styles.border + self.focused.styles.border = [("solid", "green"), ("dashed", "s")] BasicApp.run(css_file="uber.css", log="textual.log", log_verbosity=1) diff --git a/src/textual/css/_help_text.py b/src/textual/css/_help_text.py index 167da69f4..f47cfa1ae 100644 --- a/src/textual/css/_help_text.py +++ b/src/textual/css/_help_text.py @@ -4,6 +4,7 @@ import sys from dataclasses import dataclass from textual.css._help_renderables import Example, Bullet, HelpText +from textual.css.constants import VALID_BORDER if sys.version_info >= (3, 8): from typing import Literal, Iterable @@ -254,5 +255,48 @@ def border_property_help_text( property_name = _contextualize_property_name(property_name, context) return HelpText( summary=f"Invalid value for [i]{property_name}[/] property", - bullets=[*ContextSpecificBullets(inline=[Bullet("")]).get_by_context(context)], + bullets=[ + *ContextSpecificBullets( + inline=[ + Bullet( + f"In Python, set '{property_name}' using a tuple of the form (, )", + examples=[ + Example( + f'widget.styles.{property_name} = ("solid", "red")' + ), + Example( + f'widget.styles.{property_name} = ("round", #f0f0f0")' + ), + Example( + f'widget.styles.{property_name} = [("dashed", "#f0f0f0"), ("solid", "blue")] [dim]# Vertical, horizontal' + ), + ], + ), + Bullet( + f"Valid values for are {friendly_list(VALID_BORDER)}" + ), + Bullet( + f"Colors can be specified using hex, RGB, or ANSI color names" + ), + ], + css=[ + Bullet( + f"In Textual CSS, set '{property_name}' using a value of the form [i] [/]", + examples=[ + Example(f"{property_name}: solid red;"), + Example(f"{property_name}: dashed #00ee22;"), + ], + ), + Bullet( + f"Valid values for are {friendly_list(VALID_BORDER)}" + ), + Bullet( + f"Colors can be specified using hex, RGB, or ANSI color names" + ), + Bullet( + f"To set border for a specific edge, use [i]border-top[/], [i]border-left[/], etc." + ), + ], + ).get_by_context(context), + ], ) diff --git a/src/textual/css/_style_properties.py b/src/textual/css/_style_properties.py index 04b970615..9ed0bbf20 100644 --- a/src/textual/css/_style_properties.py +++ b/src/textual/css/_style_properties.py @@ -14,7 +14,7 @@ from typing import Iterable, NamedTuple, TYPE_CHECKING, cast import rich.repr from rich.style import Style -from ._help_text import scalar_help_text +from ._help_text import scalar_help_text, border_property_help_text from .. import log from ._help_text import ( spacing_wrong_number_of_values, @@ -188,7 +188,15 @@ class BoxProperty: _type, color = border new_value = border if isinstance(color, str): - new_value = (_type, Color.parse(color)) + try: + new_value = (_type, Color.parse(color)) + except ColorParseError as error: + raise StyleValueError( + str(error), + help_text=border_property_help_text( + self.name, context="inline" + ), + ) elif isinstance(color, Color): new_value = (_type, color) if obj.set_rule(self.name, new_value): diff --git a/src/textual/css/_styles_builder.py b/src/textual/css/_styles_builder.py index 42a914056..a947bf4b9 100644 --- a/src/textual/css/_styles_builder.py +++ b/src/textual/css/_styles_builder.py @@ -12,6 +12,7 @@ from ._help_text import ( spacing_invalid_value, string_enum_help_text, color_property_help_text, + border_property_help_text, ) from .constants import ( VALID_ALIGN_HORIZONTAL, @@ -391,15 +392,14 @@ class StylesBuilder: try: border_color = Color.parse(value) except ColorParseError: - # TODO: Raise specific error here self.error( - name, token, f"unexpected token {value!r} in declaration" + name, token, border_property_help_text(name, context="css") ) elif token_name == "color": border_color = Color.parse(value) else: - self.error(name, token, f"unexpected token {value!r} in declaration") + self.error(name, token, border_property_help_text(name, context="css")) return (border_type, border_color)