diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-03-27 17:09:10 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-03-27 17:09:10 -0400 |
commit | 41f2eb087900d4f2ea95c3e11087cf2c71395bae (patch) | |
tree | 63e4a734761af215275877baaee5d3cf9fc57112 | |
parent | 9b6a1a42d69ee8d1fe5a98a81cc798f9023f5447 (diff) | |
download | hkutil-41f2eb087900d4f2ea95c3e11087cf2c71395bae.tar.gz hkutil-41f2eb087900d4f2ea95c3e11087cf2c71395bae.tar.bz2 hkutil-41f2eb087900d4f2ea95c3e11087cf2c71395bae.zip |
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.
-rw-r--r-- | hkutil/progress.h | 61 |
1 files changed, 61 insertions, 0 deletions
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 @@ | |||
1 | #ifndef PROGRESS_H_A34EF856 | ||
2 | #define PROGRESS_H_A34EF856 | ||
3 | |||
4 | #include <string> | ||
5 | #include <iostream> | ||
6 | |||
7 | namespace hatkirby { | ||
8 | |||
9 | class progress { | ||
10 | public: | ||
11 | |||
12 | progress( | ||
13 | std::string message, | ||
14 | unsigned long total) : | ||
15 | message(message), | ||
16 | total(total) | ||
17 | { | ||
18 | std::cout << message << " 0%" << std::flush; | ||
19 | } | ||
20 | |||
21 | void update(unsigned long val) | ||
22 | { | ||
23 | if (val <= total) | ||
24 | { | ||
25 | cur = val; | ||
26 | } else { | ||
27 | cur = total; | ||
28 | } | ||
29 | |||
30 | unsigned long pp = cur * 100 / total; | ||
31 | if (pp != lprint) | ||
32 | { | ||
33 | lprint = pp; | ||
34 | |||
35 | std::cout << "\b\b\b\b" << std::right; | ||
36 | std::cout.width(3); | ||
37 | std::cout << pp << "%" << std::flush; | ||
38 | } | ||
39 | } | ||
40 | |||
41 | void update() | ||
42 | { | ||
43 | update(cur+1); | ||
44 | } | ||
45 | |||
46 | ~progress() | ||
47 | { | ||
48 | std::cout << "\b\b\b\b100%" << std::endl; | ||
49 | } | ||
50 | |||
51 | private: | ||
52 | |||
53 | std::string message; | ||
54 | unsigned long total; | ||
55 | unsigned long cur = 0; | ||
56 | unsigned long lprint = 0; | ||
57 | }; | ||
58 | |||
59 | } | ||
60 | |||
61 | #endif /* end of include guard: PROGRESS_H_A34EF856 */ | ||