-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyspinner.py
executable file
·84 lines (67 loc) · 2.21 KB
/
myspinner.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#title: myspinner.py
#description: My CLI Spinner
#author: Ricky Laney
#date: 20190118
#version: 0.0.1
#usage: python myspinner.py or ./myspinner.py
#notes:
#python_version: 3.6.5
#==============================================================================
import sys
import threading
import time
import itertools
class Spinner(object):
spinner_cycle = itertools.cycle(u'⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏')
def __init__(self, beep=False, force=False):
self.beep = beep
self.force = force
self.stop_running = None
self.spin_thread = None
def start(self):
if sys.stdout.isatty() or self.force:
self.stop_running = threading.Event()
self.spin_thread = threading.Thread(target=self.init_spin)
self.spin_thread.start()
def stop(self):
if self.spin_thread:
self.stop_running.set()
self.spin_thread.join()
def init_spin(self):
while not self.stop_running.is_set():
next_val = next(self.spinner_cycle)
if sys.version_info[0] == 2:
next_val = next_val.encode('utf-8')
sys.stdout.write(next_val)
sys.stdout.flush()
time.sleep(0.07)
sys.stdout.write('\b')
def __enter__(self):
self.start()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.stop()
if self.beep:
sys.stdout.write('\7')
sys.stdout.flush()
return False
def spinner(beep=False, force=False):
"""This function creates a context manager that is used to display a
spinner on stdout as long as the context has not exited.
The spinner is created only if stdout is not redirected, or if the spinner
is forced using the `force` parameter.
Parameters
----------
beep : bool
Beep when spinner finishes.
force : bool
Force creation of spinner even when stdout is redirected.
Example
-------
with spinner():
do_something()
do_something_else()
"""
return Spinner(beep, force)