-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogressbar.hpp
54 lines (47 loc) · 1.53 KB
/
progressbar.hpp
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
// Homepage: https://github.com/ananthvk/cpp-raytracer
#pragma once
#include <iomanip>
#include <iostream>
#include <stdio.h>
// A class which represents an ASCII progressbar
class ProgressBar
{
private:
int value;
int max_value;
int width;
bool display_percent;
public:
/// @brief Constructor for progressbar
/// @param max_value
/// @param width
/// @param display_percent
ProgressBar(int max_value, int width, bool display_percent = true)
: value(0), max_value(max_value), width(width), display_percent(display_percent)
{
}
/// @brief Updates the value of progressbar by given amount
/// @param dt the amount by which the progressbar value has to be increased
void tick(int dt = 1) { value += dt; }
/// @brief Display the progressbar on the passed stream
void display(std::ostream &os)
{
char fill = '=';
char head = '>';
char left_end = '[';
char right_end = ']';
os << "\r" << left_end;
double percent = (double)value / max_value;
os << std::string(percent * width, fill) << head << std::string((1 - percent) * width, ' ')
<< right_end;
if (display_percent)
{
printf("( %0.03f%% )", percent * 100);
}
os << std::flush;
}
/// @brief Hide the cursor to prevent flickering when updating progressbar
void hide_cursor(std::ostream &os) { os << "\033[?25l"; }
/// @brief Show the cursor
void show_cursor(std::ostream &os) { os << "\033[?25h"; }
};