Optimize Progress Bar
This commit is contained in:
@@ -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()
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user