From a5685c2e083b801be94f72300ab5a19f4a94c266 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Tue, 26 Apr 2022 13:05:54 +0100 Subject: [PATCH] Help text for fractional properties --- sandbox/uber.py | 1 + src/textual/css/_help_text.py | 6 +++--- src/textual/css/_style_properties.py | 11 ++++++++--- src/textual/css/_styles_builder.py | 9 ++------- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/sandbox/uber.py b/sandbox/uber.py index d373f1b36..fba575f5a 100644 --- a/sandbox/uber.py +++ b/sandbox/uber.py @@ -81,6 +81,7 @@ class BasicApp(App): def action_toggle_border(self): self.focused.styles.border = [("solid", "red"), ("dashed", "white")] + self.focused.styles.opacity = "x" 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 2495f92a4..46500422b 100644 --- a/src/textual/css/_help_text.py +++ b/src/textual/css/_help_text.py @@ -526,8 +526,8 @@ def fractional_property_help_text( Bullet( f"In Python, you can set [i]{property_name}[/] to a string or float value", examples=[ - Example(f"widget.styles.{property_name} = 0.25"), Example(f'widget.styles.{property_name} = "50%"'), + Example(f"widget.styles.{property_name} = 0.25"), ], ) ], @@ -535,11 +535,11 @@ def fractional_property_help_text( Bullet( f"In Textual CSS, you can set [i]{property_name}[/] to a string or float", examples=[ - Example(f"{property_name}: 0.25;"), Example(f"{property_name}: 50%;"), + Example(f"{property_name}: 0.25;"), ], ) ], - ) + ).get_by_context(context) ], ) diff --git a/src/textual/css/_style_properties.py b/src/textual/css/_style_properties.py index df72b4600..ddb46a993 100644 --- a/src/textual/css/_style_properties.py +++ b/src/textual/css/_style_properties.py @@ -14,7 +14,11 @@ from typing import Iterable, NamedTuple, TYPE_CHECKING, cast import rich.repr from rich.style import Style -from ._help_text import border_property_help_text, layout_property_help_text +from ._help_text import ( + border_property_help_text, + layout_property_help_text, + fractional_property_help_text, +) from ._help_text import ( spacing_wrong_number_of_values, scalar_help_text, @@ -908,8 +912,9 @@ class FractionalProperty: elif isinstance(value, str) and value.endswith("%"): float_value = float(Scalar.parse(value).value) / 100 else: - raise StyleTypeError( - f"{self.name} must be a str (e.g. '10%') or a float (e.g. 0.1)" + raise StyleValueError( + f"{self.name} must be a str (e.g. '10%') or a float (e.g. 0.1)", + help_text=fractional_property_help_text(name, context="inline"), ) if obj.set_rule(name, clamp(float_value, 0, 1)): obj.refresh() diff --git a/src/textual/css/_styles_builder.py b/src/textual/css/_styles_builder.py index fff4883b4..7ab0f294b 100644 --- a/src/textual/css/_styles_builder.py +++ b/src/textual/css/_styles_builder.py @@ -16,6 +16,7 @@ from ._help_text import ( layout_property_help_text, docks_property_help_text, dock_property_help_text, + fractional_property_help_text, ) from .constants import ( VALID_ALIGN_HORIZONTAL, @@ -315,13 +316,7 @@ class StylesBuilder: error = True if error: - self.error( - name, - token, - f"property 'opacity' has invalid value {_join_tokens(tokens)!r}; " - f"expected a percentage or float between 0 and 1; " - f"example valid values: '0.4', '40%'", - ) + self.error(name, token, fractional_property_help_text(name, context="css")) def _process_space(self, name: str, tokens: list[Token]) -> None: space: list[int] = []