File size: 27,759 Bytes
2c3c408 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 | from __future__ import annotations
import sys
from dataclasses import dataclass
from typing import Iterable
from textual.color import ColorParseError
from textual.css._help_renderables import Example, Bullet, HelpText
from textual.css.constants import (
VALID_BORDER,
VALID_LAYOUT,
VALID_ALIGN_HORIZONTAL,
VALID_ALIGN_VERTICAL,
VALID_STYLE_FLAGS,
VALID_TEXT_ALIGN,
)
if sys.version_info >= (3, 8):
from typing import Literal, Iterable, Sequence
else:
from typing_extensions import Literal
from textual.css._error_tools import friendly_list
from textual.css.scalar import SYMBOL_UNIT
StylingContext = Literal["inline", "css"]
"""The type of styling the user was using when the error was encountered.
Used to give help text specific to the context i.e. we give CSS help if the
user hit an issue with their CSS, and Python help text when the user has an
issue with inline styles."""
@dataclass
class ContextSpecificBullets:
"""
Args:
inline (Iterable[Bullet]): Information only relevant to users who are using inline styling.
css (Iterable[Bullet]): Information only relevant to users who are using CSS.
"""
inline: Sequence[Bullet]
css: Sequence[Bullet]
def get_by_context(self, context: StylingContext) -> list[Bullet]:
"""Get the information associated with the given context
Args:
context (StylingContext | None): The context to retrieve info for.
"""
if context == "inline":
return list(self.inline)
else:
return list(self.css)
def _python_name(property_name: str) -> str:
"""Convert a CSS property name to the corresponding Python attribute name
Args:
property_name (str): The CSS property name
Returns:
str: The Python attribute name as found on the Styles object
"""
return property_name.replace("-", "_")
def _css_name(property_name: str) -> str:
"""Convert a Python style attribute name to the corresponding CSS property name
Args:
property_name (str): The Python property name
Returns:
str: The CSS property name
"""
return property_name.replace("_", "-")
def _contextualize_property_name(
property_name: str,
context: StylingContext,
) -> str:
"""Convert a property name to CSS or inline by replacing
'-' with '_' or vice-versa
Args:
property_name (str): The name of the property
context (StylingContext): The context the property is being used in.
Returns:
str: The property name converted to the given context.
"""
return _css_name(property_name) if context == "css" else _python_name(property_name)
def _spacing_examples(property_name: str) -> ContextSpecificBullets:
"""Returns examples for spacing properties"""
return ContextSpecificBullets(
inline=[
Bullet(
f"Set [i]{property_name}[/] to a tuple to assign spacing to each edge",
examples=[
Example(
f"widget.styles.{property_name} = (1, 2) [dim]# Vertical, horizontal"
),
Example(
f"widget.styles.{property_name} = (1, 2, 3, 4) [dim]# Top, right, bottom, left"
),
],
),
Bullet(
"Or to an integer to assign a single value to all edges",
examples=[Example(f"widget.styles.{property_name} = 2")],
),
],
css=[
Bullet(
"Supply 1, 2 or 4 integers separated by a space",
examples=[
Example(f"{property_name}: 1;"),
Example(f"{property_name}: 1 2; [dim]# Vertical, horizontal"),
Example(
f"{property_name}: 1 2 3 4; [dim]# Top, right, bottom, left"
),
],
),
],
)
def property_invalid_value_help_text(
property_name: str, context: StylingContext, *, suggested_property_name: str = None
) -> HelpText:
"""Help text to show when the user supplies an invalid value for CSS property
property.
Args:
property_name (str): The name of the property
context (StylingContext | None): The context the spacing property is being used in.
Keyword Args:
suggested_property_name (str | None): A suggested name for the property (e.g. "width" for "wdth"). Defaults to None.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
summary = f"Invalid CSS property {property_name!r}"
if suggested_property_name:
suggested_property_name = _contextualize_property_name(
suggested_property_name, context
)
summary += f'. Did you mean "{suggested_property_name}"?'
return HelpText(summary)
def spacing_wrong_number_of_values_help_text(
property_name: str,
num_values_supplied: int,
context: StylingContext,
) -> HelpText:
"""Help text to show when the user supplies the wrong number of values
for a spacing property (e.g. padding or margin).
Args:
property_name (str): The name of the property
num_values_supplied (int): The number of values the user supplied (a number other than 1, 2 or 4).
context (StylingContext | None): The context the spacing property is being used in.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
return HelpText(
summary=f"Invalid number of values for the [i]{property_name}[/] property",
bullets=[
Bullet(
f"You supplied {num_values_supplied} values for the [i]{property_name}[/] property"
),
Bullet(
"Spacing properties like [i]margin[/] and [i]padding[/] require either 1, 2 or 4 integer values"
),
*_spacing_examples(property_name).get_by_context(context),
],
)
def spacing_invalid_value_help_text(
property_name: str,
context: StylingContext,
) -> HelpText:
"""Help text to show when the user supplies an invalid value for a spacing
property.
Args:
property_name (str): The name of the property
context (StylingContext | None): The context the spacing property is being used in.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
return HelpText(
summary=f"Invalid value for the [i]{property_name}[/] property",
bullets=_spacing_examples(property_name).get_by_context(context),
)
def scalar_help_text(
property_name: str,
context: StylingContext,
) -> HelpText:
"""Help text to show when the user supplies an invalid value for
a scalar property.
Args:
property_name (str): The name of the property
num_values_supplied (int): The number of values the user supplied (a number other than 1, 2 or 4).
context (StylingContext | None): The context the scalar property is being used in.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
return HelpText(
summary=f"Invalid value for the [i]{property_name}[/] property",
bullets=[
Bullet(
f"Scalar properties like [i]{property_name}[/] require numerical values and an optional unit"
),
Bullet(f"Valid units are {friendly_list(SYMBOL_UNIT)}"),
*ContextSpecificBullets(
inline=[
Bullet(
"Assign a string, int or Scalar object itself",
examples=[
Example(f'widget.styles.{property_name} = "50%"'),
Example(f"widget.styles.{property_name} = 10"),
Example(f"widget.styles.{property_name} = Scalar(...)"),
],
),
],
css=[
Bullet(
"Write the number followed by the unit",
examples=[
Example(f"{property_name}: 50%;"),
Example(f"{property_name}: 5;"),
],
),
],
).get_by_context(context),
],
)
def string_enum_help_text(
property_name: str,
valid_values: Iterable[str],
context: StylingContext,
) -> HelpText:
"""Help text to show when the user supplies an invalid value for a string
enum property.
Args:
property_name (str): The name of the property
valid_values (list[str]): A list of the values that are considered valid.
context (StylingContext | None): The context the property is being used in.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
return HelpText(
summary=f"Invalid value for the [i]{property_name}[/] property",
bullets=[
Bullet(
f"The [i]{property_name}[/] property can only be set to {friendly_list(valid_values)}"
),
*ContextSpecificBullets(
inline=[
Bullet(
"Assign any of the valid strings to the property",
examples=[
Example(f'widget.styles.{property_name} = "{valid_value}"')
for valid_value in sorted(valid_values)
],
)
],
css=[
Bullet(
"Assign any of the valid strings to the property",
examples=[
Example(f"{property_name}: {valid_value};")
for valid_value in sorted(valid_values)
],
)
],
).get_by_context(context),
],
)
def color_property_help_text(
property_name: str,
context: StylingContext,
*,
error: Exception = None,
) -> HelpText:
"""Help text to show when the user supplies an invalid value for a color
property. For example, an unparseable color string.
Args:
property_name (str): The name of the property
context (StylingContext | None): The context the property is being used in.
error (ColorParseError | None): The error that caused this help text to be displayed. Defaults to None.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
summary = f"Invalid value for the [i]{property_name}[/] property"
suggested_color = (
error.suggested_color if error and isinstance(error, ColorParseError) else None
)
if suggested_color:
summary += f'. Did you mean "{suggested_color}"?'
return HelpText(
summary=summary,
bullets=[
Bullet(
f"The [i]{property_name}[/] property can only be set to a valid color"
),
Bullet(f"Colors can be specified using hex, RGB, or ANSI color names"),
*ContextSpecificBullets(
inline=[
Bullet(
"Assign colors using strings or Color objects",
examples=[
Example(f'widget.styles.{property_name} = "#ff00aa"'),
Example(
f'widget.styles.{property_name} = "rgb(12,231,45)"'
),
Example(f'widget.styles.{property_name} = "red"'),
Example(
f"widget.styles.{property_name} = Color(1, 5, 29, a=0.5)"
),
],
)
],
css=[
Bullet(
"Colors can be set as follows",
examples=[
Example(f"{property_name}: [#ff00aa]#ff00aa[/];"),
Example(f"{property_name}: rgb(12,231,45);"),
Example(f"{property_name}: [rgb(255,0,0)]red[/];"),
],
)
],
).get_by_context(context),
],
)
def border_property_help_text(property_name: str, context: StylingContext) -> HelpText:
"""Help text to show when the user supplies an invalid value for a border
property (such as border, border-right, outline)
Args:
property_name (str): The name of the property
context (StylingContext | None): The context the property is being used in.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
return HelpText(
summary=f"Invalid value for [i]{property_name}[/] property",
bullets=[
*ContextSpecificBullets(
inline=[
Bullet(
f"Set [i]{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:\n{friendly_list(VALID_BORDER)}"
),
Bullet(
f"Colors can be specified using hex, RGB, or ANSI color names"
),
],
css=[
Bullet(
f"Set [i]{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:\n{friendly_list(VALID_BORDER)}"
),
Bullet(
f"Colors can be specified using hex, RGB, or ANSI color names"
),
],
).get_by_context(context),
],
)
def layout_property_help_text(property_name: str, context: StylingContext) -> HelpText:
"""Help text to show when the user supplies an invalid value
for a layout property.
Args:
property_name (str): The name of the property
context (StylingContext | None): The context the property is being used in.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
return HelpText(
summary=f"Invalid value for [i]{property_name}[/] property",
bullets=[
Bullet(
f"The [i]{property_name}[/] property expects a value of {friendly_list(VALID_LAYOUT)}"
),
],
)
def dock_property_help_text(property_name: str, context: StylingContext) -> HelpText:
"""Help text to show when the user supplies an invalid value for dock.
Args:
property_name (str): The name of the property
context (StylingContext | None): The context the property is being used in.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
return HelpText(
summary=f"Invalid value for [i]{property_name}[/] property",
bullets=[
Bullet("The value must be one of 'top', 'right', 'bottom' or 'left'"),
*ContextSpecificBullets(
inline=[
Bullet(
"The 'dock' rule aligns a widget relative to the screen.",
examples=[Example(f'header.styles.dock = "top"')],
)
],
css=[
Bullet(
"The 'dock' rule aligns a widget relative to the screen.",
examples=[Example(f"dock: top")],
)
],
).get_by_context(context),
],
)
def fractional_property_help_text(
property_name: str, context: StylingContext
) -> HelpText:
"""Help text to show when the user supplies an invalid value for a fractional property.
Args:
property_name (str): The name of the property
context (StylingContext | None): The context the property is being used in.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
return HelpText(
summary=f"Invalid value for [i]{property_name}[/] property",
bullets=[
*ContextSpecificBullets(
inline=[
Bullet(
f"Set [i]{property_name}[/] to a string or float value",
examples=[
Example(f'widget.styles.{property_name} = "50%"'),
Example(f"widget.styles.{property_name} = 0.25"),
],
)
],
css=[
Bullet(
f"Set [i]{property_name}[/] to a string or float",
examples=[
Example(f"{property_name}: 50%;"),
Example(f"{property_name}: 0.25;"),
],
)
],
).get_by_context(context)
],
)
def offset_property_help_text(context: StylingContext) -> HelpText:
"""Help text to show when the user supplies an invalid value for the offset property.
Args:
context (StylingContext | None): The context the property is being used in.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
return HelpText(
summary="Invalid value for [i]offset[/] property",
bullets=[
*ContextSpecificBullets(
inline=[
Bullet(
markup="The [i]offset[/] property expects a tuple of 2 values [i](<horizontal>, <vertical>)[/]",
examples=[
Example("widget.styles.offset = (2, '50%')"),
],
),
],
css=[
Bullet(
markup="The [i]offset[/] property expects a value of the form [i]<horizontal> <vertical>[/]",
examples=[
Example(
"offset: 2 3; [dim]# Horizontal offset of 2, vertical offset of 3"
),
Example(
"offset: 2 50%; [dim]# Horizontal offset of 2, vertical offset of 50%"
),
],
),
],
).get_by_context(context),
Bullet("<horizontal> and <vertical> can be a number or scalar value"),
],
)
def scrollbar_size_property_help_text(context: StylingContext) -> HelpText:
"""Help text to show when the user supplies an invalid value for the scrollbar-size property.
Args:
context (StylingContext | None): The context the property is being used in.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
return HelpText(
summary="Invalid value for [i]scrollbar-size[/] property",
bullets=[
*ContextSpecificBullets(
inline=[
Bullet(
markup="The [i]scrollbar_size[/] property expects a tuple of 2 values [i](<horizontal>, <vertical>)[/]",
examples=[
Example("widget.styles.scrollbar_size = (2, 1)"),
],
),
],
css=[
Bullet(
markup="The [i]scrollbar-size[/] property expects a value of the form [i]<horizontal> <vertical>[/]",
examples=[
Example(
"scrollbar-size: 2 3; [dim]# Horizontal size of 2, vertical size of 3"
),
],
),
],
).get_by_context(context),
Bullet(
"<horizontal> and <vertical> must be positive integers, greater than zero"
),
],
)
def scrollbar_size_single_axis_help_text(property_name: str) -> HelpText:
"""Help text to show when the user supplies an invalid value for a scrollbar-size-* property.
Args:
property_name (str): The name of the property
Returns:
HelpText: Renderable for displaying the help text for this property
"""
return HelpText(
summary=f"Invalid value for [i]{property_name}[/]",
bullets=[
Bullet(
markup=f"The [i]{property_name}[/] property can only be set to a positive integer, greater than zero",
examples=[
Example(f"{property_name}: 2;"),
],
),
],
)
def integer_help_text(property_name: str) -> HelpText:
"""Help text to show when the user supplies an invalid integer value.
Args:
property_name (str): The name of the property
Returns:
HelpText: Renderable for displaying the help text for this property
"""
return HelpText(
summary=f"Invalid value for [i]{property_name}[/]",
bullets=[
Bullet(
markup=f"An integer value is expected here",
examples=[
Example(f"{property_name}: 2;"),
],
),
],
)
def align_help_text() -> HelpText:
"""Help text to show when the user supplies an invalid value for a `align`.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
return HelpText(
summary="Invalid value for [i]align[/] property",
bullets=[
Bullet(
markup="The [i]align[/] property expects exactly 2 values",
examples=[
Example("align: <horizontal> <vertical>"),
Example(
"align: center middle; [dim]# Center vertically & horizontally within parent"
),
Example(
"align: left middle; [dim]# Align on the middle left of the parent"
),
],
),
Bullet(
f"Valid values for <horizontal> are {friendly_list(VALID_ALIGN_HORIZONTAL)}"
),
Bullet(
f"Valid values for <vertical> are {friendly_list(VALID_ALIGN_VERTICAL)}",
),
],
)
def text_align_help_text() -> HelpText:
"""Help text to show when the user supplies an invalid value for the text-align property
Returns:
HelpText: Renderable for displaying the help text for this property.
"""
return HelpText(
summary="Invalid value for the [i]text-align[/] property.",
bullets=[
Bullet(
f"The [i]text-align[/] property must be one of {friendly_list(VALID_TEXT_ALIGN)}",
examples=[
Example("text-align: center;"),
Example("text-align: right;"),
],
)
],
)
def offset_single_axis_help_text(property_name: str) -> HelpText:
"""Help text to show when the user supplies an invalid value for an offset-* property.
Args:
property_name (str): The name of the property
Returns:
HelpText: Renderable for displaying the help text for this property
"""
return HelpText(
summary=f"Invalid value for [i]{property_name}[/]",
bullets=[
Bullet(
markup=f"The [i]{property_name}[/] property can be set to a number or scalar value",
examples=[
Example(f"{property_name}: 10;"),
Example(f"{property_name}: 50%;"),
],
),
Bullet(f"Valid scalar units are {friendly_list(SYMBOL_UNIT)}"),
],
)
def style_flags_property_help_text(
property_name: str, value: str, context: StylingContext
) -> HelpText:
"""Help text to show when the user supplies an invalid value for a style flags property.
Args:
property_name (str): The name of the property
context (StylingContext | None): The context the property is being used in.
Returns:
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
return HelpText(
summary=f"Invalid value '{value}' in [i]{property_name}[/] property",
bullets=[
Bullet(
f"Style flag values such as [i]{property_name}[/] expect space-separated values"
),
Bullet(f"Permitted values are {friendly_list(VALID_STYLE_FLAGS)}"),
*ContextSpecificBullets(
inline=[
Bullet(
markup="Supply a string or Style object",
examples=[
Example(
f'widget.styles.{property_name} = "bold italic underline"'
)
],
),
],
css=[
Bullet(
markup="Supply style flags separated by spaces",
examples=[Example(f"{property_name}: bold italic underline;")],
)
],
).get_by_context(context),
],
)
def table_rows_or_columns_help_text(
property_name: str, value: str, context: StylingContext
):
property_name = _contextualize_property_name(property_name, context)
return HelpText(
summary=f"Invalid value '{value}' in [i]{property_name}[/] property"
)
|