1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 + p 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()
|