truncate and rotate long filenames in progress display
This commit is contained in:
parent
37ee1a70e9
commit
86db090e23
|
@ -7,6 +7,8 @@ import json
|
|||
import math
|
||||
import pathlib
|
||||
import sys
|
||||
import time
|
||||
from unicodedata import east_asian_width
|
||||
|
||||
import aiofiles
|
||||
from tqdm import tqdm
|
||||
|
@ -14,10 +16,35 @@ from websockets import connect
|
|||
|
||||
FILE_CHUNK_SIZE = 16384
|
||||
|
||||
FILE_NAME_DISPLAY_WIDTH = 24
|
||||
FILE_NAME_DISPLAY_PADDING = 3
|
||||
FILE_NAME_DISPLAY_UPDATE_PERIOD = 0.2
|
||||
|
||||
def rotating_segment(name, pos):
|
||||
output = ''
|
||||
total_width = 0
|
||||
rotating_name = name + (' ' * FILE_NAME_DISPLAY_PADDING) + name
|
||||
for char in rotating_name[pos:]:
|
||||
char_width = 2 if east_asian_width(char) in ['F', 'W'] else 1
|
||||
if char_width == 2 and total_width == FILE_NAME_DISPLAY_WIDTH - 1:
|
||||
output += ' '
|
||||
total_width += 1
|
||||
else:
|
||||
output += char
|
||||
total_width += char_width
|
||||
if total_width >= FILE_NAME_DISPLAY_WIDTH:
|
||||
return output
|
||||
|
||||
async def file_loader(files):
|
||||
with tqdm(desc="Total", total=sum(size for (path, size) in files), unit='B', unit_scale=True, leave=True, position=1) as total_progress:
|
||||
for (path, size) in files:
|
||||
with tqdm(desc=path.name, total=size, unit='B', unit_scale=True, leave=True, position=0) as file_progress:
|
||||
if len(path.name) > FILE_NAME_DISPLAY_WIDTH:
|
||||
file_name_display_pos = 0
|
||||
last_desc_update_time = time.time()
|
||||
desc = rotating_segment(path.name, 0)
|
||||
else:
|
||||
desc = path.name
|
||||
with tqdm(desc=desc, total=size, unit='B', unit_scale=True, leave=True, position=0) as file_progress:
|
||||
async with aiofiles.open(path, mode='rb') as f:
|
||||
while True:
|
||||
pos = await f.tell()
|
||||
|
@ -27,6 +54,13 @@ async def file_loader(files):
|
|||
if data == b'':
|
||||
tqdm.write("file ended early!")
|
||||
exit(1)
|
||||
if len(path.name) > FILE_NAME_DISPLAY_WIDTH and (now := time.time()) - last_desc_update_time > FILE_NAME_DISPLAY_UPDATE_PERIOD:
|
||||
file_name_display_pos = (file_name_display_pos + 1) % (len(path.name) + FILE_NAME_DISPLAY_PADDING)
|
||||
last_desc_update_time = now
|
||||
file_progress.set_description(
|
||||
desc=rotating_segment(path.name, file_name_display_pos),
|
||||
refresh=False,
|
||||
)
|
||||
total_progress.update(len(data))
|
||||
file_progress.update(len(data))
|
||||
yield data
|
||||
|
|
Loading…
Reference in a new issue