Class progress_bar

Class Documentation

class progress_bar

A simple progress bar class for terminal based output.

Using a progress bar is very convenient for the end user when you have long running code. It gives feed back on how fast an operation is performed and may give an indication how long it will take before it is finished.

Using this cif::progress_bar implementation is straightforward:

using namespace std::chrono_literals;

cif::progress_bar pb(10, "counting to ten");

for (int i = 1; i <= 10; ++i)
{
  pb.consumed(1);
  std::this_thread::sleep_for(1s);
}

When the progress_bar is created, it first checks to see if stdout is to a real TTY and if the VERBOSE flag is not less than zero (quiet mode). If this passes a thread is started that waits for updates.

The first two seconds, nothing is written to the screen so if the work is finished within those two seconds the screen stays clean.

After this time, a progress bar is printed that may look like this:

step 3           ========================--------------------------------  40% ⢁

The first characters contain the initial action name or the message text if it was used afterwards.

The thermometer is made up with ‘=’ and ‘-’ characters.

A percentage is also shown and at the end there is a spinner that gives feedback that the program is really still working.

The progress bar is removed if the max has been reached or if the progress bar is destructed. If any output has been generated, the initial action is printed out along with the total time spent.

Public Functions

progress_bar(int64_t inMax, const std::string &inAction)

Construct a new progress bar object.

Progress ranges from 0 (zero) to inMax

The action in inAction is used for display

Parameters
  • inMax – The maximum value

  • inAction – The description of what is going on

~progress_bar()

Destroy the progress bar object.

void consumed(int64_t inConsumed)

Notify the progress bar that inConsumed should be added to the internal progress counter.

void progress(int64_t inProgress)

Notify the progress bar that the internal progress counter should be updated to inProgress.

void message(const std::string &inMessage)

Replace the action string in the progress bar with inMessage.