This commit is contained in:
Will McGugan
2024-11-10 13:05:57 +00:00
parent 65481b9768
commit 636cd444f4
4 changed files with 52 additions and 68 deletions

View File

@@ -105,14 +105,6 @@ class Hit:
"""Ensure 'text' is populated."""
if self.text is None:
self.text = str(self.match_display)
# if isinstance(self.match_display, str):
# self.text = self.match_display
# elif isinstance(self.match_display, Text):
# self.text = self.match_display.plain
# else:
# raise ValueError(
# "A value for 'text' is required if 'match_display' is not a str or Text"
# )
@dataclass
@@ -166,15 +158,6 @@ class DiscoveryHit:
"""Ensure 'text' is populated."""
if self.text is None:
self.text = str(self.display)
# if self.text is None:
# if isinstance(self.display, str):
# self.text = self.display
# elif isinstance(self.display, Text):
# self.text = self.display.plain
# else:
# raise ValueError(
# "A value for 'text' is required if 'display' is not a str or Text"
# )
Hits: TypeAlias = AsyncIterator["DiscoveryHit | Hit"]
@@ -959,14 +942,6 @@ class CommandPalette(SystemModalScreen):
search_value: The value to search for.
"""
# We'll potentially use the help text style a lot so let's grab it
# the once for use in the loop further down.
# help_style = self.get_component_rich_style(
# "command-palette--help-text", partial=True
# )
help_style = VisualStyle.from_styles(
self.get_component_styles("command-palette--help-text")
)
# The list to hold on to the commands we've gathered from the
# command providers.
gathered_commands: list[Command] = []
@@ -1024,32 +999,21 @@ class CommandPalette(SystemModalScreen):
# Turn the command into something for display, and add it to the
# list of commands that have been gathered so far.
# prompt = Content(str(hit.prompt), no_wrap=True, ellipsis=True)
prompt = Content.from_rich_text(hit.prompt)
if hit.help:
prompt = prompt.append("\n").append(
Content.styled(hit.help, help_style)
)
def build_prompt() -> Iterable[Content]:
assert hit is not None
yield Content.from_rich_text(hit.prompt)
if hit.help:
help_style = VisualStyle.from_styles(
self.get_component_styles("command-palette--help-text")
)
yield Content.styled(hit.help, help_style)
# if hit.help:
# help_text = Text.from_markup(hit.help)
# help_text.stylize(help_style)
# prompt = Group(prompt, help_text)
prompt = Content("\n").join(build_prompt())
gathered_commands.append(Command(prompt, hit, id=str(command_id)))
# Before we go making any changes to the UI, we do a quick
# double-check that the worker hasn't been cancelled. There's
# little point in doing UI work on a value that isn't needed any
# more.
if worker.is_cancelled:
break
# Having made it this far, it's safe to update the list of
# commands that match the input. Note that we batch up the
# results and only refresh the list once every so often; this
# helps reduce how much UI work needs to be done, but at the
# same time we keep the update frequency often enough so that it
# looks like things are moving along.
now = monotonic()
if (now - last_update) > self._RESULT_BATCH_TIME:
self._refresh_command_list(
@@ -1058,7 +1022,6 @@ class CommandPalette(SystemModalScreen):
clear_current = False
last_update = now
# Bump the ID.
command_id += 1
# Finally, get the available command from the incoming queue;

View File

@@ -168,6 +168,20 @@ class Strip:
horizontal: AlignHorizontal,
vertical: AlignVertical,
) -> Iterable[Strip]:
"""Align a list of strips on both axis.
Args:
strips: A list of strips, such as from a render.
style: The Rich style of additional space.
width: Width of container.
height: Height of container.
horizontal: Horizontal alignment method.
vertical: Vertical alignment method.
Returns:
An iterable of strips, with additional padding.
"""
if not strips:
return
line_lengths = [strip.cell_length for strip in strips]
@@ -572,6 +586,17 @@ class Strip:
return self._render_cache
def crop_pad(self, cell_length: int, left: int, right: int, style: Style) -> Strip:
"""Crop the strip to `cell_length`, and add optional padding.
Args:
cell_length: Cell length of strip prior to padding.
left: Additional padding on the left.
right: Additional padding on the right.
style: Style of any padding.
Returns:
Cropped and padded strip.
"""
if cell_length != self.cell_length:
strip = self.adjust_cell_length(cell_length, style)
else:

View File

@@ -17,7 +17,8 @@ from rich.text import Text
from textual._context import active_app
from textual.color import TRANSPARENT, Color
from textual.css.styles import Styles
from textual.css.styles import StylesBase
from textual.css.types import AlignHorizontal, AlignVertical
from textual.geometry import Spacing
from textual.render import measure
from textual.strip import Strip
@@ -135,7 +136,7 @@ class Style:
)
@classmethod
def from_styles(cls, styles: Styles) -> Style:
def from_styles(cls, styles: StylesBase) -> Style:
text_style = styles.text_style
return Style(
styles.background,
@@ -236,8 +237,22 @@ class Visual(ABC):
style: Style,
*,
pad: bool = False,
align=("left", "top"),
align: tuple[AlignHorizontal, AlignVertical] = ("left", "top"),
) -> list[Strip]:
"""High level function to render a visual to strips.
Args:
widget: Widget that produced the visual.
visual: A Visual instance.
width: Desired width (in cells).
height: Desired height (in lines).
style: A (Visual) Style instance.
pad: Pad to desired height?
align: Tuple of horizontal and vertical alignment.
Returns:
_type_: _description_
"""
strips = visual.render_strips(widget, width, height, style)
if height is None:
height = len(strips)

View File

@@ -488,29 +488,10 @@ class OptionList(ScrollView, can_focus=True):
else self.get_visual_style("option-list--option")
)
# strips = visual.render_strips(self, width, None, style=visual_style)
strips = Visual.to_strips(self, visual, width, None, visual_style, pad=True)
print(strips)
# strips = [
# strip.extend_cell_length(width, visual_style.rich_style) for strip in strips
# ]
# padding = self.get_component_styles("option-list--option").padding
# console = self.app.console
# options = console.options.update_width(width)
# if not self._wrap:
# options = options.update(no_wrap=True, overflow="ellipsis")
# if padding:
# renderable = Padding(renderable, padding)
# lines = self.app.console.render_lines(renderable, options, style=style)
style_meta = Style.from_meta({"option": option_index})
strips = [strip.apply_style(style_meta) for strip in strips]
# strips = [Strip(line, width).apply_style(style_meta) for line in lines]
self._content_render_cache[cache_key] = strips
return strips