1"""
2examples.demo
3~~~~~~~~~~~~~
4
5Yaspin features demonstration.
6"""
7
8import random
9import time
10
11from yaspin import yaspin
12from yaspin.constants import COLOR_MAP
13from yaspin.spinners import Spinners
14
15
16COLORS = (k for k, v in COLOR_MAP.items() if v == "color")
17HIGHLIGHTS = (k for k, v in COLOR_MAP.items() if v == "on_color")
18
19
20def any_spinner_you_like():
21    params = [
22        ("line", 0.8),
23        ("dots10", 0.8),
24        ("dots11", 0.8),
25        ("simpleDots", 1),
26        ("star", 0.5),
27        ("balloon", 0.7),
28        ("noise", 0.5),
29        ("arc", 0.5),
30        ("arrow2", 0.5),
31        ("bouncingBar", 1),
32        ("bouncingBall", 1),
33        ("smiley", 0.7),
34        ("hearts", 0.5),
35        ("clock", 0.7),
36        ("earth", 0.7),
37        ("moon", 0.7),
38        ("runner", 0.5),
39        ("pong", 1),
40        ("shark", 1.5),
41        ("christmas", 0.5),
42    ]
43    random.shuffle(params)
44
45    # Setup printing
46    max_len = 45
47    msg = "[Any spinner you like]"
48
49    for name, period in params:
50        spinner = getattr(Spinners, name)
51
52        spaces_qty = max_len - len(name) - len(msg) - len(spinner.frames[0])
53        text = "{0}{1}{2}".format(name, " " * spaces_qty, msg)
54
55        with yaspin(spinner, text=text, color="cyan"):
56            time.sleep(period)
57
58
59def colors_simple(sleep=0.7):
60    # Setup printing
61    max_len = 25
62    msg = "[Colors]"
63
64    with yaspin(Spinners.dots12) as sp:
65        for color in COLORS:
66            spaces_qty = max_len - len(color) - len(msg)
67            text = "{0}{1}{2}".format(color, " " * spaces_qty, msg)
68
69            sp.color = color
70            sp.text = text
71            time.sleep(sleep)
72
73
74def color_highlights(sleep=0.5):
75    # Setup printing
76    max_len = 40
77    msg = "[Color Highlights]"
78
79    with yaspin(Spinners.bouncingBall) as sp:
80        for highlight in HIGHLIGHTS:
81            name = "On {0} color".format(highlight.split("_")[1])
82            spaces_qty = max_len - len(name) - len(msg)
83            text = "{0}{1}{2}".format(name, " " * spaces_qty, msg)
84
85            sp.on_color = highlight
86            sp.text = text
87
88            time.sleep(sleep)
89
90
91def color_attributes(sleep=0.8):
92    descriptions = [
93        "Bold white color",
94        "Dark red color",
95        "Underline green color",
96        "Blink yellow color",
97        "Reverse blue color",
98        "Concealed magenta color",
99    ]
100    # Setup printing
101    max_len = 42
102    msg = "[Color Attributes]"
103
104    with yaspin(Spinners.bouncingBall) as sp:
105        for descr in descriptions:
106            spaces_qty = max_len - len(descr) - len(msg)
107            text = "{0}{1}{2}".format(descr, " " * spaces_qty, msg)
108
109            attr, color, _ = descr.split()
110            sp.attrs, sp.color = [attr.lower()], color
111            sp.text = text
112
113            time.sleep(sleep)
114
115
116def color_craziness(sleep=1.3):
117    descriptions = [
118        "Bold underline reverse cyan color",
119        "Dark blink concealed white color",
120        "Underline red on_grey color",
121        "Reversed green on_red color",
122    ]
123
124    def parse_attrs(description):
125        """Parse attributes from text description."""
126        attrs = []
127        for word in description.split():
128            attr = word.lower()
129            sp = yaspin()
130            try:
131                getattr(sp, attr)
132            except AttributeError:
133                continue
134            else:
135                attrs.append(attr)
136        return attrs
137
138    # Setup printing
139    max_len = 58
140    msg = "[Color Craziness �� ]"
141
142    # New spinner instance should be created every iteration since
143    # multiple simultaneous color attributes are supported. Hence,
144    # updating attribute of the instance will add new attribute to
145    # the existing list of previous attributes.
146    for descr in descriptions:
147        spaces_qty = max_len - len(descr) - len(msg)
148        text = "{0}{1}{2}".format(descr, " " * spaces_qty, msg)
149
150        with yaspin(Spinners.pong, text=text) as sp:
151            # Apply all color attributes from description
152            for attr in parse_attrs(descr):
153                getattr(sp, attr)
154
155            time.sleep(sleep)
156
157
158def right_spinner(sleep=2):
159    with yaspin(text="Right spinner", side="right", color="cyan") as sp:
160        time.sleep(sleep)
161
162        # Switch to left spinner
163        sp.side = "left"
164        sp.text = "Left spinner"
165
166        time.sleep(sleep)
167
168
169def reversed_spinner(sleep=1):
170    with yaspin(text="Reversed spinner", reversal=True, color="cyan") as sp:
171        time.sleep(sleep)
172
173        sp.spinner = Spinners.line
174
175        time.sleep(sleep)
176
177        sp.text = "Enjoy!"
178        sp.ok("☀️ ")
179
180
181def main():
182    any_spinner_you_like()
183    colors_simple()
184    color_highlights()
185    color_attributes()
186    color_craziness()
187    right_spinner()
188    reversed_spinner()
189
190
191if __name__ == "__main__":
192    main()
193