mirror of
https://github.com/aristocratos/btop.git
synced 2024-05-18 19:33:03 +12:00
Refactoring and new functions
This commit is contained in:
parent
3b3a93935d
commit
224ade22fa
61
btop.cpp
61
btop.cpp
|
@ -16,8 +16,6 @@ indent = tab
|
|||
tab-size = 4
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
@ -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<string, string> dict = {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -21,8 +21,6 @@ tab-size = 4
|
|||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <iostream>
|
||||
|
||||
#include <btop_globs.h>
|
||||
|
@ -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 <timeout> 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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -23,14 +23,18 @@ tab-size = 4
|
|||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <algorithm>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
#include <sys/ioctl.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 ------------------------------------------------------
|
||||
|
||||
|
@ -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 <line>, <column>
|
||||
inline string to(int line, int col){ return Fx::e + to_string(line) + ";" + to_string(col) + "f";}
|
||||
|
||||
//* Move cursor right <x> columns
|
||||
inline string r(int x){ return Fx::e + to_string(x) + "C";}
|
||||
|
||||
//* Move cursor left <x> 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 <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> out;
|
||||
if (str != "" && delim != ""){
|
||||
if (!str.empty() && !delim.empty()){
|
||||
size_t pos = 0;
|
||||
int x = 0;
|
||||
string tmp;
|
||||
while ((pos = str.find(delim)) != string::npos){
|
||||
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());
|
||||
if (times > 0 && ++x >= times) break;
|
||||
}
|
||||
|
@ -145,6 +151,42 @@ vector<string> ssplit(string str, string delim = " ", int times = 0){
|
|||
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 -----------------------------------------------------
|
||||
|
||||
//* Collection of escape codes and functions for terminal manipulation
|
||||
|
@ -152,9 +194,9 @@ class C_Term {
|
|||
struct termios initial_settings;
|
||||
public:
|
||||
bool initialized = false;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
bool resized = false;
|
||||
unsigned width = 0;
|
||||
unsigned height = 0;
|
||||
|
||||
//* Hide terminal cursor
|
||||
const string hide_cursor = Fx::e + "?25l";
|
||||
|
@ -209,8 +251,8 @@ public:
|
|||
struct winsize w;
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||
resized = (width != w.ws_col || height != w.ws_row) ? true : false;
|
||||
width = w.ws_col;
|
||||
height = w.ws_row;
|
||||
State::width = width = w.ws_col;
|
||||
State::height = height = w.ws_row;
|
||||
return resized;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue