Skip to content

Commit

Permalink
color command example (#3314)
Browse files Browse the repository at this point in the history
* color command example

* Scroll to end
  • Loading branch information
willmcgugan authored Sep 15, 2023
1 parent 129163f commit 149c5e2
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 64 deletions.
67 changes: 67 additions & 0 deletions examples/color_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from dataclasses import dataclass
from functools import partial

from textual import on
from textual._color_constants import COLOR_NAME_TO_RGB
from textual.app import App, ComposeResult
from textual.command import Hit, Hits, Provider
from textual.message import Message
from textual.widgets import Header, Static


@dataclass
class SwitchColor(Message, bubble=False):
"""Message to tell the app to switch color."""

color: str


class ColorCommands(Provider):
"""A command provider to select colors."""

async def search(self, query: str) -> Hits:
"""Called for each key."""
matcher = self.matcher(query)
for color in COLOR_NAME_TO_RGB.keys():
score = matcher.match(color)
if score > 0:
yield Hit(
score,
matcher.highlight(color),
partial(self.app.post_message, SwitchColor(color)),
)


class ColorBlock(Static):
"""Simple block of color."""

DEFAULT_CSS = """
ColorBlock{
padding: 3 6;
margin: 1 2;
color: auto;
}
"""


class ColorApp(App):
"""Experiment with the command palette."""

COMMANDS = App.COMMANDS | {ColorCommands}
TITLE = "Press ctrl + \\ and type a color"

def compose(self) -> ComposeResult:
yield Header()

@on(SwitchColor)
def switch_color(self, event: SwitchColor) -> None:
"""Adds a color block on demand."""
color_block = ColorBlock(event.color)
color_block.styles.background = event.color
self.mount(color_block)
self.screen.scroll_end()


if __name__ == "__main__":
app = ColorApp()
app.run()
2 changes: 2 additions & 0 deletions src/textual/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def post_message(self, message: "Message") -> bool:
SegmentLines = List[List["Segment"]]
CallbackType = Union[Callable[[], Awaitable[None]], Callable[[], None]]
"""Type used for arbitrary callables used in callbacks."""
IgnoreReturnCallbackType = Union[Callable[[], Awaitable[Any]], Callable[[], Any]]
"""A callback which ignores the return type."""
WatchCallbackType = Union[
Callable[[], Awaitable[None]],
Callable[[Any], Awaitable[None]],
Expand Down
11 changes: 8 additions & 3 deletions src/textual/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from .reactive import var
from .screen import ModalScreen, Screen
from .timer import Timer
from .types import CallbackType
from .types import CallbackType, IgnoreReturnCallbackType
from .widget import Widget
from .widgets import Button, Input, LoadingIndicator, OptionList, Static
from .widgets.option_list import Option
Expand Down Expand Up @@ -62,7 +62,7 @@ class Hit:
match_display: RenderableType
"""A string or Rich renderable representation of the hit."""

command: CallbackType
command: IgnoreReturnCallbackType
"""The function to call when the command is chosen."""

text: str | None = None
Expand Down Expand Up @@ -354,8 +354,13 @@ class CommandPalette(ModalScreen[CallbackType], inherit_css=False):
color: $text-muted;
}
App.-dark-mode CommandPalette > .command-palette--highlight {
text-style: bold;
color: $warning;
}
CommandPalette > .command-palette--highlight {
text-style: bold reverse;
text-style: bold;
color: $warning-darken-2;
}
CommandPalette > Vertical {
Expand Down
8 changes: 7 additions & 1 deletion src/textual/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
from ._animator import Animatable, EasingFunction
from ._context import NoActiveAppError
from ._path import CSSPathError, CSSPathType
from ._types import CallbackType, MessageTarget, WatchCallbackType
from ._types import (
CallbackType,
IgnoreReturnCallbackType,
MessageTarget,
WatchCallbackType,
)
from .actions import ActionParseResult
from .css.styles import RenderStyles
from .widgets._data_table import CursorType
Expand All @@ -19,6 +24,7 @@
"CSSPathType",
"CursorType",
"EasingFunction",
"IgnoreReturnCallbackType",
"InputValidationOn",
"MessageTarget",
"NoActiveAppError",
Expand Down
Loading

0 comments on commit 149c5e2

Please sign in to comment.