From 41f2eb087900d4f2ea95c3e11087cf2c71395bae Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 27 Mar 2018 17:09:10 -0400 Subject: Added console progress meter This utility originated with my bot rawr-ebooks. It was packaged into a class during the development of verbly, and appears here in its most recent form with adjustments made in a currently-unreleased bot called crystalline. --- hkutil/progress.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 hkutil/progress.h diff --git a/hkutil/progress.h b/hkutil/progress.h new file mode 100644 index 0000000..1c6e96f --- /dev/null +++ b/hkutil/progress.h @@ -0,0 +1,61 @@ +#ifndef PROGRESS_H_A34EF856 +#define PROGRESS_H_A34EF856 + +#include +#include + +namespace hatkirby { + + class progress { + public: + + progress( + std::string message, + unsigned long total) : + message(message), + total(total) + { + std::cout << message << " 0%" << std::flush; + } + + void update(unsigned long val) + { + if (val <= total) + { + cur = val; + } else { + cur = total; + } + + unsigned long pp = cur * 100 / total; + if (pp != lprint) + { + lprint = pp; + + std::cout << "\b\b\b\b" << std::right; + std::cout.width(3); + std::cout << pp << "%" << std::flush; + } + } + + void update() + { + update(cur+1); + } + + ~progress() + { + std::cout << "\b\b\b\b100%" << std::endl; + } + + private: + + std::string message; + unsigned long total; + unsigned long cur = 0; + unsigned long lprint = 0; + }; + +} + +#endif /* end of include guard: PROGRESS_H_A34EF856 */ -- cgit 1.4.1