改进日志表述

This commit is contained in:
2026-04-21 19:59:13 +08:00
parent b05289d71d
commit fe236a2cc0
3 changed files with 162 additions and 143 deletions
+27
View File
@@ -0,0 +1,27 @@
# @File : utils.py
# @Time : 2026/04/21 17:24:21
# @Author : SilverAg.L
def shorten_name(filename: str, flag: int = 1):
stem, ext = filename.rsplit('.', 1) if '.' in filename else (filename, '')
stem = stem.replace(' ', '').upper()
ext = ext[:3].upper()
bstem = stem.encode(errors='ignore')
for i in range(8, 0, -1):
try:
cut = bstem[:i].decode()
if cut != stem:
cut += f'~{flag}'
stem = cut
break
except UnicodeDecodeError:
continue
return f'{stem}.{ext}' if ext else stem
def format_size(bytes: int) -> str:
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if bytes < 1024:
return f"{bytes:.2f} {unit}"
bytes /= 1024
return f"{bytes:.2f} PB"