68 lines
2.1 KiB
Python
68 lines
2.1 KiB
Python
from dataclasses import dataclass
|
|
from urllib.parse import quote, urlparse, urlunparse
|
|
from os import get_terminal_size
|
|
from colored import fg, bg, attr
|
|
|
|
|
|
@dataclass
|
|
class RGB:
|
|
r: int = 0
|
|
g: int = 0
|
|
b: int = 0
|
|
|
|
|
|
def fix_url(url: str):
|
|
parts = urlparse(str(url))
|
|
return urlunparse(parts._replace(path=quote(parts.path)))
|
|
|
|
|
|
def progress_bar(filename: bytes or str, current: int, total: int) -> None:
|
|
_filename: str
|
|
if type(filename) is str:
|
|
_filename = filename
|
|
else:
|
|
_filename = filename.decode()
|
|
|
|
def return_diff_color(c1: RGB, c2: RGB, percent: int) -> RGB:
|
|
def return_diff(n1, n2, _percent=100) -> int:
|
|
if n1 > n2:
|
|
return n1 - int((n1 - n2) * (_percent / 100))
|
|
elif n1 < n2:
|
|
return n1 + int((n2 - n1) * (_percent / 100))
|
|
return n1
|
|
|
|
new_rgb = RGB(r=return_diff(c1.r, c2.r, percent), g=return_diff(c1.g, c2.g, percent),
|
|
b=return_diff(c1.b, c2.b, percent))
|
|
return new_rgb
|
|
|
|
def color_to_hex(color: RGB) -> str:
|
|
def return_hex_number(n: int):
|
|
hnum = hex(int(n))
|
|
return f'{str(hnum).replace("0x", "").zfill(2)}'
|
|
|
|
r: str = return_hex_number(color.r)
|
|
g: str = return_hex_number(color.g)
|
|
b: str = return_hex_number(color.b)
|
|
return f"{r}{g}{b}"
|
|
|
|
base_color = RGB(r=68, g=121, b=84)
|
|
end_color = RGB(r=0, g=255, b=68)
|
|
|
|
loading_chars = "|/-\\"
|
|
try:
|
|
screen_size = get_terminal_size().columns()
|
|
except Exception:
|
|
screen_size = 120
|
|
available_colums = int(screen_size / 100 * 50)
|
|
|
|
percent = int(float(current) / float(total) * 100)
|
|
_n = percent % len(loading_chars)
|
|
|
|
load_bar = "=" * int((available_colums / 100) * percent) + '=>'
|
|
space_filling = " " * int(available_colums-len(load_bar))
|
|
|
|
_color = f'#{color_to_hex(return_diff_color(base_color, end_color, percent))}'
|
|
print(
|
|
f'\t[{loading_chars[_n]}] {fg(_color)}{load_bar}{attr("reset")} [{percent}%] {space_filling} {[" "," ",""][len(str(percent))-1]}[{(_filename[:75] + "..") if len(_filename) > 75 else _filename}]',
|
|
end='\r')
|