Rockhopper-Technologies / enlighten

Enlighten Progress Bar for Python Console Apps
https://python-enlighten.readthedocs.io
Mozilla Public License 2.0
412 stars 25 forks source link

Statusbar elapsed time gets erroneously updated even after statusbar is closed #66

Open guano opened 3 months ago

guano commented 3 months ago

Describe the bug A statusbar's elapsed field gets updated, even if it is closed and not refreshed.

Looks like this bug was fixed for counters but not statusbars see issue #62

To Reproduce

import time                                                                                                   
import enlighten                                                                                              

enlightenmanager=enlighten.get_manager()                                                                      

sb = enlightenmanager.status_bar(                                               
            leave=True, autorefresh=False, color="black_on_green",                                            
            status_format=u"statusbar elapsed:{elapsed}")                                                     
time.sleep(1)                                                                                                 
sb.update()                                                                                                   
time.sleep(1)                                                                                                 
sb.update()                                                                                                   
time.sleep(1)                                                                                                 
sb.update()                                                                                                   
time.sleep(1)                                                                                                 

sb.color = "black_on_red"                                                                                     
sb.update()                                                                                                   
sb.close()                                                                                                    
time.sleep(5)                                                                                                 

sb2 = enlightenmanager.status_bar(                                                                            
            leave=True, autorefresh=False, color="black_on_green",                                            
            status_format=u"statusbar2 {elapsed}")                                                            
time.sleep(1)                                                                                                 
sb2.close()                                                                                                   

enlightenmanager.stop()     

sb's elapsed time gets updated even though it's not being updated and is in fact closed.

Environment (please complete the following infkkkormation):

avylove commented 3 months ago

Thanks for reporting! I left some comments on the PR, but I think the main thing to sort out is when to determine the clock should be stopped for status bars. You went with the closed time which does make sense in some cases, but there could be an argument for the last update in other cases. I'm not sure what the right answer is. I'm leaning towards agreeing with you, but my hesitation is this is different than the way it's handled in progress bars. That may be ok, but I think the "why" needs to be understood and documented.

guano commented 3 months ago

Yeah, it definitely is a decision that can be disputed either way.

In the meantime I have figured out a workaround for my use case at least. The elapsed variable can just be read at close time and inserted as a static string rather than being dynamically updated whenever the manager adds/removes another bar.

from enlighten._util import format_time

# start, sleep, update, etc as in original code sample

sb.update(f"statusbar finished. time elapsed when closed: {format_time(sb.elapsed)}")
sb.close()