summary refs log tree commit diff stats
path: root/generator/progress.h
blob: 81f07a39fbe7249622d2958e0d774076732ad3b7 (plain) (blame)
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
#ifndef PROGRESS_H_A34EF856
#define PROGRESS_H_A34EF856

#include <string>

class progress {
  private:
    std::string message;
    int total;
    int cur = 0;
    int lprint = 0;
    
  public:
    progress(std::string message, int total) : message(message), total(total)
    {
      std::cout << message << "   0%" << std::flush;
    }
    
    void update(int val)
    {
      if (val <= total)
      {
        cur = val;
      } else {
        cur = total;
      }
      
      int 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;
    }
};

#endif /* end of include guard: PROGRESS_H_A34EF856 */