-
-
Notifications
You must be signed in to change notification settings - Fork 989
Expand file tree
/
Copy pathbtop_draw.cpp
More file actions
2508 lines (2255 loc) · 107 KB
/
btop_draw.cpp
File metadata and controls
2508 lines (2255 loc) · 107 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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 <algorithm>
#include <array>
#include <cmath>
#include <iterator>
#include <ranges>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <fmt/format.h>
#include "btop_config.hpp"
#include "btop_draw.hpp"
#include "btop_input.hpp"
#include "btop_log.hpp"
#include "btop_menu.hpp"
#include "btop_shared.hpp"
#include "btop_theme.hpp"
#include "btop_tools.hpp"
using std::array;
using std::clamp;
using std::cmp_equal;
using std::cmp_greater;
using std::cmp_less;
using std::cmp_less_equal;
using std::floor;
using std::max;
using std::min;
using std::round;
using std::to_string;
using std::views::iota;
using namespace Tools;
using namespace std::literals; // for operator""s
namespace rng = std::ranges;
namespace Symbols {
const string meter = "■";
const array<string, 10> superscript = { "⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹" };
const std::unordered_map<string, vector<string>> graph_symbols = {
{ "braille_up", {
" ", "⢀", "⢠", "⢰", "⢸",
"⡀", "⣀", "⣠", "⣰", "⣸",
"⡄", "⣄", "⣤", "⣴", "⣼",
"⡆", "⣆", "⣦", "⣶", "⣾",
"⡇", "⣇", "⣧", "⣷", "⣿"
}},
{"braille_down", {
" ", "⠈", "⠘", "⠸", "⢸",
"⠁", "⠉", "⠙", "⠹", "⢹",
"⠃", "⠋", "⠛", "⠻", "⢻",
"⠇", "⠏", "⠟", "⠿", "⢿",
"⡇", "⡏", "⡟", "⡿", "⣿"
}},
{"block_up", {
" ", "▗", "▗", "▐", "▐",
"▖", "▄", "▄", "▟", "▟",
"▖", "▄", "▄", "▟", "▟",
"▌", "▙", "▙", "█", "█",
"▌", "▙", "▙", "█", "█"
}},
{"block_down", {
" ", "▝", "▝", "▐", "▐",
"▘", "▀", "▀", "▜", "▜",
"▘", "▀", "▀", "▜", "▜",
"▌", "▛", "▛", "█", "█",
"▌", "▛", "▛", "█", "█"
}},
{"tty_up", {
" ", "░", "░", "▒", "▒",
"░", "░", "▒", "▒", "█",
"░", "▒", "▒", "▒", "█",
"▒", "▒", "▒", "█", "█",
"▒", "█", "█", "█", "█"
}},
{"tty_down", {
" ", "░", "░", "▒", "▒",
"░", "░", "▒", "▒", "█",
"░", "▒", "▒", "▒", "█",
"▒", "▒", "▒", "█", "█",
"▒", "█", "█", "█", "█"
}}
};
}
namespace Draw {
string banner_gen(int y, int x, bool centered, bool redraw) {
static string banner;
static size_t width = 0;
if (redraw) banner.clear();
if (banner.empty()) {
string b_color, bg, fg, oc, letter;
auto lowcolor = Config::getB("lowcolor");
auto tty_mode = Config::getB("tty_mode");
for (size_t z = 0; const auto& line : Global::Banner_src) {
if (const auto w = ulen(line[1]); w > width) width = w;
if (tty_mode) {
fg = (z > 2) ? "\x1b[31m" : "\x1b[91m";
bg = (z > 2) ? "\x1b[90m" : "\x1b[37m";
}
else {
fg = Theme::hex_to_color(line[0], lowcolor);
int bg_i = 120 - z * 12;
bg = Theme::dec_to_color(bg_i, bg_i, bg_i, lowcolor);
}
for (size_t i = 0; i < line[1].size(); i += 3) {
if (line[1][i] == ' ') {
letter = Mv::r(1);
i -= 2;
}
else
letter = line[1].substr(i, 3);
b_color = (letter == "█") ? fg : bg;
if (b_color != oc) banner += b_color;
banner += letter;
oc = b_color;
}
if (++z < Global::Banner_src.size()) banner += Mv::l(ulen(line[1])) + Mv::d(1);
}
banner += Mv::r(18 - Global::Version.size())
+ Theme::c("main_fg") + Fx::b + Fx::i + "v" + Global::Version + Fx::reset;
}
if (redraw) return "";
return (centered ? Mv::to(y, Term::width / 2 - width / 2) : Mv::to(y, x)) + banner;
}
TextEdit::TextEdit() {}
TextEdit::TextEdit(string text, bool numeric) : numeric(numeric), text(std::move(text)) {
pos = this->text.size();
upos = ulen(this->text);
}
bool TextEdit::command(const std::string_view key) {
if (key == "left" and upos > 0) {
upos--;
pos = uresize(text, upos).size();
}
else if (key == "right" and pos < text.size()) {
upos++;
pos = uresize(text, upos).size();
}
else if (key == "home" and not text.empty() and pos > 0) {
pos = upos = 0;
}
else if (key == "end" and not text.empty() and pos < text.size()) {
pos = text.size();
upos = ulen(text);
}
else if (key == "backspace" and pos > 0) {
if (pos == text.size()) {
text = uresize(text, --upos);
pos = text.size();
}
else {
const string first = uresize(text, --upos);
pos = first.size();
text = first + luresize(text.substr(pos), ulen(text) - upos - 1);
}
}
else if (key == "delete" and pos < text.size()) {
const string first = uresize(text, upos + 1);
text = uresize(first, ulen(first) - 1) + text.substr(first.size());
}
else if (key == "space" and not numeric) {
text.insert(pos++, 1, ' ');
upos++;
}
else if (ulen(key) == 1 and text.size() < text.max_size() - 20) {
if (numeric and not isint(key)) return false;
if (key.size() == 1) {
text.insert(pos++, 1, key.at(0));
upos++;
}
else {
const auto first = fmt::format("{}{}", uresize(text, upos), key);
text = first + text.substr(pos);
upos++;
pos = first.size();
}
}
else
return false;
return true;
}
string TextEdit::operator()(const size_t limit) {
string out;
size_t c_upos = upos;
if (text.empty())
return Fx::ul + " " + Fx::uul;
if (limit > 0 and ulen(text) + 1 > limit) {
try {
const size_t half = (size_t)round((double)limit / 2);
string first;
if (upos + half > ulen(text))
first = luresize(text.substr(0, pos), limit - (ulen(text) - upos));
else if (upos - half < 1)
first = text.substr(0, pos);
else
first = luresize(text.substr(0, pos), half);
out = first + uresize(text.substr(pos), limit - ulen(first));
c_upos = ulen(first);
}
catch (const std::exception& e) {
Logger::error("In TextEdit::operator() : {}", e.what());
return "";
}
}
else
out = text;
if (c_upos == 0)
return Fx::ul + uresize(out, 1) + Fx::uul + luresize(out, ulen(out) - 1);
else if (c_upos == ulen(out))
return out + Fx::ul + " " + Fx::uul;
else
return uresize(out, c_upos) + Fx::ul + luresize(uresize(out, c_upos + 1), 1) + Fx::uul + luresize(out, ulen(out) - c_upos - 1);
}
void TextEdit::clear() {
this->text.clear();
}
string createBox(
const int x, const int y, const int width, const int height, string line_color, bool fill, const std::string_view title,
const std::string_view title2, const int num
) {
string out;
if (line_color.empty())
line_color = Theme::c("div_line");
auto tty_mode = Config::getB("tty_mode");
auto rounded = Config::getB("rounded_corners");
const string numbering = (num == 0) ? "" : Theme::c("hi_fg") + (tty_mode ? std::to_string(num) : Symbols::superscript.at(clamp(num, 0, 9)));
const auto& right_up = (tty_mode or not rounded ? Symbols::right_up : Symbols::round_right_up);
const auto& left_up = (tty_mode or not rounded ? Symbols::left_up : Symbols::round_left_up);
const auto& right_down = (tty_mode or not rounded ? Symbols::right_down : Symbols::round_right_down);
const auto& left_down = (tty_mode or not rounded ? Symbols::left_down : Symbols::round_left_down);
out = Fx::reset + line_color;
//? Draw horizontal lines
for (const int& hpos : {y, y + height - 1}) {
out += Mv::to(hpos, x) + Symbols::h_line * (width - 1);
}
//? Draw vertical lines and fill if enabled
for (const int& hpos : iota(y + 1, y + height - 1)) {
out += Mv::to(hpos, x) + Symbols::v_line
+ ((fill) ? string(width - 2, ' ') : Mv::r(width - 2))
+ Symbols::v_line;
}
//? Draw corners
out += Mv::to(y, x) + left_up
+ Mv::to(y, x + width - 1) + right_up
+ Mv::to(y + height - 1, x) +left_down
+ Mv::to(y + height - 1, x + width - 1) + right_down;
//? Draw titles if defined
if (not title.empty()) {
out += fmt::format(
"{}{}{}{}{}{}{}{}{}", Mv::to(y, x + 2), Symbols::title_left, Fx::b, numbering, Theme::c("title"), title, Fx::ub,
line_color, Symbols::title_right
);
}
if (not title2.empty()) {
out += fmt::format(
"{}{}{}{}{}{}{}{}{}", Mv::to(y + height - 1, x + 2), Symbols::title_left_down, Fx::b, numbering, Theme::c("title"), title2, Fx::ub,
line_color, Symbols::title_right_down
);
}
return out + Fx::reset + Mv::to(y + 1, x + 1);
}
bool update_clock(bool force) {
const auto& clock_format = Config::getS("clock_format");
if (not Cpu::shown or clock_format.empty()) {
if (clock_format.empty() and not Global::clock.empty()) Global::clock.clear();
return false;
}
static const std::unordered_map<string, string> clock_custom_format = {
{"/user", Tools::username()},
{"/host", Tools::hostname()},
{"/uptime", ""}
};
static time_t c_time{};
static size_t clock_len{};
static string clock_str;
if (auto n_time = time(nullptr); not force and n_time == c_time)
return false;
else {
c_time = n_time;
const auto new_clock = Tools::strf_time(clock_format);
if (not force and new_clock == clock_str) return false;
clock_str = new_clock;
}
auto& out = Global::clock;
auto cpu_bottom = Config::getB("cpu_bottom");
const auto& x = Cpu::x;
const auto y = (cpu_bottom ? Cpu::y + Cpu::height - 1 : Cpu::y);
const auto& width = Cpu::width;
const auto& title_left = (cpu_bottom ? Symbols::title_left_down : Symbols::title_left);
const auto& title_right = (cpu_bottom ? Symbols::title_right_down : Symbols::title_right);
for (const auto& [c_format, replacement] : clock_custom_format) {
if (clock_str.contains(c_format)) {
if (c_format == "/uptime") {
string upstr = sec_to_dhms(system_uptime());
if (upstr.size() > 8) upstr.resize(upstr.size() - 3);
clock_str = s_replace(clock_str, c_format, upstr);
}
else {
clock_str = s_replace(clock_str, c_format, replacement);
}
}
}
clock_str = uresize(clock_str, std::max(10, width - 66 - (Term::width >= 100 and Config::getB("show_battery") and Cpu::has_battery ? 22 : 0)));
out.clear();
if (clock_str.size() != clock_len) {
if (not Global::resized and clock_len > 0)
out = Mv::to(y, x+(width / 2)-(clock_len / 2)) + Fx::ub + Theme::c("cpu_box") + Symbols::h_line * clock_len;
clock_len = clock_str.size();
}
out += Mv::to(y, x+(width / 2)-(clock_len / 2)) + Fx::ub + Theme::c("cpu_box") + title_left
+ Theme::c("title") + Fx::b + clock_str + Theme::c("cpu_box") + Fx::ub + title_right;
return true;
}
//* Meter class ------------------------------------------------------------------------------------------------------------>
Meter::Meter() {}
Meter::Meter(const int width, string color_gradient, bool invert)
: width(width), color_gradient(std::move(color_gradient)), invert(invert) {}
string Meter::operator()(int value) {
if (width < 1) return "";
value = clamp(value, 0, 100);
if (not cache.at(value).empty()) return cache.at(value);
auto& out = cache.at(value);
for (const int& i : iota(1, width + 1)) {
int y = round((double)i * 100.0 / width);
if (value >= y)
out += Theme::g(color_gradient).at(invert ? 100 - y : y) + Symbols::meter;
else {
out += Theme::c("meter_bg") + Symbols::meter * (width + 1 - i);
break;
}
}
out += Fx::reset;
return out;
}
//* Graph class ------------------------------------------------------------------------------------------------------------>
void Graph::_create(const deque<long long>& data, int data_offset) {
bool mult = (data.size() - data_offset > 1);
const auto& graph_symbol = Symbols::graph_symbols.at(symbol + '_' + (invert ? "down" : "up"));
array<int, 2> result;
const float mod = (height == 1) ? 0.3 : 0.1;
long long data_value = 0;
if (mult and data_offset > 0) {
last = data.at(data_offset - 1);
if (max_value > 0) last = clamp((last + offset) * 100 / max_value, 0ll, 100ll);
}
//? Horizontal iteration over values in <data>
for (const int& i : iota(data_offset, (int)data.size())) {
// if (tty_mode and mult and i % 2 != 0) continue;
if (not tty_mode and mult) current = not current;
if (i < 0) {
data_value = 0;
last = 0;
}
else {
data_value = data.at(i);
if (max_value > 0) data_value = clamp((data_value + offset) * 100 / max_value, 0ll, 100ll);
}
//? Vertical iteration over height of graph
for (const int& horizon : iota(0, height)) {
const int cur_high = (height > 1) ? round(100.0 * (height - horizon) / height) : 100;
const int cur_low = (height > 1) ? round(100.0 * (height - (horizon + 1)) / height) : 0;
//? Calculate previous + current value to fit two values in 1 braille character
for (int ai = 0; const auto& value : {last, data_value}) {
const int clamp_min = (no_zero and horizon == height - 1 and not (mult and i == data_offset and ai == 0)) ? 1 : 0;
if (value >= cur_high)
result[ai++] = 4;
else if (value <= cur_low)
result[ai++] = clamp_min;
else {
result[ai++] = clamp((int)round((float)(value - cur_low) * 4 / (cur_high - cur_low) + mod), clamp_min, 4);
}
}
//? Generate graph symbol from 5x5 2D vector
if (height == 1) {
if (result.at(0) + result.at(1) == 0) graphs.at(current).at(horizon) += Mv::r(1);
else {
if (not color_gradient.empty()) graphs.at(current).at(horizon) += Theme::g(color_gradient).at(clamp(max(last, data_value), 0ll, 100ll));
graphs.at(current).at(horizon) += graph_symbol.at((result.at(0) * 5 + result.at(1)));
}
}
else graphs.at(current).at(horizon) += graph_symbol.at((result.at(0) * 5 + result.at(1)));
}
if (mult and i >= 0) last = data_value;
}
last = data_value;
out.clear();
if (height == 1) {
//if (not color_gradient.empty())
// out += (last < 1 ? Theme::c("inactive_fg") : Theme::g(color_gradient).at(clamp(last, 0ll, 100ll)));
out += graphs.at(current).at(0);
}
else {
for (const int& i : iota(1, height + 1)) {
if (i > 1) out += Mv::d(1) + Mv::l(width);
if (not color_gradient.empty())
out += (invert) ? Theme::g(color_gradient).at(i * 100 / height) : Theme::g(color_gradient).at(100 - ((i - 1) * 100 / height));
out += (invert) ? graphs.at(current).at(height - i) : graphs.at(current).at(i-1);
}
}
if (not color_gradient.empty()) out += Fx::reset;
}
Graph::Graph() {}
Graph::Graph(int width, int height, const string& color_gradient,
const deque<long long>& data, const string& symbol,
bool invert, bool no_zero, long long max_value, long long offset)
: width(width), height(height), color_gradient(color_gradient),
invert(invert), no_zero(no_zero), offset(offset) {
if (Config::getB("tty_mode") or symbol == "tty") this->symbol = "tty";
else if (symbol != "default") this->symbol = symbol;
else this->symbol = Config::getS("graph_symbol");
if (this->symbol == "tty") tty_mode = true;
if (max_value == 0 and offset > 0) max_value = 100;
this->max_value = max_value;
const int value_width = (tty_mode ? data.size() : ceil((double)data.size() / 2));
int data_offset = (value_width > width) ? data.size() - width * (tty_mode ? 1 : 2) : 0;
if (not tty_mode and (data.size() - data_offset) % 2 != 0) {
data_offset--;
}
//? Populate the two switching graph vectors and fill empty space if data size < width
for (const int& i : iota(0, height * 2)) {
if (tty_mode and i % 2 != current) continue;
graphs[(i % 2 != 0)].push_back((value_width < width) ? ((height == 1) ? Mv::r(1) : " "s) * (width - value_width) : "");
}
if (data.size() == 0) return;
this->_create(data, data_offset);
}
string& Graph::operator()(const deque<long long>& data, bool data_same) {
if (data_same) return out;
//? Make room for new characters on graph
if (not tty_mode) current = not current;
for (const int& i : iota(0, height)) {
if (height == 1 and graphs.at(current).at(i).at(1) == '[') {
if (graphs.at(current).at(i).at(3) == 'C') graphs.at(current).at(i).erase(0, 4);
else graphs.at(current).at(i).erase(0, graphs.at(current).at(i).find_first_of('m') + 4);
}
else if (graphs.at(current).at(i).at(0) == ' ') graphs.at(current).at(i).erase(0, 1);
else graphs.at(current).at(i).erase(0, 3);
}
this->_create(data, (int)data.size() - 1);
return out;
}
string& Graph::operator()() {
return out;
}
//*------------------------------------------------------------------------------------------------------------------------->
}
namespace Cpu {
int width_p = 100, height_p = 32;
int min_width = 60, min_height = 8;
int x = 1, y = 1, width = 20, height;
int b_columns, b_column_size;
int b_x, b_y, b_width, b_height;
float max_observed_pwr = 1.0f;
float last_valid_cpu_watts = 0.0f;
int graph_up_height, graph_low_height;
int graph_up_width, graph_low_width;
int gpu_meter_width;
bool shown = true, redraw = true, mid_line = false;
string box;
vector<Draw::Graph> graphs_upper;
vector<Draw::Graph> graphs_lower;
Draw::Meter cpu_meter;
vector<Draw::Meter> gpu_meters;
vector<Draw::Graph> core_graphs;
vector<Draw::Graph> temp_graphs;
vector<Draw::Graph> gpu_temp_graphs;
vector<Draw::Graph> gpu_mem_graphs;
string draw(const cpu_info& cpu, const vector<Gpu::gpu_info>& gpus, bool force_redraw, bool data_same) {
if (Runner::stopping) return "";
if (force_redraw) redraw = true;
bool show_temps = (Config::getB("check_temp") and got_sensors);
bool show_watts = (Config::getB("show_cpu_watts") and supports_watts);
auto single_graph = Config::getB("cpu_single_graph");
bool hide_cores = show_temps and (cpu_temp_only or not Config::getB("show_coretemp"));
const int extra_width = (hide_cores ? max(6, 6 * b_column_size) : (b_columns == 1 && !show_temps) ? 8 : 0);
#ifdef GPU_SUPPORT
const auto& show_gpu_info = Config::getS("show_gpu_info");
const bool gpu_always = show_gpu_info == "On";
const bool gpu_auto = show_gpu_info == "Auto";
const bool show_gpu = (gpus.size() > 0 and (gpu_always or (gpu_auto and Gpu::shown < Gpu::count)));
#else
(void)gpus;
#endif
auto graph_up_field = Config::getS("cpu_graph_upper");
if (graph_up_field == "Auto" or not v_contains(Cpu::available_fields, graph_up_field))
graph_up_field = "total";
auto graph_lo_field = Config::getS("cpu_graph_lower");
if (graph_lo_field == "Auto" or not v_contains(Cpu::available_fields, graph_lo_field)) {
#ifdef GPU_SUPPORT
graph_lo_field = show_gpu ? "gpu-totals" : graph_up_field;
#else
graph_lo_field = graph_up_field;
#endif
}
auto tty_mode = Config::getB("tty_mode");
auto& graph_symbol = (tty_mode ? "tty" : Config::getS("graph_symbol_cpu"));
auto& graph_bg = Symbols::graph_symbols.at((graph_symbol == "default" ? Config::getS("graph_symbol") + "_up" : graph_symbol + "_up")).at(6);
auto& temp_scale = Config::getS("temp_scale");
auto cpu_bottom = Config::getB("cpu_bottom");
const string& title_left = Theme::c("cpu_box") + (cpu_bottom ? Symbols::title_left_down : Symbols::title_left);
const string& title_right = Theme::c("cpu_box") + (cpu_bottom ? Symbols::title_right_down : Symbols::title_right);
static int bat_pos = 0, bat_len = 0;
if (safeVal(cpu.cpu_percent, "total"s).empty()
or safeVal(cpu.core_percent, 0).empty()
or (show_temps and safeVal(cpu.temp, 0).empty())) return "";
if (safeVal(cpu.cpu_percent, "total"s).empty()
or safeVal(cpu.core_percent, 0).empty()
or (show_temps and safeVal(cpu.temp, 0).empty())) return "";
string out;
out.reserve(width * height);
//* Redraw elements not needed to be updated every cycle
if (redraw) {
mid_line = (not single_graph and graph_up_field != graph_lo_field);
graph_up_height = (single_graph ? height - 2 : ceil((double)(height - 2) / 2) - (mid_line and height % 2 != 0));
graph_low_height = height - 2 - graph_up_height - mid_line;
const int button_y = cpu_bottom ? y + height - 1 : y;
out += box;
//? Buttons on title
out += Mv::to(button_y, x + 10) + title_left + Theme::c("hi_fg") + Fx::b + 'm' + Theme::c("title") + "enu" + Fx::ub + title_right;
Input::mouse_mappings["m"] = {button_y, x + 11, 1, 4};
out += Mv::to(button_y, x + 16) + title_left + Theme::c("hi_fg") + Fx::b + 'p' + Theme::c("title") + "reset "
+ (Config::current_preset < 0 ? "*" : to_string(Config::current_preset)) + Fx::ub + title_right;
Input::mouse_mappings["p"] = {button_y, x + 17, 1, 8};
const string update = to_string(Config::getI("update_ms")) + "ms";
out += Mv::to(button_y, x + width - update.size() - 8) + title_left + Fx::b + Theme::c("hi_fg") + "- " + Theme::c("title") + update
+ Theme::c("hi_fg") + " +" + Fx::ub + title_right;
Input::mouse_mappings["-"] = {button_y, x + width - (int)update.size() - 7, 1, 2};
Input::mouse_mappings["+"] = {button_y, x + width - 5, 1, 2};
// Draw container engine name
if (Cpu::container_engine.has_value()) {
fmt::format_to(std::back_inserter(out), "{}{}{}{}{}", Mv::to(button_y, x + 28), title_left, Theme::c("title"), Cpu::container_engine.value(), title_right);
}
//? Graphs & meters
const int graph_default_width = x + width - b_width - 3;
auto init_graphs = [&](vector<Draw::Graph>& graphs, const int graph_height, int& graph_width, const string& graph_field, bool invert) {
#ifdef GPU_SUPPORT
if (graph_field.starts_with("gpu")) {
if (graph_field.find("totals") != string::npos) {
graphs.resize(gpus.size());
gpu_temp_graphs.resize(gpus.size());
gpu_mem_graphs.resize(gpus.size());
gpu_meters.resize(gpus.size());
const int gpu_draw_count = gpu_always ? Gpu::count : Gpu::count - Gpu::shown;
graph_width = gpu_draw_count <= 0 ? graph_default_width : graph_default_width/gpu_draw_count - gpu_draw_count + 1 + graph_default_width%gpu_draw_count;
for (size_t i = 0; i < gpus.size(); i++) {
if (gpu_auto and v_contains(Gpu::shown_panels, i))
continue;
auto& gpu = gpus[i]; auto& graph = graphs[i];
//? GPU graphs
if (gpu.supported_functions.gpu_utilization) {
if (i + 1 < gpus.size()) {
graph = Draw::Graph{graph_width, graph_height, "cpu", safeVal(gpu.gpu_percent, graph_field), graph_symbol, invert, true};
}
else {
graph = Draw::Graph{
graph_width + graph_default_width%graph_width - (int)gpus.size() + 1,
graph_height, "cpu", safeVal(gpu.gpu_percent, graph_field), graph_symbol, invert, true
};
}
}
}
} else {
graphs.resize(1);
graph_width = graph_default_width;
graphs[0] = Draw::Graph{ graph_width, graph_height, "cpu", safeVal(Gpu::shared_gpu_percent, graph_field), graph_symbol, invert, true };
}
}
else {
#endif
graphs.resize(1);
graph_width = graph_default_width;
graphs[0] = Draw::Graph{ graph_width, graph_height, "cpu", safeVal(cpu.cpu_percent, graph_field), graph_symbol, invert, true };
#ifdef GPU_SUPPORT
}
#endif
};
init_graphs(graphs_upper, graph_up_height, graph_up_width, graph_up_field, false);
if (not single_graph)
init_graphs(graphs_lower, graph_low_height, graph_low_width, graph_lo_field, Config::getB("cpu_invert_lower"));
#ifdef GPU_SUPPORT
if (show_gpu and b_columns > 1) {
gpu_temp_graphs.resize(gpus.size());
gpu_mem_graphs.resize(gpus.size());
gpu_meters.resize(gpus.size());
// Shrink gpu graph width in small boxes to prevent line width extending past box border
auto gpu_graph_width = b_width < 42 ? 4 : 5;
for (size_t i = 0; i < gpus.size(); i++) {
if (gpu_auto and v_contains(Gpu::shown_panels, i))
continue;
auto& gpu = gpus[i];
//? GPU graphs/meters
auto width_left = b_width - 10 - (gpus.size() > 9 ? 2 : gpus.size() > 1 ? 1 : 0);
if (gpu.supported_functions.temp_info and show_temps) {
gpu_temp_graphs[i] = Draw::Graph{ gpu_graph_width, 1, "temp", gpu.temp, graph_symbol, false, false, gpu.temp_max, -23 };
width_left -= 11;
}
if (gpu.supported_functions.mem_used and gpu.supported_functions.mem_total and b_columns > 1) {
gpu_mem_graphs[i] = Draw::Graph{ gpu_graph_width, 1, "used", safeVal(gpu.gpu_percent, "gpu-vram-totals"s), graph_symbol };
width_left -= 5;
}
width_left -= (gpu.supported_functions.mem_used ? 5 : 0);
width_left -= (gpu.supported_functions.mem_total ? 6 : 0);
width_left -= (gpu.supported_functions.pwr_usage ? 6 : 0);
if (gpu.supported_functions.gpu_utilization) {
gpu_meters[i] = Draw::Meter{width_left, "cpu" };
}
}
}
#endif
int cpu_meter_width = b_width - (show_temps ? 23 - (b_column_size <= 1 and b_columns == 1 ? 6 : 0) : 11);
if (show_watts) {
cpu_meter_width -= 6;
}
cpu_meter = Draw::Meter{cpu_meter_width, "cpu"};
if (mid_line) {
out += Mv::to(y + graph_up_height + 1, x) + Fx::ub + Theme::c("cpu_box") + Symbols::div_left + Theme::c("div_line")
+ Symbols::h_line * (width - b_width - 2) + Symbols::div_right
+ Mv::to(y + graph_up_height + 1, x + ((width - b_width) / 2) - ((graph_up_field.size() + graph_lo_field.size()) / 2) - 4)
+ Theme::c("main_fg") + graph_up_field + Mv::r(1) + "▲▼" + Mv::r(1) + graph_lo_field;
}
if (b_column_size > 0 or extra_width > 0) {
core_graphs.clear();
for (const auto& core_data : cpu.core_percent) {
core_graphs.emplace_back(5 * b_column_size + extra_width, 1, "cpu", core_data, graph_symbol);
}
}
if (show_temps) {
temp_graphs.clear();
temp_graphs.emplace_back(5, 1, "temp", safeVal(cpu.temp, 0), graph_symbol, false, false, cpu.temp_max, -23);
if (not hide_cores and b_column_size > 1) {
for (const auto& i : iota((size_t)1, cpu.temp.size())) {
temp_graphs.emplace_back(5, 1, "temp", safeVal(cpu.temp, i), graph_symbol, false, false, cpu.temp_max, -23);
}
}
}
}
//? Draw battery if enabled and present
if (Config::getB("show_battery") and has_battery) {
static int old_percent{}; // defaults to = 0
static long old_seconds{}; // defaults to = 0
static float old_watts{}; // defaults to = 0
static string old_status;
static Draw::Meter bat_meter {10, "cpu", true};
static const std::unordered_map<string, string> bat_symbols = {
{"charging", "▲"},
{"discharging", "▼"},
{"full", "■"},
{"unknown", "○"}
};
const auto& [percent, watts, seconds, status] = current_bat;
if (redraw or percent != old_percent or (watts != old_watts and Config::getB("show_battery_watts")) or seconds != old_seconds or status != old_status) {
old_percent = percent;
old_watts = watts;
old_seconds = seconds;
old_status = status;
const string str_time = (seconds > 0 ? sec_to_dhms(seconds, false, true) : "");
const string str_percent = to_string(percent) + '%';
const string str_watts = (watts != -1 and Config::getB("show_battery_watts") ? fmt::format("{:.2f}", watts) + 'W' : "");
const auto& bat_symbol = bat_symbols.at((bat_symbols.contains(status) ? status : "unknown"));
const int current_len = (Term::width >= 100 ? 11 : 0) + str_time.size() + str_percent.size() + str_watts.size() + to_string(Config::getI("update_ms")).size();
const int current_pos = Term::width - current_len - 17;
if ((bat_pos != current_pos or bat_len != current_len) and bat_pos > 0 and not redraw)
out += Mv::to(y, bat_pos) + Fx::ub + Theme::c("cpu_box") + Symbols::h_line * (bat_len + 4);
bat_pos = current_pos;
bat_len = current_len;
out += Mv::to(y, bat_pos) + title_left + Theme::c("title") + Fx::b + "BAT" + bat_symbol + ' ' + str_percent
+ (Term::width >= 100 ? Fx::ub + ' ' + bat_meter(percent) + Fx::b : "")
+ (not str_time.empty() ? ' ' + Theme::c("title") + str_time : "") + (not str_watts.empty() ? " " + Theme::c("title") + Fx::b + str_watts : "") + Fx::ub + title_right;
}
}
else if (bat_pos > 0) {
out += Mv::to(y, bat_pos) + Fx::ub + Theme::c("cpu_box") + Symbols::h_line * (bat_len + 4);
bat_pos = bat_len = 0;
}
try {
//? Cpu/Gpu graphs
out += Fx::ub + Mv::to(y + 1, x + 1);
auto draw_graphs = [&](vector<Draw::Graph>& graphs, const int graph_height, const int graph_width, const string& graph_field) {
#ifdef GPU_SUPPORT
if (graph_field.starts_with("gpu"))
if (graph_field.ends_with("totals")) {
int gpu_drawn = 0;
for (size_t i = 0; i < gpus.size(); i++) {
if (gpu_auto and v_contains(Gpu::shown_panels, i)) {
continue;
}
try {
const auto& gpu_percent = gpus[i].gpu_percent;
out += graphs[i](safeVal(gpu_percent, graph_field), (data_same or redraw));
} catch (std::out_of_range& /* unused */) {
continue;
}
if (Gpu::count - (gpu_auto ? Gpu::shown : 0) > 1) {
auto i_str = to_string(i);
out += Mv::l(graph_width-1) + Mv::u(graph_height/2) + (graph_width > 5 ? "GPU" : "") + i_str
+ Mv::d(graph_height/2) + Mv::r(graph_width - 1 - (graph_width > 5)*3 - i_str.size());
}
if (++gpu_drawn < Gpu::count - (gpu_auto ? Gpu::shown : 0))
out += Theme::c("div_line") + (Symbols::v_line + Mv::l(1) + Mv::u(1))*graph_height + Mv::r(1) + Mv::d(1);
}
}
else
out += graphs[0](safeVal(Gpu::shared_gpu_percent, graph_field), (data_same or redraw));
else
#else
(void)graph_height;
(void)graph_width;
#endif
out += graphs[0](safeVal(cpu.cpu_percent, graph_field), (data_same or redraw));
};
draw_graphs(graphs_upper, graph_up_height, graph_up_width, graph_up_field);
if (not single_graph) {
out += Mv::to(y + graph_up_height + 1 + mid_line, x + 1);
draw_graphs(graphs_lower, graph_low_height, graph_low_width, graph_lo_field);
}
//? Uptime
if (Config::getB("show_uptime")) {
string upstr = sec_to_dhms(system_uptime());
if (upstr.size() > 8) {
upstr.resize(upstr.size() - 3);
upstr = trans(upstr);
}
out += Mv::to(y + (single_graph or not Config::getB("cpu_invert_lower") ? 1 : height - 2), x + 2)
+ Theme::c("graph_text") + "up" + Mv::r(1) + upstr;
}
#ifdef __linux__
const bool freq_range = Config::getS("freq_mode") == "range";
#else
const bool freq_range = false;
#endif
//? Cpu clock and cpu meter
if (Config::getB("show_cpu_freq") and not cpuHz.empty())
out += Mv::to(b_y, b_x + b_width - (freq_range ? 20 : 10)) + Fx::ub + Theme::c("div_line")
+ Symbols::h_line * ((freq_range ? 17 : 7) - cpuHz.size())
+ Symbols::title_left + Fx::b + Theme::c("title") + cpuHz + Fx::ub + Theme::c("div_line") + Symbols::title_right;
out += Mv::to(b_y + 1, b_x + 1) + Theme::c("main_fg") + Fx::b + "CPU " + cpu_meter(safeVal(cpu.cpu_percent, "total"s).back())
+ Theme::g("cpu").at(clamp(safeVal(cpu.cpu_percent, "total"s).back(), 0ll, 100ll)) + rjust(to_string(safeVal(cpu.cpu_percent, "total"s).back()), 4) + Theme::c("main_fg") + '%';
if (show_temps) {
const auto [temp, unit] = celsius_to(safeVal(cpu.temp, 0).back(), temp_scale);
const auto temp_color = Theme::g("temp").at(clamp(safeVal(cpu.temp, 0).back() * 100 / cpu.temp_max, 0ll, 100ll));
if ((b_column_size > 1 or b_columns > 1) and temp_graphs.size() >= 1ll)
out += ' ' + Theme::c("inactive_fg") + graph_bg * 5 + Mv::l(5) + temp_color
+ temp_graphs.at(0)(safeVal(cpu.temp, 0), data_same or redraw);
out += rjust(to_string(temp), 4) + Theme::c("main_fg") + unit;
}
if (show_watts) {
// Check for good cpu watts reading. It is rare but there has been at least one system
// that has given a bad reading for one collect cycle every once in while.
const auto watts_error = cpu.usage_watts < 0.0f or cpu.usage_watts > 9'999.0f;
if (not watts_error) last_valid_cpu_watts = cpu.usage_watts;
max_observed_pwr = max(max_observed_pwr, last_valid_cpu_watts);
fmt::format_to(std::back_inserter(out), " {watts_color}{cwatts:>4.{precision}f}{main_fg}W",
"watts_color"_a = Theme::g("cached").at(clamp(last_valid_cpu_watts / max_observed_pwr * 100.0f, 0.0f, 100.0f)),
"cwatts"_a = last_valid_cpu_watts,
"precision"_a = last_valid_cpu_watts < 9.995f ? 2 : last_valid_cpu_watts < 99.95f ? 1 : 0,
"main_fg"_a = Theme::c("main_fg"));
}
out += Theme::c("div_line") + Symbols::v_line;
} catch (const std::exception& e) {
throw std::runtime_error("graphs, clock, meter : " + string{e.what()});
}
int max_row = b_height - 3; // Subtracting one extra row for the load average (and power if enabled)
int n_gpus_to_show = 0;
#ifdef GPU_SUPPORT
n_gpus_to_show = show_gpu ? (gpus.size() - (gpu_always ? 0 : Gpu::shown)) : 0;
#endif
max_row -= n_gpus_to_show;
auto is_cpu_enabled = [&cpu](const std::int32_t num) -> bool {
return !cpu.active_cpus.has_value() || std::ranges::find(cpu.active_cpus.value(), num) != cpu.active_cpus.value().end();
};
//? Core text and graphs
int cx = 0, cy = 1, cc = 0, core_width = (b_column_size == 0 ? 2 : 3);
if (Shared::coreCount >= 100) core_width++;
for (const auto& n : iota(0, Shared::coreCount)) {
auto enabled = is_cpu_enabled(n);
out += Mv::to(b_y + cy + 1, b_x + cx + 1) + Theme::c(enabled ? "main_fg" : "inactive_fg") + (Shared::coreCount < 100 ? Fx::b + 'C' + Fx::ub : "")
+ ljust(to_string(n), core_width);
if ((b_column_size > 0 or extra_width > 0) and cmp_less(n, core_graphs.size()))
out += Theme::c("inactive_fg") + graph_bg * (5 * b_column_size + extra_width) + Mv::l(5 * b_column_size + extra_width)
+ core_graphs.at(n)(safeVal(cpu.core_percent, n), data_same or redraw);
out += enabled ? Theme::g("cpu").at(clamp(safeVal(cpu.core_percent, n).back(), 0ll, 100ll)) : Theme::c("inactive_fg");
out += rjust(to_string(safeVal(cpu.core_percent, n).back()), (b_column_size < 2 ? 3 : 4)) + Theme::c(enabled ? "main_fg" : "inactive_fg") + '%';
if (show_temps and not hide_cores) {
const auto [temp, unit] = celsius_to(safeVal(cpu.temp, n+1).back(), temp_scale);
const auto temp_color = enabled ? Theme::g("temp").at(clamp(safeVal(cpu.temp, n+1).back() * 100 / cpu.temp_max, 0ll, 100ll)) : Theme::c("inactive_fg");
if (b_column_size > 1 and std::cmp_greater_equal(temp_graphs.size(), n))
out += ' ' + Theme::c("inactive_fg") + graph_bg * 5 + Mv::l(5)
+ temp_graphs.at(n+1)(safeVal(cpu.temp, n+1), data_same or redraw);
out += temp_color + rjust(to_string(temp), 4) + Theme::c(enabled ? "main_fg" : "inactive_fg") + unit;
}
out += Theme::c("div_line") + Symbols::v_line;
if ((++cy > ceil((double)Shared::coreCount / b_columns) or cy == max_row) and n != Shared::coreCount - 1) {
if (++cc >= b_columns) break;
cy = 1; cx = (b_width / b_columns) * cc;
}
}
//? Load average
if (cy < b_height - 1 and cc <= b_columns) {
cy = b_height - 2 - n_gpus_to_show;
string load_avg_pre = "Load avg:";
string load_avg;
for (const auto& val : cpu.load_avg) {
load_avg += fmt::format(" {:.2f}", val);
}
int len = load_avg_pre.size() + load_avg.size();
out += Mv::to(b_y + cy, b_x + 1) + string(max(b_width - len - 2, 0), ' ') + Theme::c("main_fg") + Fx::b + load_avg_pre + Fx::ub + load_avg;
}
#ifdef GPU_SUPPORT
//? Gpu brief info
if (show_gpu) {
for (unsigned long i = 0; i < gpus.size(); ++i) {
if (gpu_auto and v_contains(Gpu::shown_panels, i))
continue;
out += Mv::to(b_y + ++cy, b_x + 1) + Theme::c("main_fg") + Fx::b + "GPU";
if (gpus.size() > 1) out += rjust(to_string(i), 1 + (gpus.size() > 9));
if (gpus[i].supported_functions.gpu_utilization) {
out += ' ';
if (b_columns > 1) {
out += gpu_meters[i](safeVal(gpus[i].gpu_percent, "gpu-totals"s).back())
+ Theme::g("cpu").at(clamp(safeVal(gpus[i].gpu_percent, "gpu-totals"s).back(), 0ll, 100ll));
}
out += rjust(to_string(safeVal(gpus[i].gpu_percent, "gpu-totals"s).back()), 3) + Theme::c("main_fg") + '%';
if (b_columns == 1)
out += ' ';
}
if (gpus[i].supported_functions.mem_used and gpus[i].supported_functions.mem_total and b_columns > 1) {
out += ' ' + Theme::c("inactive_fg") + graph_bg * 5 + Mv::l(5) + Theme::g("used").at(safeVal(gpus[i].gpu_percent, "gpu-vram-totals"s).back())
+ gpu_mem_graphs[i](safeVal(gpus[i].gpu_percent, "gpu-vram-totals"s), data_same or redraw);
}
if (gpus[i].supported_functions.mem_used) {
out += Theme::c("main_fg")
+ rjust(floating_humanizer(gpus[i].mem_used, true), 5);
}
if (gpus[i].supported_functions.mem_total) {
out += Theme::c("inactive_fg") + '/' + Theme::c("main_fg") + ljust(floating_humanizer(gpus[i].mem_total, true), 4);
}
if (show_temps and gpus[i].supported_functions.temp_info) {
const auto [temp, unit] = celsius_to(gpus[i].temp.back(), temp_scale);
out += ' ';
if (b_columns > 1)
out += Theme::c("inactive_fg") + graph_bg * 5 + Mv::l(5) + Theme::g("temp").at(clamp(gpus[i].temp.back() * 100 / gpus[i].temp_max, 0ll, 100ll))
+ gpu_temp_graphs[i](gpus[i].temp, data_same or redraw);
else out += Theme::g("temp").at(clamp(gpus[i].temp.back() * 100 / gpus[i].temp_max, 0ll, 100ll));
out += rjust(to_string(temp), 3) + Theme::c("main_fg") + unit;
}
if (gpus[i].supported_functions.pwr_usage) {
fmt::format_to(std::back_inserter(out), " {watts_color}{gpu_watts:>4.{precision}f}{main_fg}W",
"watts_color"_a = Theme::g("cached").at(clamp(safeVal(gpus[i].gpu_percent, "gpu-pwr-totals"s).back(), 0ll, 100ll)),
"gpu_watts"_a = gpus[i].pwr_usage / 1000.0,
"precision"_a = gpus[i].pwr_usage < 9'995 ? 2 : gpus[i].pwr_usage < 99'950 ? 1 : 0,
"main_fg"_a = Theme::c("main_fg"));
}
if (cy > b_height - 1) break;
}
}
#endif
redraw = false;
return out + Fx::reset;
}
}
#ifdef GPU_SUPPORT
namespace Gpu {
int width_p = 100, height_p = 32;
int min_width = 41, min_height = 8;
int width = 41, total_height;
vector<int> x_vec = {}, y_vec = {}, b_height_vec = {};
int b_width;
vector<int> b_x_vec = {}, b_y_vec = {};
vector<bool> redraw = {};
int shown = 0;
int count = 0;
vector<int> shown_panels = {};
int graph_up_height;
vector<Draw::Graph> graph_upper_vec = {}, graph_lower_vec = {};
vector<Draw::Graph> temp_graph_vec = {};
vector<Draw::Graph> mem_used_graph_vec = {}, mem_util_graph_vec = {};
vector<Draw::Meter> gpu_meter_vec = {};
vector<Draw::Meter> pwr_meter_vec = {};
vector<Draw::Meter> enc_meter_vec = {};
vector<string> box = {};