-
Notifications
You must be signed in to change notification settings - Fork 0
/
loading.py
43 lines (36 loc) · 1.11 KB
/
loading.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
import sys
import threading
import time
class Loading:
def __init__(self):
self.spinner = '|/-\\'
self.spinner_index = 0
self.running = False
self.thread = None
def hide_cursor(self):
sys.stdout.write('\033[?25l')
sys.stdout.flush()
def show_cursor(self):
sys.stdout.write('\033[?25h')
sys.stdout.flush()
def clear_line(self):
sys.stdout.write('\r\033[K')
sys.stdout.flush()
def update(self):
while self.running:
sys.stdout.write('\rWaiting for assistant response... ' + self.spinner[self.spinner_index])
sys.stdout.flush()
self.spinner_index = (self.spinner_index + 1) % len(self.spinner)
time.sleep(0.1)
self.clear_line()
self.show_cursor()
def start(self):
if not self.running:
self.running = True
self.hide_cursor()
self.thread = threading.Thread(target=self.update)
self.thread.start()
def stop(self):
if self.running:
self.running = False
self.thread.join()