File: basic_two_column_heavy_styles.py

package info (click to toggle)
textual-autocomplete 4.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 788 kB
  • sloc: python: 1,835; makefile: 4
file content (55 lines) | stat: -rw-r--r-- 1,795 bytes parent folder | download
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
"""A two-column autocomplete example with heavy styling."""

from textual.app import App, ComposeResult
from textual.content import Content
from textual.widgets import Input, Label

from textual_autocomplete import AutoComplete, DropdownItem
from examples._headers import headers

# Define a mapping of sections to colors
SECTION_COLORS: dict[str, str] = {
    "Authentication": "$text-success",
    "Caching": "$text-primary",
    "Conditionals": "$text-warning",
    "Connection management": "$text-error",
    "Content negotiation": "$text-success",
    "Controls": "$text-accent",
    "Cookies": "$text-warning",
    "CORS": "$text-error",
    # Add fallback color for any other sections
    "default": "$foreground",
}

# Create dropdown items with two columns: rank and language name
CANDIDATES = [
    DropdownItem(
        Content.styled(
            str(header["name"]),
            style=SECTION_COLORS.get(
                str(header.get("section", "default")), SECTION_COLORS["default"]
            ),
        ),  # Main text to be completed with color based on section
        prefix=Content.from_markup(
            "[$text-primary on $primary-muted] $number", number=f"{i:<3}"
        ),  # Prefix showing rank, styled with Textual markup!
    )
    for i, header in enumerate(headers)
]


class TwoColumnAutoCompleteExample(App[None]):
    def compose(self) -> ComposeResult:
        yield Label("Start typing an HTTP header name:")
        text_input = Input(placeholder="Type here...")
        yield text_input

        yield AutoComplete(
            target=text_input,  # The widget to attach autocomplete to
            candidates=CANDIDATES,  # The list of completion candidates
        )


if __name__ == "__main__":
    app = TwoColumnAutoCompleteExample()
    app.run()