File: examples.py

package info (click to toggle)
python-spinners 0.0~git20200220.a73d561-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 912 kB
  • sloc: python: 961; makefile: 3
file content (111 lines) | stat: -rw-r--r-- 2,388 bytes parent folder | download | duplicates (2)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""Examples demonstrating various spinners.

Attributes
----------
CLEAR_LINE : str
    Ascii to clear the current line
"""
from __future__ import print_function

import os
os.sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import sys
import time
import codecs

try:
    import cursor
except ImportError:
    import subprocess

    if sys.version_info.major == 2:
        subprocess.call(['pip2', 'install', 'cursor'])
    else:
        subprocess.call(['pip3', 'install', 'cursor'])

    import cursor

from spinners import Spinners

CLEAR_LINE = '\033[K'

def decode_utf_8_text(text):
    """Decodes the text from utf-8 format.
    
    Parameters
    ----------
    text : str
        Text to be decoded
    
    Returns
    -------
    str
        Decoded text
    """
    try:
        return codecs.decode(text, 'utf-8')
    except:
        return text


def encode_utf_8_text(text):
    """Encodes the text to utf-8 format
    
    Parameters
    ----------
    text : str
        Text to be encoded
    
    Returns
    -------
    str
        Encoded text
    """
    try:
        return codecs.encode(text, 'utf-8')
    except:
        return text

if sys.version_info.major == 2:
    get_coded_text = encode_utf_8_text
else:
    get_coded_text = decode_utf_8_text

def animate(frames, interval, name, iterations=2):
    """Animate given frame for set number of iterations.

    Parameters
    ----------
    frames : list
        Frames for animating
    interval : float
        Interval between two frames
    name : str
        Name of animation
    iterations : int, optional
        Number of loops for animations
    """
    for i in range(iterations):
        for frame in frames:
            frame = get_coded_text(frame)
            output = "\r{0} {1}".format(frame, name)
            sys.stdout.write(output)
            sys.stdout.write(CLEAR_LINE)
            sys.stdout.flush()
            time.sleep(0.001 * interval)

try:
    cursor.hide()
    spinners = [spinner for spinner in Spinners] # pylint: disable=not-an-iterable
    sorted_spinners = sorted(spinners, key=lambda spinner: spinner.name)

    for spinner in sorted_spinners:
        frames = spinner.value['frames']
        interval = spinner.value['interval']
        name = spinner.name
        animate(frames, interval, name)

    print('\n')
finally:
    cursor.show()