Refactoring and new functions

This commit is contained in:
aristocratos 2021-05-09 00:18:51 +02:00
parent 3b3a93935d
commit 224ade22fa
5 changed files with 101 additions and 56 deletions

View file

@ -16,8 +16,6 @@ indent = tab
tab-size = 4 tab-size = 4
*/ */
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <cmath> #include <cmath>
@ -107,7 +105,6 @@ public:
string b_color, bg, fg, out, oc, letter; string b_color, bg, fg, out, oc, letter;
int bg_i; int bg_i;
int new_len; int new_len;
banner_str = "";
for (auto line: Global::Banner_src) { for (auto line: Global::Banner_src) {
new_len = ulen(line[1]); new_len = ulen(line[1]);
if (new_len > width) width = new_len; if (new_len > width) width = new_len;
@ -141,69 +138,77 @@ public:
//? --------------------------------------------- Main starts here! --------------------------------------------------- //? --------------------------------------------- Main starts here! ---------------------------------------------------
int main(int argc, char **argv){ int main(int argc, char **argv){
int debug = 0;
int tests = 0;
//? Init //? Init
cout.setf(std::ios::boolalpha); cout.setf(std::ios::boolalpha);
if (argc > 1) argumentParser(argc, argv); if (argc > 1) argumentParser(argc, argv);
//? Initialize terminal and set options
C_Term Term; C_Term Term;
if (!Term.initialized) { 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); exit(1);
} }
//? Read config file if present
C_Config Config; C_Config Config;
//? Generate the theme
C_Theme Theme(Global::Default_theme); C_Theme Theme(Global::Default_theme);
//? Create the btop++ banner
C_Banner Banner; C_Banner Banner;
//? Initialize the Input class
C_Input Input; 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; if (debug < 2) cout << Term.alt_screen << Term.clear << Term.hide_cursor << flush;
cout << Theme("main_fg") << endl; cout << Theme("main_fg") << endl;
cout << Mv::r(Term.width / 2 - Banner.width / 2) << Banner() << 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 //* Test MENUS
for (auto& outer : Global::Menus){ // for (auto& outer : Global::Menus){
for (auto& inner : outer.second){ // for (auto& inner : outer.second){
for (auto& item : inner.second){ // for (auto& item : inner.second){
cout << item << endl; // cout << item << endl;
} // }
} // }
} // }
string korv5 = "hejsan";
if (korv5.starts_with("hej")) cout << "hej" << endl;
// cout << Config(Bool, "truecolor") << endl;
//cout << korv2.size() << " " << ulen(korv2) << 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 //* Test theme
int i = 0; 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 << " "; cout << Theme(item.first) << item.first << ":" << Theme("main_fg") << Theme(item.first).erase(0, 2) << Fx::reset << " ";
if (++i == 4) { if (++i == 4) {
i = 0; i = 0;
cout << endl; cout << endl;
} }
cout << Fx::reset << endl;
} }
cout << Fx::reset << endl;
// if (thread_test){ // if (thread_test){
// int max = 50000; // int max = 50000;
@ -278,7 +283,7 @@ int main(int argc, char **argv){
//if (tests>5){ //if (tests>5){
cout << "Width=" << Term.width << " Height=" << Term.height << endl;
//} //}
// map<string, string> dict = { // map<string, string> dict = {

View file

@ -39,6 +39,7 @@ using std::string, std::to_string, std::vector, std::map;
namespace State { namespace State {
bool truecolor = true; bool truecolor = true;
string fg, bg; string fg, bg;
unsigned width, height;
} }
class C_Config { class C_Config {

View file

@ -21,8 +21,6 @@ tab-size = 4
#include <string> #include <string>
#include <map> #include <map>
#include <chrono>
#include <thread>
#include <iostream> #include <iostream>
#include <btop_globs.h> #include <btop_globs.h>
@ -78,16 +76,16 @@ class C_Input {
auto timer = 0; auto timer = 0;
while (timeout == -1 || timer * 10 <= timeout) { while (timeout == -1 || timer * 10 <= timeout) {
if (cin.rdbuf()->in_avail() > 0) return true; if (cin.rdbuf()->in_avail() > 0) return true;
std::this_thread::sleep_for(std::chrono::milliseconds(10)); sleep_ms(10);
if (timeout >= 0) ++timer; if (timeout >= 0) ++timer;
} }
return false; return false;
} }
string get(){ string get(){
string key = ""; string key;
while (cin.rdbuf()->in_avail() > 0 && key.size() < 100) key += cin.get(); 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.substr(0,2) == Fx::e) key.erase(0, 1);
if (Key_escapes.contains(key)) key = Key_escapes.at(key); if (Key_escapes.contains(key)) key = Key_escapes.at(key);
else if (ulen(key) > 1) key = ""; else if (ulen(key) > 1) key = "";
@ -98,12 +96,11 @@ class C_Input {
public: public:
//* Wait <timeout> ms for input on stdin and return true if available //* Wait <timeout> ms for input on stdin and return true if available
//* 0 to just check for input
//* -1 for infinite wait //* -1 for infinite wait
bool operator()(int timeout){ bool operator()(int timeout){
if (wait(timeout)) { if (wait(timeout)) {
last = get(); last = get();
return last != ""; return !last.empty();
} else { } else {
last = ""; last = "";
return false; return false;

View file

@ -129,7 +129,7 @@ class C_Theme {
} }
} }
else out[item.first] = ""; 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; return out;
} }

View file

@ -23,14 +23,18 @@ tab-size = 4
#include <cmath> #include <cmath>
#include <vector> #include <vector>
#include <map> #include <map>
#include <chrono>
#include <thread>
#include <algorithm>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <btop_globs.h> #include <btop_globs.h>
#include <btop_config.h>
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 ------------------------------------------------------ //? ------------------------------------------------- NAMESPACES ------------------------------------------------------
@ -38,32 +42,28 @@ using std::string, std::to_string, std::round, std::vector, std::map, std::cin;
namespace Fx { namespace Fx {
//* Escape sequence start //* Escape sequence start
const string e = "\x1b["; const string e = "\x1b[";
//* Bold on //* Bold on
const string b = e + "1m"; const string b = e + "1m";
//* Bold off
const string ub = e + "22m";
//* Dark on //* Dark on
const string d = e + "2m"; const string d = e + "2m";
//* Dark off
const string ud = e + "22m";
//* Italic on //* Italic on
const string i = e + "3m"; const string i = e + "3m";
//* Italic off
const string ui = e + "23m";
//* Underline on //* Underline on
const string ul = e + "4m"; const string ul = e + "4m";
//* Underline off
const string uul = e + "24m";
//* Blink on //* Blink on
const string bl = e + "5m"; const string bl = e + "5m";
//* Blink off
const string ubl = e + "25m";
//* Strike / crossed-out on //* Strike / crossed-out on
const string s = e + "9m"; const string s = e + "9m";
//* Strike / crossed-out off
const string us = e + "29m";
//* Reset foreground/background color and text effects //* Reset foreground/background color and text effects
const string reset_base = e + "0m"; const string reset_base = e + "0m";
//* Reset text effects and restore default foregrund and background color < Changed by C_Theme //* Reset text effects and restore default foregrund and background color < Changed by C_Theme
string reset = reset_base; string reset = reset_base;
}; };
@ -72,16 +72,22 @@ namespace Fx {
namespace Mv { namespace Mv {
//* Move cursor to <line>, <column> //* Move cursor to <line>, <column>
inline string to(int line, int col){ return Fx::e + to_string(line) + ";" + to_string(col) + "f";} inline string to(int line, int col){ return Fx::e + to_string(line) + ";" + to_string(col) + "f";}
//* Move cursor right <x> columns //* Move cursor right <x> columns
inline string r(int x){ return Fx::e + to_string(x) + "C";} inline string r(int x){ return Fx::e + to_string(x) + "C";}
//* Move cursor left <x> columns //* Move cursor left <x> columns
inline string l(int x){ return Fx::e + to_string(x) + "D";} inline string l(int x){ return Fx::e + to_string(x) + "D";}
//* Move cursor up x lines //* Move cursor up x lines
inline string u(int x){ return Fx::e + to_string(x) + "A";} inline string u(int x){ return Fx::e + to_string(x) + "A";}
//* Move cursor down x lines //* Move cursor down x lines
inline string d(int x) { return Fx::e + to_string(x) + "B";} inline string d(int x) { return Fx::e + to_string(x) + "B";}
//* Save cursor position //* Save cursor position
const string save = Fx::e + "s"; const string save = Fx::e + "s";
//* Restore saved cursor postion //* Restore saved cursor postion
const string restore = Fx::e + "u"; 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); return ltrim(rtrim(str, t_str), t_str);
} }
//* Split <string> at <delim> <times> (0 for unlimited) times and return vector //* Split <string> at <delim> <time> number of times (0 for unlimited) and return vector
vector<string> ssplit(string str, string delim = " ", int times = 0){ vector<string> ssplit(string str, string delim = " ", int times = 0){
vector<string> out; vector<string> out;
if (str != "" && delim != ""){ if (!str.empty() && !delim.empty()){
size_t pos = 0; size_t pos = 0;
int x = 0; int x = 0;
string tmp; string tmp;
while ((pos = str.find(delim)) != string::npos){ while ((pos = str.find(delim)) != string::npos){
tmp = str.substr(0, pos); tmp = str.substr(0, pos);
if (tmp != delim && tmp != "") out.push_back(tmp); if (tmp != delim && !tmp.empty()) out.push_back(tmp);
str.erase(0, pos + delim.size()); str.erase(0, pos + delim.size());
if (times > 0 && ++x >= times) break; if (times > 0 && ++x >= times) break;
} }
@ -145,6 +151,42 @@ vector<string> ssplit(string str, string delim = " ", int times = 0){
return out; return out;
} }
//* Put current thread to sleep for <ms> milliseconds
void sleep_ms(unsigned ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
//* Left justify string <str> if <x> is greater than <str> length
string ljustify(string str, size_t x){
return str + string(max((int)(x - str.size()), 0), ' ');
}
//* Right justify string <str> if <x> is greater than <str> length
string rjustify(string str, size_t x){
return string(max((int)(x - str.size()), 0), ' ') + str;
}
//* Trim trailing characters if string length is greatear than <x>
string limit(string str, size_t x){
if (str.size() > x) str.resize(x);
return str;
}
//* Replace whitespaces " " with escape code for move right
string trans(string str){
size_t pos;
string newstr;
while ((pos = str.find(' ')) != string::npos){
newstr.append(str.substr(0, pos));
str.erase(0, pos);
pos = 1;
while (pos < str.size() && str.at(pos) == ' ') pos++;
newstr.append(Mv::r(pos));
str.erase(0, pos);
}
return (newstr.empty()) ? str : newstr;
}
//? --------------------------------------------------- CLASSES ----------------------------------------------------- //? --------------------------------------------------- CLASSES -----------------------------------------------------
//* Collection of escape codes and functions for terminal manipulation //* Collection of escape codes and functions for terminal manipulation
@ -152,9 +194,9 @@ class C_Term {
struct termios initial_settings; struct termios initial_settings;
public: public:
bool initialized = false; bool initialized = false;
int width = 0;
int height = 0;
bool resized = false; bool resized = false;
unsigned width = 0;
unsigned height = 0;
//* Hide terminal cursor //* Hide terminal cursor
const string hide_cursor = Fx::e + "?25l"; const string hide_cursor = Fx::e + "?25l";
@ -209,8 +251,8 @@ public:
struct winsize w; struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
resized = (width != w.ws_col || height != w.ws_row) ? true : false; resized = (width != w.ws_col || height != w.ws_row) ? true : false;
width = w.ws_col; State::width = width = w.ws_col;
height = w.ws_row; State::height = height = w.ws_row;
return resized; return resized;
} }