Skip to content
Joshua Z. Zhang edited this page Nov 13, 2015 · 1 revision

What is this

Progress bar is used to show working status in console so that you have a better idea when the job would done.

Animation

It is definitely more intuitive than plain text.

How to use it

The API is quite simple.

#include "zupply.hpp"
using namespace zz;

void test_progbar()
{
    unsigned task = 1000; // this is the number of tasks you have to finish
    log::ProgBar pb(task, "TestProgbar"); // create a progress bar with # tasks and name
    // now let's do something
    for (unsigned i = 0; i < task; ++i)
    {
        // run some functions here as on task, I use sleep here for example
        time::sleep(1);
        // increment task status
        pb.step(1);
    }
    // the progress bar will automatically destroy when 100% reached, or you can stop it manually
    // pb.stop();
}

Some notes on it

  • ProgBar will only work in console, if you have redirect stdout to file or something, it won't show up by design.
  • ProgBar will detect the width of console, and adjust its length accordingly.
  • ProgBar will use background thread to handle refresh, thus main thread will not be blocked.
  • ProgBar use redirect scheme to handle message outputs during working period, otherwise the screen will be messy.

This design, however, introduce a caveat that your output message is not guaranteed to be properly shown on screen. The iostream is not thread-safe or at least not guaranteed to be thread-safe, which caused the problem. I currently have no plan to rewrite it, nor designing wrapper for it because the ProgBar is so simple and light-weight, I don't want to mess it up.

Workaround? Do not print too many messages during ProgBar working period, that's it. As is shown in the demo, small amount of messages is okay.