-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogressBar.h
100 lines (87 loc) · 2.48 KB
/
progressBar.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef PROGRESSBAR_H
#define PROGRESSBAR_H
//
#include <string>
#include <sstream>
#include <cassert>
#include <iostream>
class ProgressBar
{
public:
ProgressBar( const unsigned int& times = 0, const std::string& message =""):m_curIteration(0),m_numIterations(times),m_message(message),m_curPercentage(0.01)
{
}
void operator++()
{
if( !m_curIteration) std::cout << "\r" << m_message << "..." << "0%" << std::flush;
++m_curIteration;
if ( double(m_curIteration)/m_numIterations > m_curPercentage)
{
std::cout << "\r" << m_message << "..." << (unsigned int)(m_curPercentage*100) << "%" << std::flush;
m_curPercentage += 0.01;
}
if ( m_curIteration == m_numIterations)
{
std::cout << "\r" << m_message << "...done!\n" << std::flush;
}
}
void reset(const unsigned int& times, const std::string& message ="")
{
m_curIteration = 0;
m_numIterations = times;
m_message = message;
m_curPercentage = 0.01;
}
private:
unsigned int m_curIteration;
unsigned int m_numIterations;
std::string m_message;
double m_curPercentage;
};
class ProgressStream
{
public:
ProgressStream( const unsigned int& times):m_curIteration(0),m_numIterations(times),m_curPercentage(0.01)
{
++m_curIteration;
++m_numIterations;
}
void operator++()
{
if( m_curIteration)
{
++m_curIteration;
if ( double(m_curIteration)/m_numIterations > m_curPercentage)
{
std::cout << "\r" << m_label.str() << "..." << (unsigned int)(m_curPercentage*100) << "%" << std::flush;
m_curPercentage += 0.01;
}
if ( m_curIteration == m_numIterations)
{
std::cout << "\r" << m_label.str() << "...done!\n" << std::flush;
}
}
else
{
std::cout << "\r" << m_label.str() << "..." << "0%" << std::flush;
++m_curIteration;
assert(false);
}
}
std::stringstream& label()
{
return m_label;
}
void reset(const unsigned int& times)
{
m_curIteration = 0;
m_numIterations = times;
m_curPercentage = 0.01;
}
private:
unsigned int m_curIteration;
unsigned int m_numIterations;
double m_curPercentage;
std::stringstream m_label;
};
#endif //PROGRESSBAR_H