Optimize Progress Bar

This commit is contained in:
1826013250 2023-04-18 00:44:01 +08:00
parent 18de4e1cea
commit f0ce51e87d
3 changed files with 29 additions and 41 deletions

View File

@ -8,7 +8,6 @@ from queue import Empty
from time import sleep from time import sleep
import mutagen.mp3 import mutagen.mp3
from progress.bar import Bar
from Cryptodome.Cipher import AES from Cryptodome.Cipher import AES
from mutagen import File, flac from mutagen import File, flac
from mutagen.id3 import ID3, TPE1, APIC, COMM, TIT2, TALB from mutagen.id3 import ID3, TPE1, APIC, COMM, TIT2, TALB
@ -16,6 +15,7 @@ from mutagen.id3 import ID3, TPE1, APIC, COMM, TIT2, TALB
from modules.utils.clear_screen import clear from modules.utils.clear_screen import clear
from modules.functions.get_song import get_song_lyric from modules.functions.get_song import get_song_lyric
from modules.utils.inputs import cinput, rinput from modules.utils.inputs import cinput, rinput
from modules.utils.bar import CompactBar
def load_information_from_song(path) -> str | dict: def load_information_from_song(path) -> str | dict:
@ -137,15 +137,15 @@ def load_and_decrypt_from_ncm(file_path, target_dir) -> dict: # nondanee的源
# 对解密后的文件进行信息补全 # 对解密后的文件进行信息补全
if meta_data["format"] == "mp3": # 针对 mp3 使用 ID3 进行信息补全 if meta_data["format"] == "mp3": # 针对 mp3 使用 ID3 进行信息补全
audio = File(os.path.join(target_dir, os.path.splitext(file_path.split("/")[-1])[0] + ".mp3")) audio = ID3(os.path.join(target_dir, os.path.splitext(file_path.split("/")[-1])[0] + ".mp3"))
artists = [] artists = []
for i in meta_data["artist"]: for i in meta_data["artist"]:
artists.append(i[0]) artists.append(i[0])
audio.tags["TPE1"] = TPE1(encoding=3, text=artists) # 插入歌手 audio["TPE1"] = TPE1(encoding=3, text=artists) # 插入歌手
audio.tags["APIC"] = APIC(encoding=3, mime='image/jpg', type=3, desc='', data=image_data) # 插入封面 audio["APIC"] = APIC(encoding=3, mime='image/jpg', type=3, desc='', data=image_data) # 插入封面
audio.tags["COMM::XXX"] = COMM(encoding=3, lang='XXX', desc='', text=[comment.decode("utf-8")]) # 插入 163 key 注释 audio["COMM::XXX"] = COMM(encoding=3, lang='XXX', desc='', text=[comment.decode("utf-8")]) # 插入 163 key 注释
audio.tags["TIT2"] = TIT2(encoding=3, text=[meta_data["musicName"]]) # 插入歌曲名 audio["TIT2"] = TIT2(encoding=3, text=[meta_data["musicName"]]) # 插入歌曲名
audio.tags["TALB"] = TALB(encoding=3, text=[meta_data["album"]]) # 插入专辑名 audio["TALB"] = TALB(encoding=3, text=[meta_data["album"]]) # 插入专辑名
audio.save() audio.save()
elif meta_data["format"] == "flac": # 针对 flac 使用 FLAC 进行信息补全 elif meta_data["format"] == "flac": # 针对 flac 使用 FLAC 进行信息补全
audio = flac.FLAC(os.path.join(target_dir, os.path.splitext(file_path.split("/")[-1])[0] + ".flac")) audio = flac.FLAC(os.path.join(target_dir, os.path.splitext(file_path.split("/")[-1])[0] + ".flac"))
@ -236,8 +236,8 @@ def get_lyric_from_folder(self):
max_process = 20 # 最大进程数 max_process = 20 # 最大进程数
current_process = 0 # 当前正在活动的进程数 current_process = 0 # 当前正在活动的进程数
passed = 0 # 总共结束的进程数 passed = 0 # 总共结束的进程数
with Bar(f"正在破解 %(index){len(str(len(ncm_files)))}d/%(max)d", with CompactBar(f"正在破解 %(index){len(str(len(ncm_files)))}d/%(max)d",
suffix="", max=len(ncm_files), color="blue") as bar: suffix="", max=len(ncm_files), color="blue") as bar:
total = len(ncm_files) total = len(ncm_files)
allocated = 0 # 已经分配的任务数量 allocated = 0 # 已经分配的任务数量
while True: # 进入循环,执行 新建进程->检测队列->检测任务完成 的循环 while True: # 进入循环,执行 新建进程->检测队列->检测任务完成 的循环
@ -251,8 +251,6 @@ def get_lyric_from_folder(self):
q_info)).start() q_info)).start()
allocated += 1 allocated += 1
current_process += 1 current_process += 1
bar.suffix = f"已分配: {ncm_files[allocated-1]}"
print(len(bar.fill))
bar.update() bar.update()
while True: # 错误队列检测 while True: # 错误队列检测
try: try:
@ -269,8 +267,8 @@ def get_lyric_from_folder(self):
musics.append({"id": r['musicId'], "name": r["musicName"], "artists": r["artist"]}) musics.append({"id": r['musicId'], "name": r["musicName"], "artists": r["artist"]})
passed += 1 passed += 1
current_process -= 1 current_process -= 1
bar.suffix = f"已完成: {r['musicName']} - "\ bar.print_onto_bar(f"已完成: {r['musicName']} - "
f"{''.join([x + ', ' for x in [x[0] for x in r['artist']]])[:-2]}" f"{''.join([x + ', ' for x in [x[0] for x in r['artist']]])[:-2]}")
bar.next() bar.next()
except Empty: except Empty:
break break

