summaryrefslogtreecommitdiffstats
path: root/examples/prompts/colored-prompt.py
blob: 1e63e296b1e9b16de650a8f20bd9f69e953f93c1 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
"""
Example of a colored prompt.
"""
from prompt_toolkit import prompt
from prompt_toolkit.formatted_text import ANSI, HTML
from prompt_toolkit.styles import Style

style = Style.from_dict(
    {
        # Default style.
        "": "#ff0066",
        # Prompt.
        "username": "#884444 italic",
        "at": "#00aa00",
        "colon": "#00aa00",
        "pound": "#00aa00",
        "host": "#000088 bg:#aaaaff",
        "path": "#884444 underline",
        # Make a selection reverse/underlined.
        # (Use Control-Space to select.)
        "selected-text": "reverse underline",
    }
)


def example_1():
    """
    Style and list of (style, text) tuples.
    """
    # Not that we can combine class names and inline styles.
    prompt_fragments = [
        ("class:username", "john"),
        ("class:at", "@"),
        ("class:host", "localhost"),
        ("class:colon", ":"),
        ("class:path", "/user/john"),
        ("bg:#00aa00 #ffffff", "#"),
        ("", " "),
    ]

    answer = prompt(prompt_fragments, style=style)
    print("You said: %s" % answer)


def example_2():
    """
    Using HTML for the formatting.
    """
    answer = prompt(
        HTML(
            "<username>john</username><at>@</at>"
            "<host>localhost</host>"
            "<colon>:</colon>"
            "<path>/user/john</path>"
            '<style bg="#00aa00" fg="#ffffff">#</style> '
        ),
        style=style,
    )
    print("You said: %s" % answer)


def example_3():
    """
    Using ANSI for the formatting.
    """
    answer = prompt(
        ANSI(
            "\x1b[31mjohn\x1b[0m@"
            "\x1b[44mlocalhost\x1b[0m:"
            "\x1b[4m/user/john\x1b[0m"
            "# "
        )
    )
    print("You said: %s" % answer)


if __name__ == "__main__":
    example_1()
    example_2()
    example_3()