From 224ade22fa2a5ccd92071c3bcf9c676321513c1d Mon Sep 17 00:00:00 2001 From: aristocratos Date: Sun, 9 May 2021 00:18:51 +0200 Subject: [PATCH] Refactoring and new functions --- btop.cpp | 61 +++++++++++++++++++---------------- src/btop_config.h | 1 + src/btop_input.h | 11 +++---- src/btop_theme.h | 2 +- src/btop_tools.h | 82 +++++++++++++++++++++++++++++++++++------------ 5 files changed, 101 insertions(+), 56 deletions(-) diff --git a/btop.cpp b/btop.cpp index 868bc69..dd6cb0f 100644 --- a/btop.cpp +++ b/btop.cpp @@ -16,8 +16,6 @@ indent = tab tab-size = 4 */ - - #include #include #include @@ -107,7 +105,6 @@ public: string b_color, bg, fg, out, oc, letter; int bg_i; int new_len; - banner_str = ""; for (auto line: Global::Banner_src) { new_len = ulen(line[1]); if (new_len > width) width = new_len; @@ -141,69 +138,77 @@ public: //? --------------------------------------------- Main starts here! --------------------------------------------------- int main(int argc, char **argv){ - int debug = 0; - int tests = 0; //? Init cout.setf(std::ios::boolalpha); - if (argc > 1) argumentParser(argc, argv); + //? Initialize terminal and set options C_Term Term; - if (!Term.initialized) { - cout << "No terminal detected!" << endl; + cout << "ERROR: No tty detected!" << endl; + cout << "Sorry, btop++ needs an interactive shell to run." << endl; exit(1); } + //? Read config file if present C_Config Config; + + //? Generate the theme C_Theme Theme(Global::Default_theme); + + //? Create the btop++ banner C_Banner Banner; + + //? Initialize the Input class C_Input Input; - cout << Theme("main_bg") << Term.clear << flush; - // bool thread_test = false; + //* ------------------------------------------------ TESTING ------------------------------------------------------ + + int debug = 2; + int tests = 0; + + // cout << Theme("main_bg") << Term.clear << flush; + // bool thread_test = false; if (debug < 2) cout << Term.alt_screen << Term.clear << Term.hide_cursor << flush; cout << Theme("main_fg") << endl; cout << Mv::r(Term.width / 2 - Banner.width / 2) << Banner() << endl; + cout << string(50, '-') << Mv::l(50) << flush; + cout << rjustify("Terminal Width=", 20) << trans("Korven s kommer ") << Term.width << " Height=" << Term.height << endl; //* Test MENUS - for (auto& outer : Global::Menus){ - for (auto& inner : outer.second){ - for (auto& item : inner.second){ - cout << item << endl; - } - } - } + // for (auto& outer : Global::Menus){ + // for (auto& inner : outer.second){ + // for (auto& item : inner.second){ + // cout << item << endl; + // } + // } + // } - string korv5 = "hejsan"; - if (korv5.starts_with("hej")) cout << "hej" << endl; - - //cout << korv2.size() << " " << ulen(korv2) << endl; - - cout << Config(Bool, "truecolor") << endl; - cout << Config(Int, "tree_depth") << endl; - cout << Config(String, "color_theme") << endl; + // cout << Config(Bool, "truecolor") << endl; + // cout << Config(Int, "tree_depth") << endl; + // cout << Config(String, "color_theme") << endl; //* Test theme int i = 0; - if (tests==0) for(auto& item : Global::Default_theme) { + if (tests>0) for(auto& item : Global::Default_theme) { cout << Theme(item.first) << item.first << ":" << Theme("main_fg") << Theme(item.first).erase(0, 2) << Fx::reset << " "; if (++i == 4) { i = 0; cout << endl; } + cout << Fx::reset << endl; } - cout << Fx::reset << endl; + // if (thread_test){ // int max = 50000; @@ -278,7 +283,7 @@ int main(int argc, char **argv){ //if (tests>5){ - cout << "Width=" << Term.width << " Height=" << Term.height << endl; + //} // map dict = { diff --git a/src/btop_config.h b/src/btop_config.h index 0de604a..4cc9d91 100644 --- a/src/btop_config.h +++ b/src/btop_config.h @@ -39,6 +39,7 @@ using std::string, std::to_string, std::vector, std::map; namespace State { bool truecolor = true; string fg, bg; + unsigned width, height; } class C_Config { diff --git a/src/btop_input.h b/src/btop_input.h index 1b44077..85c8ecf 100644 --- a/src/btop_input.h +++ b/src/btop_input.h @@ -21,8 +21,6 @@ tab-size = 4 #include #include -#include -#include #include #include @@ -78,16 +76,16 @@ class C_Input { auto timer = 0; while (timeout == -1 || timer * 10 <= timeout) { if (cin.rdbuf()->in_avail() > 0) return true; - std::this_thread::sleep_for(std::chrono::milliseconds(10)); + sleep_ms(10); if (timeout >= 0) ++timer; } return false; } string get(){ - string key = ""; + string key; while (cin.rdbuf()->in_avail() > 0 && key.size() < 100) key += cin.get(); - if (key != ""){ + if (!key.empty()){ if (key.substr(0,2) == Fx::e) key.erase(0, 1); if (Key_escapes.contains(key)) key = Key_escapes.at(key); else if (ulen(key) > 1) key = ""; @@ -98,12 +96,11 @@ class C_Input { public: //* Wait ms for input on stdin and return true if available - //* 0 to just check for input //* -1 for infinite wait bool operator()(int timeout){ if (wait(timeout)) { last = get(); - return last != ""; + return !last.empty(); } else { last = ""; return false; diff --git a/src/btop_theme.h b/src/btop_theme.h index 799a8cd..ec08f4f 100644 --- a/src/btop_theme.h +++ b/src/btop_theme.h @@ -129,7 +129,7 @@ class C_Theme { } } else out[item.first] = ""; - if (out[item.first] == "") out[item.first] = hex_to_color(item.second, !State::truecolor, depth); + if (out[item.first].empty()) out[item.first] = hex_to_color(item.second, !State::truecolor, depth); } return out; } diff --git a/src/btop_tools.h b/src/btop_tools.h index c5fbbd2..08ee5d7 100644 --- a/src/btop_tools.h +++ b/src/btop_tools.h @@ -23,14 +23,18 @@ tab-size = 4 #include #include #include +#include +#include +#include #include #include #include #include +#include -using std::string, std::to_string, std::round, std::vector, std::map, std::cin; +using std::string, std::to_string, std::round, std::vector, std::map, std::cin, std::max; //? ------------------------------------------------- NAMESPACES ------------------------------------------------------ @@ -38,32 +42,28 @@ using std::string, std::to_string, std::round, std::vector, std::map, std::cin; namespace Fx { //* Escape sequence start const string e = "\x1b["; + //* Bold on const string b = e + "1m"; - //* Bold off - const string ub = e + "22m"; + //* Dark on const string d = e + "2m"; - //* Dark off - const string ud = e + "22m"; + //* Italic on const string i = e + "3m"; - //* Italic off - const string ui = e + "23m"; + //* Underline on const string ul = e + "4m"; - //* Underline off - const string uul = e + "24m"; + //* Blink on const string bl = e + "5m"; - //* Blink off - const string ubl = e + "25m"; + //* Strike / crossed-out on const string s = e + "9m"; - //* Strike / crossed-out off - const string us = e + "29m"; + //* Reset foreground/background color and text effects const string reset_base = e + "0m"; + //* Reset text effects and restore default foregrund and background color < Changed by C_Theme string reset = reset_base; }; @@ -72,16 +72,22 @@ namespace Fx { namespace Mv { //* Move cursor to , inline string to(int line, int col){ return Fx::e + to_string(line) + ";" + to_string(col) + "f";} + //* Move cursor right columns inline string r(int x){ return Fx::e + to_string(x) + "C";} + //* Move cursor left columns inline string l(int x){ return Fx::e + to_string(x) + "D";} + //* Move cursor up x lines inline string u(int x){ return Fx::e + to_string(x) + "A";} + //* Move cursor down x lines inline string d(int x) { return Fx::e + to_string(x) + "B";} + //* Save cursor position const string save = Fx::e + "s"; + //* Restore saved cursor postion const string restore = Fx::e + "u"; }; @@ -127,16 +133,16 @@ string trim(string str, string t_str = " "){ return ltrim(rtrim(str, t_str), t_str); } -//* Split at (0 for unlimited) times and return vector +//* Split at