From d5aa08be7b23be5b6eacb4caa77492fb1679dba2 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Mon, 31 May 2021 21:47:41 +0200 Subject: [PATCH] Graph changes --- btop.cpp | 35 +++++++++++++++++++++-------------- src/btop_draw.h | 30 +++++++++++++++--------------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/btop.cpp b/btop.cpp index f44b07c..f530b32 100644 --- a/btop.cpp +++ b/btop.cpp @@ -39,6 +39,7 @@ namespace Global { {"#801414", "██████╔╝ ██║ ╚██████╔╝██║ ╚═╝ ╚═╝"}, {"#000000", "╚═════╝ ╚═╝ ╚═════╝ ╚═╝"}, }; + const uint banner_width = 49; const std::string Version = "0.0.10"; } @@ -78,7 +79,6 @@ using namespace Tools; namespace Global { string banner; - const uint banner_width = 49; fs::path self_path; @@ -341,48 +341,55 @@ int main(int argc, char **argv){ vector mydata; for (long long i = 0; i <= 100; i++) mydata.push_back(i); for (long long i = 100; i >= 0; i--) mydata.push_back(i); - mydata.push_back(0); - mydata.push_back(0); - mydata.push_back(100); + // mydata.push_back(0); + // mydata.push_back(0); + // mydata.push_back(100); // for (long long i = 0; i <= 100; i++) mydata.push_back(i); // for (long long i = 100; i >= 0; i--) mydata.push_back(i); Draw::Graph kgraph {}; + Draw::Meter kmeter {}; Draw::Graph kgraph2 {}; Draw::Graph kgraph3 {}; auto kts = time_micros(); kgraph(Term::width, 10, "process", mydata, false, false); + kmeter(Term::width, "process"); + // cout << Mv::save << kgraph(mydata) << "\n\nInit took " << time_micros() - kts << " μs. " << endl; + + // exit(0); kgraph2(Term::width, 10, "process", mydata, true, false); kgraph3(Term::width, 1, "process", mydata, false, false); // cout << kgraph() << endl; // cout << kgraph2() << endl; // exit(0); - cout << Mv::save << kgraph(mydata) << "\n" << kgraph2(mydata) << "\n" << kgraph3(mydata) << "\n\nInit took " << time_micros() - kts << " μs. " << endl; + // cout << Mv::save << kgraph(mydata) << "\n" << kmeter(mydata.back()) << "\n\nInit took " << time_micros() - kts << " μs. " << endl; + cout << Mv::save << kgraph(mydata) << "\n" << kgraph2(mydata) << "\n" << kgraph3(mydata) << "\n" << kmeter(mydata.back()) << "\n\nInit took " << time_micros() - kts << " μs. " << endl; // sleep_ms(1000); // mydata.push_back(50); // cout << Mv::restore << kgraph(mydata) << "\n" << kgraph2(mydata) << "\n\nInit took " << time_micros() - kts << " μs. " << endl; // exit(0); - // int x = 0; - // long long y = 0; - // bool flip = false; + // int x = 0q; + long long y = 0; + bool flip = false; list ktavg; while (true) { - mydata.back() = std::rand() % 101; - // mydata.back() = y; + // mydata.back() = std::rand() % 101; + mydata.back() = y; kts = time_micros(); // cout << Mv::restore << " "s * Term::width << "\n" << " "s * Term::width << endl; - cout << Mv::restore << kgraph(mydata) << "\n" << kgraph2(mydata) << "\n" << " "s * Term::width << Mv::l(Term::width) << kgraph3(mydata) << endl; + // cout << Mv::restore << kgraph(mydata) << "\n" << kmeter(mydata.back()) << endl; + cout << Mv::restore << kgraph(mydata) << "\n" << kgraph2(mydata) << "\n" << " "s * Term::width << Mv::l(Term::width) << kgraph3(mydata) << "\n" << kmeter(mydata.back()) << endl; ktavg.push_front(time_micros() - kts); if (ktavg.size() > 100) ktavg.pop_back(); cout << "Time: " << ktavg.front() << " μs. Avg: " << accumulate(ktavg.begin(), ktavg.end(), 0) / ktavg.size() << " μs. " << flush; - // if (flip) y--; - // else y++; - // if (y == 100 || y == 0) flip = !flip; + if (flip) y--; + else y++; + if (y == 100 || y == 0) flip = !flip; if (Input::poll()) { if (Input::get() == "space") Input::wait(true); else break; diff --git a/src/btop_draw.h b/src/btop_draw.h index 03beff6..f6a0ec8 100644 --- a/src/btop_draw.h +++ b/src/btop_draw.h @@ -21,6 +21,7 @@ tab-size = 4 #include #include #include +#include #include #include #include @@ -186,11 +187,11 @@ namespace Draw { unordered_flat_map graph_symbol; //* Create two representations of the graph to switch between to represent two values for each braille character - void _create(vector& data, int data_offset) { - bool mult = (data.size() - data_offset > 1); + void _create(const vector& data, int data_offset) { + const bool mult = (data.size() - data_offset > 1); if (mult && (data.size() - data_offset) % 2 != 0) data_offset--; - unordered_flat_map shifter; - unordered_flat_map result; + vector result; + const float mod = (height == 1) ? 0.3 : 0.1; long long data_value = 0; if (mult && data_offset > 0) { last = data[data_offset - 1]; @@ -204,20 +205,19 @@ namespace Draw { if (i == -1) { data_value = 0; last = 0; } else data_value = data[i]; if (max_value > 0) data_value = clamp((data_value + offset) * 100 / max_value, 0ll, 100ll); - shifter = { {"left", last}, {"right", data_value} }; - for (auto [side, value] : shifter) { + result.clear(); + for (auto value : {last, data_value}) { if (value >= cur_high) - result[side] = 4; - else if (value <= cur_low) - result[side] = 0; + result.push_back(4); + else if (value < cur_low) + result.push_back(0); else { - if (height == 1) result[side] = round((float)value * 4 / 100 + 0.3); - else result[side] = round((float)(value - cur_low) * 4 / (cur_high - cur_low) + 0.1); + result.push_back(round((float)(value - cur_low) * 4 / (cur_high - cur_low) + mod)); + if (no_zero && horizon == height - 1 && i != -1 && result.back() == 0) result.back() = 1; } - if (no_zero && horizon == height - 1 && i != -1 && result[side] == 0) result[side] = 1; } if (mult && i > data_offset) last = data_value; - graphs[current][horizon] += graph_symbol[(float)result["left"] + (float)result["right"] / 10]; + graphs[current][horizon] += graph_symbol[(float)result.at(0) + (float)result.at(1) / 10]; } } last = data_value; @@ -236,7 +236,7 @@ namespace Draw { public: //* Set graph options and initialize with data - void operator()(int width, int height, string color_gradient, vector data, bool invert = false, bool no_zero = false, long long max_value = 0, long long offset = 0, bool data_same = true) { + void operator()(int width, int height, string color_gradient, const vector& data, bool invert = false, bool no_zero = false, long long max_value = 0, long long offset = 0, bool data_same = true) { graphs[true].clear(); graphs[false].clear(); this->width = width; this->height = height; this->invert = invert; this->offset = offset; @@ -262,7 +262,7 @@ namespace Draw { } //* Add number of values from back of and return string representation of graph - string operator()(vector& data, int num = 1) { + string operator()(const vector& data, const int num = 1) { if (data_same) {data_same = false; return out;} current = !current;