fastai / fastprogress

Simple and flexible progress bar for Jupyter Notebook and console
Apache License 2.0
1.09k stars 106 forks source link

Progress bar not clearing line in Console mode #106

Open ucohen opened 8 months ago

ucohen commented 8 months ago

In console mode, when adding a progress comment, the previous line is not completely cleared thus causing the leftover ending to remain. for example:

from fastprogress import progress_bar
from pathlib import Path
import time

files = list(Path('/etc').glob('*'))
pb = progress_bar(files)

for fn in pb:
    pb.comment = f'{fn.stem}'
    time.sleep(0.1)

console output:

|█████████████----| 77.69% [188/242 00:18<00:05 login]es]]e]te].conf]

I managed to root-cause the issue and solved it by updating on_update and padding with empty spaces to max_len

modify:

if len(to_write) > self.max_len: self.max_len=len(to_write)

to

if len(to_write) > self.max_len: self.max_len=len(to_write)
else: to_write += ' ' * (self.max_len - len(to_write))

is this the proper way to fix it?