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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/* Copyright 2021 Aristocratos (jakob@qvantnet.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
indent = tab
tab-size = 4
*/
#include <array>
#include <map>
#include <ranges>
#include <algorithm>
#include <cmath>
#include <btop_draw.h>
#include <btop_config.h>
#include <btop_theme.h>
#include <btop_tools.h>
using robin_hood::unordered_flat_map, std::round, std::views::iota,
std::string_literals::operator""s, std::clamp, std::array, std::floor;
namespace rng = std::ranges;
namespace Symbols {
const string h_line = "─";
const string v_line = "│";
const string left_up = "┌";
const string right_up = "┐";
const string left_down = "└";
const string right_down = "┘";
const string title_left = "┤";
const string title_right = "├";
const string div_up = "┬";
const string div_down = "┴";
const string meter = "■";
const array<string, 10> superscript = { "⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹" };
const unordered_flat_map<string, vector<string>> graph_symbols = {
{ "braille_up", {
" ", "⢀", "⢠", "⢰", "⢸",
"⡀", "⣀", "⣠", "⣰", "⣸",
"⡄", "⣄", "⣤", "⣴", "⣼",
"⡆", "⣆", "⣦", "⣶", "⣾",
"⡇", "⣇", "⣧", "⣷", "⣿"
}},
{"braille_down", {
" ", "⠈", "⠘", "⠸", "⢸",
"⠁", "⠉", "⠙", "⠹", "⢹",
"⠃", "⠋", "⠛", "⠻", "⢻",
"⠇", "⠏", "⠟", "⠿", "⢿",
"⡇", "⡏", "⡟", "⡿", "⣿"
}},
{"block_up", {
" ", "▗", "▗", "▐", "▐",
"▖", "▄", "▄", "▟", "▟",
"▖", "▄", "▄", "▟", "▟",
"▌", "▙", "▙", "█", "█",
"▌", "▙", "▙", "█", "█"
}},
{"block_down", {
" ", "▝", "▝", "▐", "▐",
"▘", "▀", "▀", "▜", "▜",
"▘", "▀", "▀", "▜", "▜",
"▌", "▛", "▛", "█", "█",
"▌", "▛", "▛", "█", "█"
}},
{"tty_up", {
" ", "░", "░", "▒", "▒",
"░", "░", "▒", "▒", "█",
"░", "▒", "▒", "▒", "█",
"▒", "▒", "▒", "█", "█",
"▒", "█", "█", "█", "█"
}},
{"tty_down", {
" ", "░", "░", "▒", "▒",
"░", "░", "▒", "▒", "█",
"░", "▒", "▒", "▒", "█",
"▒", "▒", "▒", "█", "█",
"▒", "█", "█", "█", "█"
}}
};
}
namespace Draw {
using namespace Tools;
//* Create a box using values from a BoxConf struct and return as a string
string createBox(BoxConf c){
string out;
string lcolor = (c.line_color.empty()) ? Theme::c("div_line") : c.line_color;
string numbering = (c.num == 0) ? "" : Theme::c("hi_fg") + Symbols::superscript[c.num];
out = Fx::reset + lcolor;
//* Draw horizontal lines
for (uint hpos : {c.y, c.y + c.height - 1}){
out += Mv::to(hpos, c.x) + Symbols::h_line * (c.width - 1);
}
//* Draw vertical lines and fill if enabled
for (uint hpos : iota(c.y + 1, c.y + c.height - 1)){
out += Mv::to(hpos, c.x) + Symbols::v_line +
((c.fill) ? string(c.width - 2, ' ') : Mv::r(c.width - 2)) +
Symbols::v_line;
}
//* Draw corners
out += Mv::to(c.y, c.x) + Symbols::left_up +
Mv::to(c.y, c.x + c.width - 1) + Symbols::right_up +
Mv::to(c.y + c.height - 1, c.x) + Symbols::left_down +
Mv::to(c.y + c.height - 1, c.x + c.width - 1) + Symbols::right_down;
//* Draw titles if defined
if (!c.title.empty()){
out += Mv::to(c.y, c.x + 2) + Symbols::title_left + Fx::b + numbering + Theme::c("title") + c.title +
Fx::ub + lcolor + Symbols::title_right;
}
if (!c.title2.empty()){
out += Mv::to(c.y + c.height - 1, c.x + 2) + Symbols::title_left + Theme::c("title") + c.title2 +
Fx::ub + lcolor + Symbols::title_right;
}
return out + Fx::reset + Mv::to(c.y + 1, c.x + 1);
}
void Meter::operator()(int width, string color_gradient, bool invert) {
this->width = width;
this->color_gradient = color_gradient;
this->invert = invert;
cache.clear();
cache.insert(cache.begin(), 101, "");
}
string Meter::operator()(int value) {
if (width < 1) return "";
value = clamp(value, 0, 100);
if (!cache.at(value).empty()) return cache.at(value);
string& out = cache.at(value);
for (int i : iota(1, width + 1)) {
int y = round((double)i * 100.0 / width);
if (value >= y)
out += Theme::g(color_gradient)[invert ? 100 - y : y] + Symbols::meter;
else {
out += Theme::c("meter_bg") + Symbols::meter * (width + 1 - i);
break;
}
}
out += Fx::reset;
return out;
}
void Graph::_create(const vector<long long>& data, int data_offset) {
const bool mult = (data.size() - data_offset > 1);
if (mult && (data.size()
|