Textualize / textual

The lean application framework for Python. Build sophisticated user interfaces with a simple Python API. Run your apps in the terminal and a web browser.
https://textual.textualize.io/
MIT License
25.23k stars 774 forks source link

Several command palette issues on 0.71.0 #4705

Closed darrenburns closed 2 months ago

darrenburns commented 3 months ago

I found 3 issues with the command palette in the latest release.

  1. The help text doesn't appear for the initial "discovery" hits. When you start to search, the help text starts to appear.
  2. There's something wrong with scrolling when you use the keyboard to move the cursor through the option list. In the example below, scrolling seems to break after you've started searching.
  3. The initial commands listed (DiscoveryHits) are sorted alphabetically, which seems wrong (I want control over the order - it's easy enough for me to sort my commands if that's what I require). Normal hits from searches are not sorted (as expected).
from typing import Any
from textual.app import App, ComposeResult
from textual.command import DiscoveryHit, Hit, Hits, Provider
from textual.widgets import Label

class MyProvider(Provider):
    @property
    def commands(self) -> list[tuple[str, Any, str]]:
        return [(f"option-{i}", lambda: 1, f"This is option {i}") for i in range(30)]

    async def discover(self) -> Hits:
        for name, runnable, help_text in self.commands:
            yield DiscoveryHit(
                name,
                runnable,
                help_text,
            )

    async def search(self, query: str) -> Hits:
        matcher = self.matcher(query)
        for name, runnable, help_text in self.commands:
            if (match := matcher.match(name)) > 0:
                yield Hit(
                    match,
                    matcher.highlight(name),
                    runnable,
                    help=help_text,
                )

class OptionListScrolling(App[None]):
    COMMANDS = {MyProvider}

    def compose(self) -> ComposeResult:
        yield Label("hey!")

if __name__ == "__main__":
    app = OptionListScrolling()
    app.run()
TomJGooding commented 3 months ago

The help text doesn't appear for the initial "discovery" hits. When you start to search, the help text starts to appear.

It looks like you just need a small tweak to your DiscoveryHit:


    async def discover(self) -> Hits:
        for name, runnable, help_text in self.commands:
            yield DiscoveryHit(
                name,
                runnable,
                help=help_text,
            )
TomJGooding commented 3 months ago

There's something wrong with scrolling when you use the keyboard to move the cursor through the option list.

From a quick look at the recent changes to the OptionList, I'm not sure the "line position for the start of the option" is correctly tracked here?

https://github.com/Textualize/textual/blob/37f0f26f1c9380a9fe45866dd383005686df7839/src/textual/widgets/_option_list.py#L363-L364

github-actions[bot] commented 2 months ago

Don't forget to star the repository!

Follow @textualizeio for Textual updates.