From f0ce51e87ddbdbb6761b879a744a4323c2d52431 Mon Sep 17 00:00:00 2001 From: 1826013250 <1826013250@qq.com> Date: Tue, 18 Apr 2023 00:44:01 +0800 Subject: [PATCH] Optimize Progress Bar --- modules/functions/load_file_song.py | 24 ++++++++--------- modules/utils/bar.py | 41 +++++++++-------------------- modules/utils/length.py | 5 ++++ 3 files changed, 29 insertions(+), 41 deletions(-) diff --git a/modules/functions/load_file_song.py b/modules/functions/load_file_song.py index dda7f6d..0d36d83 100644 --- a/modules/functions/load_file_song.py +++ b/modules/functions/load_file_song.py @@ -8,7 +8,6 @@ from queue import Empty from time import sleep import mutagen.mp3 -from progress.bar import Bar from Cryptodome.Cipher import AES from mutagen import File, flac 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.functions.get_song import get_song_lyric from modules.utils.inputs import cinput, rinput +from modules.utils.bar import CompactBar 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 进行信息补全 - 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 = [] for i in meta_data["artist"]: artists.append(i[0]) - audio.tags["TPE1"] = TPE1(encoding=3, text=artists) # 插入歌手 - audio.tags["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.tags["TIT2"] = TIT2(encoding=3, text=[meta_data["musicName"]]) # 插入歌曲名 - audio.tags["TALB"] = TALB(encoding=3, text=[meta_data["album"]]) # 插入专辑名 + audio["TPE1"] = TPE1(encoding=3, text=artists) # 插入歌手 + audio["APIC"] = APIC(encoding=3, mime='image/jpg', type=3, desc='', data=image_data) # 插入封面 + audio["COMM::XXX"] = COMM(encoding=3, lang='XXX', desc='', text=[comment.decode("utf-8")]) # 插入 163 key 注释 + audio["TIT2"] = TIT2(encoding=3, text=[meta_data["musicName"]]) # 插入歌曲名 + audio["TALB"] = TALB(encoding=3, text=[meta_data["album"]]) # 插入专辑名 audio.save() elif meta_data["format"] == "flac": # 针对 flac 使用 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 # 最大进程数 current_process = 0 # 当前正在活动的进程数 passed = 0 # 总共结束的进程数 - with Bar(f"正在破解 %(index){len(str(len(ncm_files)))}d/%(max)d", - suffix="", max=len(ncm_files), color="blue") as bar: + with CompactBar(f"正在破解 %(index){len(str(len(ncm_files)))}d/%(max)d", + suffix="", max=len(ncm_files), color="blue") as bar: total = len(ncm_files) allocated = 0 # 已经分配的任务数量 while True: # 进入循环,执行 新建进程->检测队列->检测任务完成 的循环 @@ -251,8 +251,6 @@ def get_lyric_from_folder(self): q_info)).start() allocated += 1 current_process += 1 - bar.suffix = f"已分配: {ncm_files[allocated-1]}" - print(len(bar.fill)) bar.update() while True: # 错误队列检测 try: @@ -269,8 +267,8 @@ def get_lyric_from_folder(self): musics.append({"id": r['musicId'], "name": r["musicName"], "artists": r["artist"]}) passed += 1 current_process -= 1 - bar.suffix = f"已完成: {r['musicName']} - "\ - f"{''.join([x + ', ' for x in [x[0] for x in r['artist']]])[:-2]}" + bar.print_onto_bar(f"已完成: {r['musicName']} - " + f"{''.join([x + ', ' for x in [x[0] for x in r['artist']]])[:-2]}") bar.next() except Empty: break diff --git a/modules/utils/bar.py b/modules/utils/bar.py index ca3f5bb..9debb33 100644 --- a/modules/utils/bar.py +++ b/modules/utils/bar.py @@ -1,33 +1,18 @@ -"""一些有关调整进度条的设置""" +"""修改了进度条""" from os import get_terminal_size from progress.bar import Bar +from modules.utils.length import get_more_length -def suit_length(bar: Bar, original: Bar): - """尽量调整进度条大小去匹配终端宽度""" - length = get_terminal_size().columns - original_length = original.width + len((original.message + - original.bar_suffix + - original.bar_prefix + - original.suffix) % bar) - if length >= original_length: - return - if length < len(str(bar.max) + "/" + str(bar.index)): - bar.bar_suffix = bar.suffix = bar.bar_prefix = "" - 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 + +class CompactBar(Bar): + def print_onto_bar(self, message: str): + print() + self.update() + print(f"\x1b[1A\x1b[{get_terminal_size().columns}D{(get_terminal_size().columns-1) * ' '}" + f"\x1b[{get_terminal_size().columns}D"+message[:(get_terminal_size().columns - get_more_length(message))]) + self.update() + + def next_without_newline(self): + self.next() diff --git a/modules/utils/length.py b/modules/utils/length.py index 5297e89..97c9310 100644 --- a/modules/utils/length.py +++ b/modules/utils/length.py @@ -5,3 +5,8 @@ def len_abs(content): """针对中文:将一个汉字识别为2个长度,而不是1个""" 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