Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Counter placeholder not being removed upon clearing counter. #58

Closed
SKnight79 opened this issue Jul 30, 2023 · 3 comments
Closed

Counter placeholder not being removed upon clearing counter. #58

SKnight79 opened this issue Jul 30, 2023 · 3 comments
Labels

Comments

@SKnight79
Copy link

SKnight79 commented Jul 30, 2023

I created a test python program using the enlighten progress bar module that will generate 10 progress bars in threads, with random counters. They will load and run independently but attached to a manager. Once the counters complete, they are removed. The issue is that it leaves a blank line. Is there any way to remove that behavior?

import time, enlighten, random
from threading import Thread, Event

manager = enlighten.get_manager()
status_bar = manager.status_bar('Test', color=random.randrange(255), justify=enlighten.Justify.CENTER)
prefix = '[test]: '
counters = list()

global_threads = list()


def create(*args, **kwargs):
    counter = kwargs['counter']
    rand = kwargs['rand']
    counter.refresh()

    for num in range(rand+1):
        time.sleep(1)  # Simulate work
        counter.update()

    counter.close()

for id in range(10):
    rand = random.randrange(20)
    print('Creating counter {}'.format(id))
    counter = manager.counter(
        total=rand,
        desc='Counter ' + str(id),
        unit='seconds',
        color=random.randrange(255),
        leave=False,
        bar_format='{desc}{desc_pad}{percentage:3.0f}%|{bar}| {count:{len_total}d}/{total:d} [{elapsed} < {eta}]'
    )

    thread_kwargs = dict()
    thread_kwargs['counter'] = counter
    thread_kwargs['rand'] = rand
    counters.append(counter)

    thread = Thread(target=create, kwargs=thread_kwargs)
    global_threads.append(thread)
    thread.start()
    time.sleep(1)

for num in range(20):
    time.sleep(1)  # Simulate work
    print('Counters are running. Interval is {}'.format(num))

manager.stop()
print('Counter manager stop.')
@SKnight79 SKnight79 added the bug label Jul 30, 2023
@avylove
Copy link
Contributor

avylove commented Jul 30, 2023

I made a few changes while testing.

for num in range(rand+1):

Should be

for num in range(rand):

If rand is 3, then range(rand) would yield 0, 1, and 2. With range(rand + 1), you would also get 3 and that would exceed the total for the counter.

for num in range(20):
    time.sleep(1)  # Simulate work
    print('Counters are running. Interval is {}'.format(num))

Can probably be better expressed as

start = time.time()
while any(thread.is_alive() for thread in global_threads):
    time.sleep(1)  # Simulate work
    print(f'Counters are running: {time.time() - start:.2f} seconds.')

Not required, but more flexible

counter.close()

If you want to blank the line, change this to

counter.close(clear=True)

As far as removing the blank or completed lines, Enlighten currently only does that when adding new counters, since that's when the positions get calculated, but I can definitely see wanting to do it as the threads finish. I can add that when I get a chance.

@SKnight79
Copy link
Author

I like the code improvements. Thanks! Look forward to the additions you mentioned.

@avylove
Copy link
Contributor

avylove commented Sep 24, 2023

As of 1.12.0, counter positions are now recalculated when a counter is closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants