Skip to content

bmc-labs/progress_bar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

progress_bar - a CLI progress bar

pipeline status

I needed a command line progress bar and none of the options I found on GitHub or via Google were much to my liking, what with their camel cased names and lack of operator<< overload and such. Now this is certainly not the best ever implementation of anything, I just hacked this together real quick. It supposedly works on Windows, but I haven't tested that.

Using it

It's header only. It has no dependencies other than the STL and it's Apache v2 licensed.

So basically you just go

#include <chrono>
#include <iostream>
#include <numeric>
#include <thread>
#include <vector>

#include "progress_bar.h"


using namespace std::chrono_literals;


int main() {
  bmc::progress_bar pb{
    13,    // min, also initial value
    156,   // max
    80,    // width in percent of screen (including percentage and time)
    true,  // auto increment when outputting
    true   // show time at the end
  };

  // generate some data
  std::vector<int> vec(pb.size());
  std::iota(std::begin(vec), std::end(vec), 42);

  for (auto & e : vec) {
    e *= 2;
    std::this_thread::sleep_for(50ms);
    std::cout << pb;
  }

  std::cout << pb;  // if progress bar is already full, no increment happens

  return 0;
}

and you get

[----->                             ]  10% -  0:42.312 elapsed

Of course there are a bunch more things you can do. If you don't like it to auto increment when using operator<<, just give it a false there (4th ctor argument). You might then want to use ++ or -- in exactly the way you'd expect, or step(int steps), or set(int pos). Essentially, the following works:

bmc::progress_bar pb{-13, 156, 80, false, true};

++pb;
pb++;
--pb;
pb--;

pb.step(5);   // move 5 steps forward
pb.set(137);  // go to progress 137

If you try to move outside min and max, it simply doesn't do that. If you try to give it a min that is bigger than max, it throws. If you try to push width (percentage of terminal width) to 0, it throws.

More

That's it. The example above sits in src/main.cc and actually does run. Other than that - just clone this repo and/or throw the header file in your project.

You're good to go.

Releases

No releases published

Packages

No packages published