View File

@ -1,33 +1,18 @@
"""一些有关调整进度条的设置""" """修改了进度条"""
from os import get_terminal_size from os import get_terminal_size
from progress.bar import Bar from progress.bar import Bar
from modules.utils.length import get_more_length
def suit_length(bar: Bar, original: Bar):
"""尽量调整进度条大小去匹配终端宽度""" class CompactBar(Bar):
length = get_terminal_size().columns def print_onto_bar(self, message: str):
original_length = original.width + len((original.message + print()
original.bar_suffix + self.update()
original.bar_prefix + print(f"\x1b[1A\x1b[{get_terminal_size().columns}D{(get_terminal_size().columns-1) * ' '}"
original.suffix) % bar) f"\x1b[{get_terminal_size().columns}D"+message[:(get_terminal_size().columns - get_more_length(message))])
if length >= original_length: self.update()
return
if length < len(str(bar.max) + "/" + str(bar.index)): def next_without_newline(self):
bar.bar_suffix = bar.suffix = bar.bar_prefix = "" self.next()
bar.width = 0
bar.suffix = Bar.suffix
elif length >= original_length - bar.width:
bar.width = length - len((bar.message + bar.bar_suffix + bar.bar_prefix + bar.suffix) % bar)
elif length >= original_length - (bar.width + len(bar.suffix % bar)) and "(index)" in bar.message:
bar.suffix = ""
bar.width = length - len((bar.message + bar.bar_suffix + bar.bar_prefix + bar.suffix) % bar)
elif length >= original_length - (bar.width + len(bar.message % bar)) and "(index)" in bar.suffix:
bar.message = ""
bar.width = length - len((bar.message + bar.bar_suffix + bar.bar_prefix + bar.suffix) % bar)
elif length >= original_length - (bar.width + len((bar.message + bar.suffix) % bar)) + Bar.suffix % bar:
bar.message = ""
bar.suffix = Bar.suffix
bar.width = length - len((bar.message + bar.bar_suffix + bar.bar_prefix + bar.suffix) % bar)
bar.update()
return

View File

@ -5,3 +5,8 @@ def len_abs(content):
"""针对中文将一个汉字识别为2个长度而不是1个""" """针对中文将一个汉字识别为2个长度而不是1个"""
return len(content) + (len(content.encode("utf-8")) - len(content)) // 2 return len(content) + (len(content.encode("utf-8")) - len(content)) // 2
def get_more_length(content):
"""将相对于正常长度的超出值返回"""
return (len(content.encode("utf-8")) - len(content)) // 2