Error messages for invalid colours in border property

This commit is contained in:
Darren Burns
2022-04-25 10:51:39 +01:00
parent 87f56d602c
commit 88d30dca55
5 changed files with 60 additions and 12 deletions

View File

@@ -12,6 +12,5 @@
min-width: 80;
background: dark_blue;
padding: 2;
border-top: solid red ;
border-left: solid red ;
border: solid red;
}

View File

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

View File

@@ -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 (<bordertype>, <color>)",
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 <bordertype> 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]<bordertype> <color>[/]",
examples=[
Example(f"{property_name}: solid red;"),
Example(f"{property_name}: dashed #00ee22;"),
],
),
Bullet(
f"Valid values for <bordertype> 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),
],
)

View File

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

View File

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