File size: 1,174 Bytes
70e3e61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
""" progress bar"""

import time
import sys

def print_progress_bar(duration, msg='', bar_length=50):
    """
    Prints a progress bar with a message to the terminal.

    :param duration: The number of seconds the progress bar will run.
    :param msg: The message to display before the progress bar.
    :param bar_length: The character length of the bar (default is 50).
    """
    for i in range(duration):
        # Calculate the percentage completion.
        percent = (i + 1) / duration
        filled_length = int(bar_length * percent)

        # Create the bar string.
        progress = '█' * filled_length + '-' * (bar_length - filled_length)

        # Print the progress bar with the message.
        sys.stdout.write(f'\r{msg} |{progress}| {percent*100:.2f}% Complete')
        sys.stdout.flush()

        # Sleep for one second.
        time.sleep(1)

    # Print a newline at the end to ensure the next terminal output is on a new line.
    print()

# You can add some testing code here to see how it works when you run this module directly.
if __name__ == '__main__':
    # Test the progress bar function.
    print_progress_bar(60, msg='Loading')