-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
halo.cpp
113 lines (102 loc) · 2.96 KB
/
halo.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "halo.hpp"
#include "colorama.hpp"
#include <iostream>
#include <chrono>
#include <thread>
#ifdef _WIN32
#include <windows.h>
#endif
void Halo::set_cursor_hidden(bool hidden) {
#ifdef _WIN32
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(out, &cursorInfo);
cursorInfo.bVisible = !hidden;
SetConsoleCursorInfo(out, &cursorInfo);
#else
std::cout << "\033[?25" << (hidden?'l':'h') << std::flush;
#endif
}
void Halo::show_spinner_frame(std::string_view frame) {
// Set color
*settings.stream << settings.color;
// Show frame
*settings.stream << frame;
}
void Halo::show_spinner() {
// Increment frame index
assert(!settings.spinner.frames[0].empty());
if (++frame_idx > settings.spinner.get_frame_count()) {
frame_idx = 0;
}
// Show frame
show_spinner_frame(settings.spinner.frames[frame_idx]);
}
void Halo::show_custom_text(std::string_view text) {
// Set color
*settings.stream << settings.text_color;
// Calculate text length difference
ssize_t diff = last_text_len - text.size();
// Print text (TODO: Add animations)
*settings.stream << text;
last_text_len = text.size();
// Make sure to overwrite old text
if (diff > 0) {
std::string placeholder;
placeholder.resize(diff, ' ');
*settings.stream << placeholder << colorama::Cursor::BACK(diff);
}
}
void Halo::show_text() {
show_custom_text(settings.text);
}
void Halo::show() {
// Don't run if disabled
if (!enabled) return;
// Show spinner and text, in the correct order
*settings.stream << std::flush;
if (settings.placement == left) {
show_spinner();
*settings.stream << ' ';
}
show_text();
if (settings.placement == right) {
*settings.stream << ' ';
show_spinner();
}
*settings.stream << '\r' << std::flush;
// Reset color
*settings.stream << colorama::Fore::RESET;
}
void Halo::status(std::string_view frame, std::string_view message, colorama::Color text_color) {
// Set custom color
auto color_bak = settings.text_color;
if (text_color != neutral_color) {
settings.text_color = text_color;
}
// Show given spinner frame and text, in the correct order
*settings.stream << std::flush;
if (settings.placement == left) {
show_spinner_frame(frame);
*settings.stream << ' ';
}
show_custom_text(message);
if (settings.placement == right) {
*settings.stream << ' ';
show_spinner_frame(frame);
}
*settings.stream << std::endl;
// Reset color
*settings.stream << colorama::Fore::RESET;
// Restore color
settings.text_color = color_bak;
}
void Halo::run_thread() {
auto stop_req = wants_stop;
set_cursor_hidden(true);
while (!*stop_req) {
show();
std::this_thread::sleep_for(std::chrono::milliseconds(settings.interval));
}
set_cursor_hidden(false);
}