Skip to content

Commit

Permalink
Allow console markup in commands
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Nov 27, 2024
1 parent f48b71b commit 4c9988e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/textual/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,8 +1120,10 @@ def build_prompt() -> Iterable[Content]:
help_style = VisualStyle.from_styles(
self.get_component_styles("command-palette--help-text")
)
yield Content.styled(hit.help, help_style)
yield Content.from_rich_text(hit.help).stylize_before(help_style)
# yield Content.styled(hit.help, help_style)

prompt = list(build_prompt())[0]
prompt = Content("\n").join(build_prompt())

gathered_commands.append(Command(prompt, hit, id=str(command_id)))
Expand Down
12 changes: 10 additions & 2 deletions src/textual/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from rich.text import Text

from textual._cells import cell_len
from textual._context import active_app
from textual._loop import loop_last
from textual.color import Color
from textual.css.types import TextAlign
Expand Down Expand Up @@ -180,7 +181,7 @@ def from_rich_text(
New Content.
"""
if isinstance(text, str):
return cls(text, align=align, no_wrap=no_wrap, ellipsis=ellipsis)
text = Text.from_markup(text)
spans = [
Span(
start,
Expand All @@ -189,6 +190,7 @@ def from_rich_text(
)
for start, end, style in text._spans
]

return cls(
text.plain,
spans,
Expand Down Expand Up @@ -681,9 +683,15 @@ def render(
return

if parse_style is None:
console = active_app.get().console
# TODO: Update when we add Content.from_markup

def get_style(style: str, /) -> Style:
return TRANSPARENT_STYLE if isinstance(style, str) else style
return (
Style.from_rich_style(console.get_style(style))
if isinstance(style, str)
else style
)

else:
get_style = parse_style
Expand Down
18 changes: 18 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from textual import events, on
from textual.app import App, ComposeResult
from textual.binding import Binding, Keymap
from textual.command import SimpleCommand
from textual.containers import Center, Container, Grid, Middle, Vertical, VerticalScroll
from textual.pilot import Pilot
from textual.renderables.gradient import LinearGradient
Expand Down Expand Up @@ -2749,3 +2750,20 @@ async def run_before(pilot: Pilot) -> None:
await pilot.click("Select")

snap_compare(TallSelectApp(), run_before=run_before)


def test_markup_command_list(snap_compare):
"""Regression test for https://github.com/Textualize/textual/issues/5276
You should see a command list, with console markup applied to the action name and help text."""

class MyApp(App):
def on_mount(self) -> None:
self.search_commands(
[
SimpleCommand(
"Hello [u green]World", lambda: None, "Help [u red]text"
)
]
)

snap_compare(MyApp())

0 comments on commit 4c9988e

Please sign in to comment.