-
Notifications
You must be signed in to change notification settings - Fork 7
/
spinner.hpp
74 lines (60 loc) · 1.43 KB
/
spinner.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
#include "loggr.hpp"
#include <chrono>
class spinner
{
public:
static constexpr std::uint8_t CHARACTER_SEQUENCE[] =
{
'|', '/', '-', '\\'
};
inline void start()
{
if (this->m_active)
{
loggr::log_error("Cannot initialise spinner object more than once!");
return;
}
this->m_active = true;
}
inline void update()
{
if (!this->m_active)
{
loggr::log_error("Cannot initialise spinner object without initialization!");
return;
}
// GET TIME SINCE LAST UPDATE
using clock_t = std::chrono::high_resolution_clock;
const auto time = clock_t::now();
const auto time_delta = time - m_last_update_time;
const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(time_delta);
// ONLY UPDATE SPINNER EVERY x MS
constexpr auto update_time = 150;
if (duration.count() <= update_time)
{
return;
}
// UPDATE THE SPINNER CHARACTER
loggr::clear_line();
loggr::log_raw("[{}]", static_cast<char>(spinner::CHARACTER_SEQUENCE[this->m_seq_index]));
// INCREMENT AND WRAP
this->m_seq_index++;
this->m_seq_index %= sizeof(spinner::CHARACTER_SEQUENCE);
this->m_last_update_time = time;
}
inline void stop()
{
if (!this->m_active)
{
loggr::log_error("Spinner is not active!");
return;
}
loggr::clear_line();
this->m_active = false;
}
private:
bool m_active = false;
std::size_t m_seq_index = 0;
std::chrono::steady_clock::time_point m_last_update_time;
};