-
Notifications
You must be signed in to change notification settings - Fork 8
/
spinner.hpp
77 lines (58 loc) · 1.45 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
75
76
77
#pragma once
#include "loggr.hpp"
#include <chrono>
class spinner
{
public:
spinner(loggr* new_logger) : m_logger(new_logger) { }
static constexpr std::uint8_t CHARACTER_SEQUENCE[] =
{
'|', '/', '-', '\\'
};
inline void start()
{
if (this->m_active)
{
this->m_logger->log_error("Cannot initialise spinner object more than once!");
return;
}
this->m_active = true;
}
inline void update()
{
if (!this->m_active)
{
this->m_logger->log_error("Cannot initialise spinner object without initialization!");
return;
}
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);
constexpr auto update_time = 150;
if (duration.count() <= update_time)
{
return;
}
this->m_logger->clear_line();
this->m_logger->log_raw("[{}]", static_cast<char>(spinner::CHARACTER_SEQUENCE[this->m_seq_index]));
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)
{
this->m_logger->log_error("Spinner is not active!");
return;
}
this->m_logger->clear_line();
this->m_active = false;
}
private:
const loggr* m_logger;
bool m_active = false;
std::size_t m_seq_index = 0;
std::chrono::steady_clock::time_point m_last_update_time;
};