mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Some output redesign/improvements
This commit is contained in:
@@ -7,8 +7,9 @@
|
||||
}
|
||||
|
||||
.list-item {
|
||||
height: 8;
|
||||
height: 8x;
|
||||
min-width: 80;
|
||||
background: dark_blue;
|
||||
padding: 1 2 3;
|
||||
padding: 2%;
|
||||
opacity: x;
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class HelpText:
|
||||
def __rich_console__(
|
||||
self, console: Console, options: ConsoleOptions
|
||||
) -> RenderResult:
|
||||
tree = Tree(_markup_and_highlight(f"[b red]{self.summary}"), guide_style="red")
|
||||
tree = Tree(_markup_and_highlight(f"[b blue]{self.summary}"), guide_style="dim")
|
||||
for bullet in self.bullets:
|
||||
tree.add(bullet)
|
||||
yield tree
|
||||
|
||||
@@ -77,14 +77,48 @@ def _contextualize_property_name(
|
||||
return property_name
|
||||
|
||||
|
||||
def spacing_help_text(
|
||||
def _spacing_examples(property_name: str) -> ContextSpecificBullets:
|
||||
return ContextSpecificBullets(
|
||||
inline=[
|
||||
Bullet(
|
||||
"In Python, you can set it 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(
|
||||
"In Textual CSS, 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 spacing_wrong_number_of_values(
|
||||
property_name: str,
|
||||
num_values_supplied: int,
|
||||
context: StylingContext | None = None,
|
||||
) -> HelpText:
|
||||
property_name = _contextualize_property_name(property_name, context)
|
||||
return HelpText(
|
||||
summary=f"Invalid value for the [i]{property_name}[/] property",
|
||||
summary=f"Invalid number of values for the [i]{property_name}[/] property",
|
||||
bullets=[
|
||||
Bullet(
|
||||
f"You supplied {num_values_supplied} values for the '{property_name}' property"
|
||||
@@ -92,43 +126,21 @@ def spacing_help_text(
|
||||
Bullet(
|
||||
"Spacing properties like 'margin' and 'padding' require either 1, 2 or 4 integer values"
|
||||
),
|
||||
*ContextSpecificBullets(
|
||||
inline=[
|
||||
Bullet(
|
||||
"In Python, you can set it 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(
|
||||
"In Textual CSS, supply 1, 2 or 4 values 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"
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
).get_by_context(context),
|
||||
*_spacing_examples(property_name).get_by_context(context),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def spacing_invalid_value(
|
||||
property_name: str, context: StylingContext | None = None
|
||||
) -> HelpText:
|
||||
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 | None = None
|
||||
) -> HelpText:
|
||||
|
||||
@@ -15,7 +15,7 @@ from typing import Iterable, NamedTuple, TYPE_CHECKING, cast
|
||||
import rich.repr
|
||||
from rich.style import Style
|
||||
|
||||
from ._help_text import spacing_help_text, scalar_help_text
|
||||
from ._help_text import spacing_wrong_number_of_values, scalar_help_text
|
||||
from ..color import Color, ColorPair
|
||||
from ._error_tools import friendly_list
|
||||
from .constants import NULL_SPACING
|
||||
@@ -380,7 +380,7 @@ class SpacingProperty:
|
||||
except ValueError as error:
|
||||
raise StyleValueError(
|
||||
str(error),
|
||||
help_text=spacing_help_text(
|
||||
help_text=spacing_wrong_number_of_values(
|
||||
property_name=self.name,
|
||||
num_values_supplied=len(spacing),
|
||||
context="inline",
|
||||
|
||||
@@ -6,7 +6,11 @@ import rich.repr
|
||||
|
||||
from ._error_tools import friendly_list
|
||||
from ._help_renderables import HelpText
|
||||
from ._help_text import spacing_help_text, scalar_help_text
|
||||
from ._help_text import (
|
||||
spacing_wrong_number_of_values,
|
||||
scalar_help_text,
|
||||
spacing_invalid_value,
|
||||
)
|
||||
from .constants import (
|
||||
VALID_BORDER,
|
||||
VALID_BOX_SIZING,
|
||||
@@ -292,14 +296,16 @@ class StylesBuilder:
|
||||
try:
|
||||
append(int(value))
|
||||
except ValueError:
|
||||
self.error(name, token, f"expected a number here; found {value!r}")
|
||||
self.error(name, token, spacing_invalid_value(name, context="css"))
|
||||
else:
|
||||
self.error(name, token, f"expected a number here; found {value!r}")
|
||||
self.error(name, token, spacing_invalid_value(name, context="css"))
|
||||
if len(space) not in (1, 2, 4):
|
||||
self.error(
|
||||
name,
|
||||
tokens[0],
|
||||
spacing_help_text(name, num_values_supplied=len(space), context="css"),
|
||||
spacing_wrong_number_of_values(
|
||||
name, num_values_supplied=len(space), context="css"
|
||||
),
|
||||
)
|
||||
self.styles._rules[name] = Spacing.unpack(cast(SpacingDimensions, tuple(space)))
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ from rich.console import RenderableType, Console, ConsoleOptions
|
||||
from rich.highlighter import ReprHighlighter
|
||||
from rich.markup import render
|
||||
from rich.padding import Padding
|
||||
from rich.panel import Panel
|
||||
from rich.rule import Rule
|
||||
from rich.style import Style
|
||||
from rich.syntax import Syntax
|
||||
@@ -83,26 +84,33 @@ class StylesheetErrors:
|
||||
if token.referenced_by:
|
||||
line_idx, col_idx = token.referenced_by.location
|
||||
line_no, col_no = line_idx + 1, col_idx + 1
|
||||
path_string = f"{filename}:{line_no}:{col_no}"
|
||||
path_string = f"{path.absolute()}:{line_no}:{col_no}"
|
||||
else:
|
||||
line_idx, col_idx = token.location
|
||||
line_no, col_no = line_idx + 1, col_idx + 1
|
||||
path_string = f"{filename}:{line_no}:{col_no}"
|
||||
path_string = f"{path.absolute()}:{line_no}:{col_no}"
|
||||
|
||||
link_style = Style(
|
||||
link=f"file://{path.absolute()}", color="red", bold=True
|
||||
link=f"file://{path.absolute()}",
|
||||
color="red",
|
||||
bold=True,
|
||||
italic=True,
|
||||
)
|
||||
|
||||
path_text = Text(path_string, style=link_style)
|
||||
title = Text.assemble(Text("Error at ", style="bold red"), path_text)
|
||||
yield Rule(style="red")
|
||||
yield Padding(title, pad=(0, 0, 0, 1))
|
||||
yield Padding(self._get_snippet(token.code, line_no), pad=(0, 0, 1, 1))
|
||||
yield Padding(message, pad=(0, 0, 0, 1))
|
||||
if is_last:
|
||||
yield Rule(style="red")
|
||||
yield ""
|
||||
yield Panel(
|
||||
self._get_snippet(token.code, line_no),
|
||||
title=title,
|
||||
title_align="left",
|
||||
border_style="red",
|
||||
)
|
||||
yield Padding(message, pad=(0, 0, 1, 3))
|
||||
|
||||
yield Rule(style="red")
|
||||
yield render(
|
||||
f" [b]{error_count} error{'s' if error_count != 1 else ''}[/] found in the stylesheet"
|
||||
f" [b]{error_count} error{'s' if error_count != 1 else ''}[/] found in stylesheet"
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user