This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ascii_art_spinner.py
90 lines (74 loc) · 1.8 KB
/
ascii_art_spinner.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
85
86
87
88
89
90
"""
ascii_art_spinner.py
Abstracts an ASCII art spinner
"""
import sys
WIDTH, HEIGHT = (0, 0)
progress = 0
terget = 100
def getTerminalSize():
import os
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
import struct
import os
cr = struct.unpack(
'hh',
fcntl.ioctl(
fd,
termios.TIOCGWINSZ,
'1234'
)
)
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
### Use get(key[, default]) instead of a try/catch
#try:
# cr = (env['LINES'], env['COLUMNS'])
#except:
# cr = (25, 80)
return int(cr[1]), int(cr[0])
def clear():
"""
Move the buffer backward WIDTH spaces
"""
sys.stdout.write('\b' * WIDTH)
sys.stdout.write(' ' * WIDTH)
sys.stdout.write('\b' * WIDTH)
def draw():
"""
Draw the ascii art
NOTE: this expects the cursor to be at the beginning
"""
sys.stdout.write('[')
blocks = int(progress / target * WIDTH)
sys.stdout.write('\u2593' * (blocks))
sys.stdout.write('\u2591' * (WIDTH - 2 - blocks))
sys.stdout.write(']')
sys.stdout.flush()
def tick():
global progress
progress += 1
clear()
draw()
def start(target_):
global target, WIDTH, HEIGHT
target = target_
WIDTH, HEIGHT = getTerminalSize()
# keeps things looking nice
WIDTH -= 1
draw()