-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.py
More file actions
102 lines (69 loc) · 2.74 KB
/
Copy pathprogress.py
File metadata and controls
102 lines (69 loc) · 2.74 KB
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
import threading
import sys
import time
class Progress(threading.Thread):
def __init__(self):
super().__init__()
#define global variables.
self.total_lines = 0
self.current_line = 0
self.p_map = {} #percentage map
self.is_running = False
def start(self):
pass
def end(self):
pass
def update_current(self, current_line):
self.current_line = current_line
def fetch_current_percentage(self):
# Finds current line in hashmap
if self.current_line == 0:
return 0
return self.p_map[self.current_line]
def format_output_string(self, percentage):
#Slices string to fit the current percentage.
percentage = self.fetch_current_percentage()
hash_count = int((percentage / 100) * self.bar_length)
bar = '#' * hash_count
spaces = ' ' * (self.bar_length - hash_count)
return f"[{bar}{spaces}] Progress: {percentage}%"
def percentage_map(total_lines):
percentage_map = {}
for i in range(1, total_lines + 1):
percentage = int((i / total_lines) * 100)
percentage_map[i] = percentage
return percentage_map
def get_line_range(filepath):
valid_lines = 0
with open(filepath, 'r') as file:
for i in file:
clean_lines = i.strip()
if not i == "" or i.startswith("#"):
valid_lines += 1
return valid_lines
def prepare_load_visual(kill_status):
#make sure to pass the percentage map thread id into the parameter field
#Creating spinning cursor for each thread while a thread is mapping lines
#Trigger by doing kill_status = threading.Event() and add it as a thread argument
spinner_frames = ['|', '/', '-', '\\']
idx = 0
while not kill_status.is_set():
frame = spinner_frames[idx]
sys.stdout.write(f"\r\033[K[ {frame} ] Initializing setup...")
sys.stdout.flush()
idx = (idx + 1) % len(spinner_frames)
time.sleep(0.1)
def grab_and_display(data_thread, loading_thread, kill_flag):
# When percentage map thread is complete, kills pre-loading spinner, and clears screen for new loading bar
if data_thread:
data_thread.join()
if kill_flag:
kill_flag.set()
if loading_thread:
loading_thread.join()
sys.stdout.write("\r\033[K")
sys.stdout.flush()
def ansi_escape_format(progress_string):
#Use ansi escape codes to keep our processed string cleanly in one position at all times, without disrupting additional output
sys.stdout.write(f"\r\033[K{progress_string}")
sys.stdout.flush()