From 3e92a5e89ab8979e6bbb633a1d61854a7cd15e34 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Sat, 19 Jun 2021 22:48:31 +0200 Subject: More file reorganizations --- src/btop.cpp | 19 ++-- src/btop_config.cpp | 4 +- src/btop_config.h | 67 -------------- src/btop_config.hpp | 67 ++++++++++++++ src/btop_draw.cpp | 8 +- src/btop_draw.h | 105 ---------------------- src/btop_draw.hpp | 102 ++++++++++++++++++++++ src/btop_input.cpp | 114 ++++++++++++++++++++++++ src/btop_input.h | 115 ------------------------ src/btop_input.hpp | 47 ++++++++++ src/btop_linux.cpp | 92 +++++++++++--------- src/btop_linux.h | 65 -------------- src/btop_linux.hpp | 64 ++++++++++++++ src/btop_menu.h | 74 ---------------- src/btop_menu.hpp | 74 ++++++++++++++++ src/btop_theme.cpp | 6 +- src/btop_theme.h | 64 -------------- src/btop_theme.hpp | 64 ++++++++++++++ src/btop_tools.cpp | 11 ++- src/btop_tools.h | 245 ---------------------------------------------------- src/btop_tools.hpp | 245 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 21 files changed, 850 insertions(+), 802 deletions(-) delete mode 100644 src/btop_config.h create mode 100644 src/btop_config.hpp delete mode 100644 src/btop_draw.h create mode 100644 src/btop_draw.hpp create mode 100644 src/btop_input.cpp delete mode 100644 src/btop_input.h create mode 100644 src/btop_input.hpp delete mode 100644 src/btop_linux.h create mode 100644 src/btop_linux.hpp delete mode 100644 src/btop_menu.h create mode 100644 src/btop_menu.hpp delete mode 100644 src/btop_theme.h create mode 100644 src/btop_theme.hpp delete mode 100644 src/btop_tools.h create mode 100644 src/btop_tools.hpp diff --git a/src/btop.cpp b/src/btop.cpp index 8ed0a60..e2651c9 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -31,17 +31,18 @@ tab-size = 4 #include #include #include +#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #if defined(__linux__) #define LINUX - #include + #include #elif defined(__unix__) || !defined(__APPLE__) && defined(__MACH__) #include #if defined(BSD) @@ -572,9 +573,9 @@ int main(int argc, char **argv){ lc = 0; ostring = Mv::u(2) + Mv::l(Term::width) + Mv::r(12) - + trans("Filter: " + filter + (filtering ? Fx::bl + "█" + Fx::reset : "")) + Mv::l(Term::width) + + trans("Filter: " + filter + (filtering ? Fx::bl + "█" + Fx::reset : " ")) + trans(rjust("Per core: " + (Config::getB("proc_per_core") ? "On "s : "Off"s) + " Sorting: " - + string(Config::getS("proc_sorting")), Term::width - 3)) + + string(Config::getS("proc_sorting")), Term::width - 23 - ulen(filter))) + Mv::restore; for (auto& p : plist){ diff --git a/src/btop_config.cpp b/src/btop_config.cpp index c250a78..45fce43 100644 --- a/src/btop_config.cpp +++ b/src/btop_config.cpp @@ -22,8 +22,8 @@ tab-size = 4 #include #include -#include -#include +#include +#include using robin_hood::unordered_flat_map, std::map, std::array, std::atomic; namespace fs = std::filesystem; diff --git a/src/btop_config.h b/src/btop_config.h deleted file mode 100644 index 98e239c..0000000 --- a/src/btop_config.h +++ /dev/null @@ -1,67 +0,0 @@ -/* 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 -*/ - -#pragma once - -#include -#include -#include - -using std::string, std::vector; - -//* Functions and variables for reading and writing the btop config file -namespace Config { - - extern std::filesystem::path conf_dir; - extern std::filesystem::path conf_file; - - extern vector valid_graph_symbols; - - //* Return bool config value - const bool& getB(string name); - - //* Return integer config value - const int& getI(string name); - - //* Return string config value - const string& getS(string name); - - //* Set config value to bool - void set(string name, bool value); - - //* Set config value to int - void set(string name, int value); - - //* Set config value to string - void set(string name, string value); - - //* Flip config bool - void flip(string name); - - //* Wait if locked then lock config and cache changes until unlock - void lock(); - - //* Unlock config and write any cached values to config - void unlock(); - - //* Load the config file from disk - void load(std::filesystem::path conf_file, vector& load_errors); - - //* Write the config file to disk - void write(); -} \ No newline at end of file diff --git a/src/btop_config.hpp b/src/btop_config.hpp new file mode 100644 index 0000000..98e239c --- /dev/null +++ b/src/btop_config.hpp @@ -0,0 +1,67 @@ +/* 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 +*/ + +#pragma once + +#include +#include +#include + +using std::string, std::vector; + +//* Functions and variables for reading and writing the btop config file +namespace Config { + + extern std::filesystem::path conf_dir; + extern std::filesystem::path conf_file; + + extern vector valid_graph_symbols; + + //* Return bool config value + const bool& getB(string name); + + //* Return integer config value + const int& getI(string name); + + //* Return string config value + const string& getS(string name); + + //* Set config value to bool + void set(string name, bool value); + + //* Set config value to int + void set(string name, int value); + + //* Set config value to string + void set(string name, string value); + + //* Flip config bool + void flip(string name); + + //* Wait if locked then lock config and cache changes until unlock + void lock(); + + //* Unlock config and write any cached values to config + void unlock(); + + //* Load the config file from disk + void load(std::filesystem::path conf_file, vector& load_errors); + + //* Write the config file to disk + void write(); +} \ No newline at end of file diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index f059e0f..0f11235 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -22,10 +22,10 @@ tab-size = 4 #include #include -#include -#include -#include -#include +#include +#include +#include +#include using robin_hood::unordered_flat_map, std::round, std::views::iota, std::string_literals::operator""s, std::clamp, std::array, std::floor; diff --git a/src/btop_draw.h b/src/btop_draw.h deleted file mode 100644 index 3e21a6d..0000000 --- a/src/btop_draw.h +++ /dev/null @@ -1,105 +0,0 @@ -/* 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 -*/ - -#pragma once - -#include -#include -#include - -#include -#include - -using std::string, std::vector, robin_hood::unordered_flat_map; - -namespace Symbols { - extern const string h_line; - extern const string v_line; - extern const string left_up; - extern const string right_up; - extern const string left_down; - extern const string right_down; - extern const string title_left; - extern const string title_right; - extern const string div_up; - extern const string div_down; -} - -namespace Draw { - - struct BoxConf { - uint x=0, y=0; - uint width=0, height=0; - string line_color = "", title = "", title2 = ""; - bool fill = true; - uint num=0; - }; - - //* Create a box using values from a BoxConf struct and return as a string - string createBox(BoxConf c); - - //* Class holding a percentage meter - class Meter { - string color_gradient; - int width = 0; - bool invert = false; - vector cache; - public: - //* Set meter options - void operator()(int width, string color_gradient, bool invert = false); - - //* Return a string representation of the meter with given value - string operator()(int value); - }; - - //* Class holding a graph - class Graph { - string out, color_gradient, symbol = "default"; - int width = 0, height = 0; - long long last = 0, max_value = 0, offset = 0; - bool current = true, no_zero = false, invert = false, tty_mode = false; - unordered_flat_map> graphs = { {true, {}}, {false, {}}}; - - //* Create two representations of the graph to switch between to represent two values for each braille character - void _create(const vector& data, int data_offset); - - public: - //* Set graph options and initialize with data - void operator()(int width, int height, string color_gradient, const vector& data, string symbol = "default", bool invert = false, bool no_zero = false, long long max_value = 0, long long offset = 0); - - //* Add last value from back of and return string representation of graph - string& operator()(const vector& data, bool data_same = false); - - //* Return string representation of graph - string& operator()(); - }; - -} - -namespace Box { - - - - -} - -namespace Proc { - - // Draw::BoxConf box; - -} \ No newline at end of file diff --git a/src/btop_draw.hpp b/src/btop_draw.hpp new file mode 100644 index 0000000..de3d8db --- /dev/null +++ b/src/btop_draw.hpp @@ -0,0 +1,102 @@ +/* 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 +*/ + +#pragma once + +#include +#include +#include + +using std::string, std::vector, robin_hood::unordered_flat_map; + +namespace Symbols { + extern const string h_line; + extern const string v_line; + extern const string left_up; + extern const string right_up; + extern const string left_down; + extern const string right_down; + extern const string title_left; + extern const string title_right; + extern const string div_up; + extern const string div_down; +} + +namespace Draw { + + struct BoxConf { + uint x=0, y=0; + uint width=0, height=0; + string line_color = "", title = "", title2 = ""; + bool fill = true; + uint num=0; + }; + + //* Create a box using values from a BoxConf struct and return as a string + string createBox(BoxConf c); + + //* Class holding a percentage meter + class Meter { + string color_gradient; + int width = 0; + bool invert = false; + vector cache; + public: + //* Set meter options + void operator()(int width, string color_gradient, bool invert = false); + + //* Return a string representation of the meter with given value + string operator()(int value); + }; + + //* Class holding a graph + class Graph { + string out, color_gradient, symbol = "default"; + int width = 0, height = 0; + long long last = 0, max_value = 0, offset = 0; + bool current = true, no_zero = false, invert = false, tty_mode = false; + unordered_flat_map> graphs = { {true, {}}, {false, {}}}; + + //* Create two representations of the graph to switch between to represent two values for each braille character + void _create(const vector& data, int data_offset); + + public: + //* Set graph options and initialize with data + void operator()(int width, int height, string color_gradient, const vector& data, string symbol = "default", bool invert = false, bool no_zero = false, long long max_value = 0, long long offset = 0); + + //* Add last value from back of and return string representation of graph + string& operator()(const vector& data, bool data_same = false); + + //* Return string representation of graph + string& operator()(); + }; + +} + +namespace Box { + + + + +} + +namespace Proc { + + // Draw::BoxConf box; + +} \ No newline at end of file diff --git a/src/btop_input.cpp b/src/btop_input.cpp new file mode 100644 index 0000000..eb13516 --- /dev/null +++ b/src/btop_input.cpp @@ -0,0 +1,114 @@ +/* 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 +#include +#include + +#include +#include + +using std::string, robin_hood::unordered_flat_map, std::cin; +using namespace Tools; + +/* The input functions relies on the following std::cin options being set: + cin.sync_with_stdio(false); + cin.tie(NULL); + These will automatically be set when running Term::init() from btop_tools.cpp +*/ + +//* Functions and variables for handling keyboard and mouse input +namespace Input { + namespace { + //* Map for translating key codes to readable values + const unordered_flat_map Key_escapes = { + {"\033", "escape"}, + {"\n", "enter"}, + {" ", "space"}, + {"\x7f", "backspace"}, + {"\x08", "backspace"}, + {"[A", "up"}, + {"OA", "up"}, + {"[B", "down"}, + {"OB", "down"}, + {"[D", "left"}, + {"OD", "left"}, + {"[C", "right"}, + {"OC", "right"}, + {"[2~", "insert"}, + {"[3~", "delete"}, + {"[H", "home"}, + {"[F", "end"}, + {"[5~", "page_up"}, + {"[6~", "page_down"}, + {"\t", "tab"}, + {"[Z", "shift_tab"}, + {"OP", "f1"}, + {"OQ", "f2"}, + {"OR", "f3"}, + {"OS", "f4"}, + {"[15~", "f5"}, + {"[17~", "f6"}, + {"[18~", "f7"}, + {"[19~", "f8"}, + {"[20~", "f9"}, + {"[21~", "f10"}, + {"[23~", "f11"}, + {"[24~", "f12"} + }; + } + + //* Last entered key + string last = ""; + + //* Poll keyboard & mouse input for ms and return input availabilty as a bool + bool poll(int timeout){ + if (timeout < 1) return cin.rdbuf()->in_avail() > 0; + while (timeout > 0) { + if (cin.rdbuf()->in_avail() > 0) return true; + sleep_ms(timeout < 10 ? timeout : 10); + timeout -= 10; + } + return false; + } + + //* Get a key or mouse action from input + string get(bool clear){ + string key; + while (cin.rdbuf()->in_avail() > 0 && key.size() < 100) key += cin.get(); + if (!clear && !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 = ""; + last = key; + } + return key; + } + + //* Wait until input is available + void wait(bool clear){ + while (cin.rdbuf()->in_avail() < 1) sleep_ms(10); + if (clear) get(clear); + } + + //* Clears last entered key + void clear(){ + last.clear(); + } + +} \ No newline at end of file diff --git a/src/btop_input.h b/src/btop_input.h deleted file mode 100644 index 5d694e8..0000000 --- a/src/btop_input.h +++ /dev/null @@ -1,115 +0,0 @@ -/* 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 -*/ - -#pragma once - -#include -#include -#include - -#include - -using std::string, robin_hood::unordered_flat_map, std::cin; -using namespace Tools; - -/* The input functions relies on the following std::cin options being set: - cin.sync_with_stdio(false); - cin.tie(NULL); - These will automatically be set when running Term::init() from btop_tools.cpp -*/ - -//* Functions and variables for handling keyboard and mouse input -namespace Input { - namespace { - //* Map for translating key codes to readable values - const unordered_flat_map Key_escapes = { - {"\033", "escape"}, - {"\n", "enter"}, - {" ", "space"}, - {"\x7f", "backspace"}, - {"\x08", "backspace"}, - {"[A", "up"}, - {"OA", "up"}, - {"[B", "down"}, - {"OB", "down"}, - {"[D", "left"}, - {"OD", "left"}, - {"[C", "right"}, - {"OC", "right"}, - {"[2~", "insert"}, - {"[3~", "delete"}, - {"[H", "home"}, - {"[F", "end"}, - {"[5~", "page_up"}, - {"[6~", "page_down"}, - {"\t", "tab"}, - {"[Z", "shift_tab"}, - {"OP", "f1"}, - {"OQ", "f2"}, - {"OR", "f3"}, - {"OS", "f4"}, - {"[15~", "f5"}, - {"[17~", "f6"}, - {"[18~", "f7"}, - {"[19~", "f8"}, - {"[20~", "f9"}, - {"[21~", "f10"}, - {"[23~", "f11"}, - {"[24~", "f12"} - }; - } - - //* Last entered key - string last = ""; - - //* Poll keyboard & mouse input for ms and return input availabilty as a bool - bool poll(int timeout=0){ - if (timeout < 1) return cin.rdbuf()->in_avail() > 0; - while (timeout > 0) { - if (cin.rdbuf()->in_avail() > 0) return true; - sleep_ms(timeout < 10 ? timeout : 10); - timeout -= 10; - } - return false; - } - - //* Get a key or mouse action from input - string get(bool clear = false){ - string key; - while (cin.rdbuf()->in_avail() > 0 && key.size() < 100) key += cin.get(); - if (!clear && !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 = ""; - last = key; - } - return key; - } - - //* Wait until input is available - void wait(bool clear=false){ - while (cin.rdbuf()->in_avail() < 1) sleep_ms(10); - if (clear) get(clear); - } - - //* Clears last entered key - void clear(){ - last.clear(); - } - -} \ No newline at end of file diff --git a/src/btop_input.hpp b/src/btop_input.hpp new file mode 100644 index 0000000..901183b --- /dev/null +++ b/src/btop_input.hpp @@ -0,0 +1,47 @@ +/* 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 +*/ + +#pragma once + +#include + +/* The input functions relies on the following std::cin options being set: + cin.sync_with_stdio(false); + cin.tie(NULL); + These will automatically be set when running Term::init() from btop_tools.cpp +*/ + +//* Functions and variables for handling keyboard and mouse input +namespace Input { + + //* Last entered key + extern std::string last; + + //* Poll keyboard & mouse input for ms and return input availabilty as a bool + bool poll(int timeout=0); + + //* Get a key or mouse action from input + std::string get(bool clear = false); + + //* Wait until input is available + void wait(bool clear=false); + + //* Clears last entered key + void clear(); + +} \ No newline at end of file diff --git a/src/btop_linux.cpp b/src/btop_linux.cpp index 0541752..eb4d6d8 100644 --- a/src/btop_linux.cpp +++ b/src/btop_linux.cpp @@ -32,9 +32,9 @@ tab-size = 4 #include -#include -#include -#include +#include +#include +#include @@ -98,7 +98,7 @@ namespace Proc { out_procs.push_back(cur_proc); int children = 0; for (auto& p : in_procs) { - if (p.ppid == (int)cur_proc.pid) { + if (p.ppid == cur_proc.pid) { children++; if (collapsed) { out_procs.back().cpu_p += p.cpu_p; @@ -254,46 +254,52 @@ namespace Proc { s_pos = instr.find_last_of(')') + 2; if (s_pos == string::npos) continue; - do { - c_pos = instr.find(' ', s_pos); - if (c_pos == string::npos) break; - - switch (s_count) { - case 0: { //? Process state - new_proc.state = instr[s_pos]; - break; - } - case 1: { //? Process parent pid - new_proc.ppid = stoi(instr.substr(s_pos, c_pos - s_pos)); - break; - } - case 11: { //? Process utime - cpu_t = stoull(instr.substr(s_pos, c_pos - s_pos)); - break; + try { + do { + c_pos = instr.find(' ', s_pos); + if (c_pos == string::npos) break; + + switch (s_count) { + case 0: { //? Process state + new_proc.state = instr[s_pos]; + break; + } + case 1: { //? Process parent pid + new_proc.ppid = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 11: { //? Process utime + cpu_t = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 12: { //? Process stime + cpu_t += stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 16: { //? Process nice value + new_proc.p_nice = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 17: { //? Process number of threads + new_proc.threads = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 19: { //? Cache cpu seconds + if (new_cache) cache[new_proc.pid].cpu_s = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } + case 36: { //? CPU number last executed on + new_proc.cpu_n = stoull(instr.substr(s_pos, c_pos - s_pos)); + break; + } } - case 12: { //? Process stime - cpu_t += stoull(instr.substr(s_pos, c_pos - s_pos)); - break; - } - case 16: { //? Process nice value - new_proc.p_nice = stoi(instr.substr(s_pos, c_pos - s_pos)); - break; - } - case 17: { //? Process number of threads - new_proc.threads = stoul(instr.substr(s_pos, c_pos - s_pos)); - break; - } - case 19: { //? Cache cpu seconds - if (new_cache) cache[new_proc.pid].cpu_s = stoull(instr.substr(s_pos, c_pos - s_pos)); - break; - } - case 36: { //? CPU number last executed on - new_proc.cpu_n = stoi(instr.substr(s_pos, c_pos - s_pos)); - break; - } - } - s_pos = c_pos + 1; - } while (s_count++ < 36); + + s_pos = c_pos + 1; + } while (s_count++ < 36); + } + catch (std::out_of_range&) { + continue; + } if (s_count < 19) continue; diff --git a/src/btop_linux.h b/src/btop_linux.h deleted file mode 100644 index 64ea044..0000000 --- a/src/btop_linux.h +++ /dev/null @@ -1,65 +0,0 @@ -/* 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 -*/ - -#pragma once - -#include -#include -#include -#include - -using std::string, std::vector, std::atomic; - -namespace Global { - extern int coreCount; -} - -namespace Tools { - double system_uptime(); -} - -namespace Proc { - - extern std::filesystem::path proc_path; - extern size_t numpids; - extern atomic stop; - extern atomic collecting; - extern vector sort_vector; - - //* Container for process information - struct proc_info { - uint pid; - string name = "", cmd = ""; - size_t threads = 0; - string user = ""; - uint64_t mem = 0; - double cpu_p = 0.0, cpu_c = 0.0; - char state = '0'; - int cpu_n = 0, p_nice = 0; - int ppid = -1; - string prefix = ""; - }; - - extern vector current_procs; - - //* Collects and sorts process information from /proc, saves to and returns reference to Proc::current_procs; - vector& collect(); - - //* Initialize needed variables for collect - void init(); -} \ No newline at end of file diff --git a/src/btop_linux.hpp b/src/btop_linux.hpp new file mode 100644 index 0000000..a871e4e --- /dev/null +++ b/src/btop_linux.hpp @@ -0,0 +1,64 @@ +/* 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 +*/ + +#pragma once + +#include +#include +#include +#include + +using std::string, std::vector, std::atomic; + +namespace Global { + extern int coreCount; +} + +namespace Tools { + double system_uptime(); +} + +namespace Proc { + + extern std::filesystem::path proc_path; + extern size_t numpids; + extern atomic stop; + extern atomic collecting; + extern vector sort_vector; + + //* Container for process information + struct proc_info { + uint64_t pid; + string name = "", cmd = ""; + size_t threads = 0; + string user = ""; + uint64_t mem = 0; + double cpu_p = 0.0, cpu_c = 0.0; + char state = '0'; + uint64_t cpu_n = 0, p_nice = 0, ppid = 0; + string prefix = ""; + }; + + extern vector current_procs; + + //* Collects and sorts process information from /proc, saves to and returns reference to Proc::current_procs; + vector& collect(); + + //* Initialize needed variables for collect + void init(); +} \ No newline at end of file diff --git a/src/btop_menu.h b/src/btop_menu.h deleted file mode 100644 index ce5479f..0000000 --- a/src/btop_menu.h +++ /dev/null @@ -1,74 +0,0 @@ -/* 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 -*/ - -#pragma once - -#include -#include -#include -#include -#include - -using std::string, std::vector, std::unordered_map, std::array, std::atomic, robin_hood::unordered_flat_map; - -namespace Menu { - - atomic active(false); - - const unordered_flat_map>> Menus = { - { "options", { - { "normal", { - "┌─┐┌─┐┌┬┐┬┌─┐┌┐┌┌─┐", - "│ │├─┘ │ ││ ││││└─┐", - "└─┘┴ ┴ ┴└─┘┘└┘└─┘" - } }, - { "selected", { - "╔═╗╔═╗╔╦╗╦╔═╗╔╗╔╔═╗", - "║ ║╠═╝ ║ ║║ ║║║║╚═╗", - "╚═╝╩ ╩ ╩╚═╝╝╚╝╚═╝" - } } - } }, - { "help", { - { "normal", { - "┬ ┬┌─┐┬ ┌─┐", - "├─┤├┤ │ ├─┘", - "┴ ┴└─┘┴─┘┴ " - } }, - { "selected", { - "╦ ╦╔═╗╦ ╔═╗", - "╠═╣║╣ ║ ╠═╝", - "╩ ╩╚═╝╩═╝╩ " - } } - } }, - { "quit", { - { "normal", { - "┌─┐ ┬ ┬ ┬┌┬┐", - "│─┼┐│ │ │ │ ", - "└─┘└└─┘ ┴ ┴ " - } }, - { "selected", { - "╔═╗ ╦ ╦ ╦╔╦╗ ", - "║═╬╗║ ║ ║ ║ ", - "╚═╝╚╚═╝ ╩ ╩ " - } } - } } - }; - - - -} diff --git a/src/btop_menu.hpp b/src/btop_menu.hpp new file mode 100644 index 0000000..ce5479f --- /dev/null +++ b/src/btop_menu.hpp @@ -0,0 +1,74 @@ +/* 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 +*/ + +#pragma once + +#include +#include +#include +#include +#include + +using std::string, std::vector, std::unordered_map, std::array, std::atomic, robin_hood::unordered_flat_map; + +namespace Menu { + + atomic active(false); + + const unordered_flat_map>> Menus = { + { "options", { + { "normal", { + "┌─┐┌─┐┌┬┐┬┌─┐┌┐┌┌─┐", + "│ │├─┘ │ ││ ││││└─┐", + "└─┘┴ ┴ ┴└─┘┘└┘└─┘" + } }, + { "selected", { + "╔═╗╔═╗╔╦╗╦╔═╗╔╗╔╔═╗", + "║ ║╠═╝ ║ ║║ ║║║║╚═╗", + "╚═╝╩ ╩ ╩╚═╝╝╚╝╚═╝" + } } + } }, + { "help", { + { "normal", { + "┬ ┬┌─┐┬ ┌─┐", + "├─┤├┤ │ ├─┘", + "┴ ┴└─┘┴─┘┴ " + } }, + { "selected", { + "╦ ╦╔═╗╦ ╔═╗", + "╠═╣║╣ ║ ╠═╝", + "╩ ╩╚═╝╩═╝╩ " + } } + } }, + { "quit", { + { "normal", { + "┌─┐ ┬ ┬ ┬┌┬┐", + "│─┼┐│ │ │ │ ", + "└─┘└└─┘ ┴ ┴ " + } }, + { "selected", { + "╔═╗ ╦ ╦ ╦╔╦╗ ", + "║═╬╗║ ║ ║ ║ ", + "╚═╝╚╚═╝ ╩ ╩ " + } } + } } + }; + + + +} diff --git a/src/btop_theme.cpp b/src/btop_theme.cpp index 9792c87..59ff663 100644 --- a/src/btop_theme.cpp +++ b/src/btop_theme.cpp @@ -23,9 +23,9 @@ tab-size = 4 #include #include -#include -#include -#include +#include +#include +#include using std::round, std::vector, robin_hood::unordered_flat_map, std::stoi, std::views::iota, std::array, std::clamp, std::max, std::min, std::ceil, std::to_string; diff --git a/src/btop_theme.h b/src/btop_theme.h deleted file mode 100644 index 699d416..0000000 --- a/src/btop_theme.h +++ /dev/null @@ -1,64 +0,0 @@ -/* 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 -*/ - -#pragma once - -#include -#include -#include - -using std::string; - -namespace Theme { - extern std::filesystem::path theme_dir; - extern std::filesystem::path user_theme_dir; - - extern const robin_hood::unordered_flat_map Default_theme; - - //* Generate escape sequence for 24-bit or 256 color and return as a string - //* Args hexa: ["#000000"-"#ffffff"] for color, ["#00"-"#ff"] for greyscale - //* t_to_256: [true|false] convert 24bit value to 256 color value - //* depth: ["fg"|"bg"] for either a foreground color or a background color - string hex_to_color(string hexa, bool t_to_256=false, string depth="fg"); - - //* Generate escape sequence for 24-bit or 256 color and return as a string - //* Args r: [0-255], g: [0-255], b: [0-255] - //* t_to_256: [true|false] convert 24bit value to 256 color value - //* depth: ["fg"|"bg"] for either a foreground color or a background color - string dec_to_color(int r, int g, int b, bool t_to_256=false, string depth="fg"); - - //* Return an array of red, green and blue, 0-255 values for a 24-bit color escape string - std::array esc_to_rgb(string c_string); - - //* Set current theme using map - void set(robin_hood::unordered_flat_map source); - - //* Return escape code for color - const string& c(string name); - - //* Return array of escape codes for color gradient - const std::array& g(string name); - - //* Return array of red, green and blue in decimal for color - const std::array& dec(string name); - - //? Testing - robin_hood::unordered_flat_map& test_colors(); - robin_hood::unordered_flat_map>& test_gradients(); - -} \ No newline at end of file diff --git a/src/btop_theme.hpp b/src/btop_theme.hpp new file mode 100644 index 0000000..699d416 --- /dev/null +++ b/src/btop_theme.hpp @@ -0,0 +1,64 @@ +/* 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 +*/ + +#pragma once + +#include +#include +#include + +using std::string; + +namespace Theme { + extern std::filesystem::path theme_dir; + extern std::filesystem::path user_theme_dir; + + extern const robin_hood::unordered_flat_map Default_theme; + + //* Generate escape sequence for 24-bit or 256 color and return as a string + //* Args hexa: ["#000000"-"#ffffff"] for color, ["#00"-"#ff"] for greyscale + //* t_to_256: [true|false] convert 24bit value to 256 color value + //* depth: ["fg"|"bg"] for either a foreground color or a background color + string hex_to_color(string hexa, bool t_to_256=false, string depth="fg"); + + //* Generate escape sequence for 24-bit or 256 color and return as a string + //* Args r: [0-255], g: [0-255], b: [0-255] + //* t_to_256: [true|false] convert 24bit value to 256 color value + //* depth: ["fg"|"bg"] for either a foreground color or a background color + string dec_to_color(int r, int g, int b, bool t_to_256=false, string depth="fg"); + + //* Return an array of red, green and blue, 0-255 values for a 24-bit color escape string + std::array esc_to_rgb(string c_string); + + //* Set current theme using map + void set(robin_hood::unordered_flat_map source); + + //* Return escape code for color + const string& c(string name); + + //* Return array of escape codes for color gradient + const std::array& g(string name); + + //* Return array of red, green and blue in decimal for color + const std::array& dec(string name); + + //? Testing + robin_hood::unordered_flat_map& test_colors(); + robin_hood::unordered_flat_map>& test_gradients(); + +} \ No newline at end of file diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index 85e6cbc..004209c 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -31,7 +31,7 @@ tab-size = 4 #include #include -#include +#include using std::string_view, std::array, std::regex, std::max, std::to_string, std::cin, std::atomic, robin_hood::unordered_flat_map; @@ -278,11 +278,10 @@ namespace Tools { newstr.reserve(str.size()); while ((pos = oldstr.find(' ')) != string::npos){ newstr.append(oldstr.substr(0, pos)); - oldstr.remove_prefix(pos+1); - pos = 1; - while (pos < oldstr.size() && oldstr.at(pos) == ' ') pos++; - newstr.append(Mv::r(pos)); - oldstr.remove_suffix(pos-1); + size_t x = 0; + while (pos + x < oldstr.size() && oldstr.at(pos + x) == ' ') x++; + newstr.append(Mv::r(x)); + oldstr.remove_prefix(pos + x); } return (newstr.empty()) ? str : newstr + (string)oldstr; } diff --git a/src/btop_tools.h b/src/btop_tools.h deleted file mode 100644 index c9fc091..0000000 --- a/src/btop_tools.h +++ /dev/null @@ -1,245 +0,0 @@ -/* 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 -*/ - -#pragma once - -#include -#include -#include -#include -#include -#include - - -using std::string, std::vector; - - -//? ------------------------------------------------- NAMESPACES ------------------------------------------------------ - -namespace Global { - extern string Version; -} - -//* Collection of escape codes for text style and formatting -namespace Fx { - extern const string e; //* Escape sequence start - extern const string b; //* Bold on/off - extern const string ub; //* Bold off - extern const string d; //* Dark on - extern const string ud; //* Dark off - extern const string i; //* Italic on - extern const string ui; //* Italic off - extern const string ul; //* Underline on - extern const string uul; //* Underline off - extern const string bl; //* Blink on - extern const string ubl; //* Blink off - extern const string s; //* Strike/crossed-out on - extern const string us; //* Strike/crossed-out on/off - - //* Reset foreground/background color and text effects - extern const string reset_base; - - //* Reset text effects and restore theme foregrund and background color - extern string reset; - - //* Regex for matching color, style and curse move escape sequences - extern const std::regex escape_regex; - - //* Regex for matching only color and style escape sequences - extern const std::regex color_regex; - - //* Return a string with all colors and text styling removed - string uncolor(string& s); -} - -//* Collection of escape codes and functions for cursor manipulation -namespace Mv { - //* Move cursor to , - const string to(int line, int col); - - //* Move cursor right columns - const string r(int x); - - //* Move cursor left columns - const string l(int x); - - //* Move cursor up x lines - const string u(int x); - - //* Move cursor down x lines - const string d(int x); - - //* Save cursor position - extern const string save; - - //* Restore saved cursor postion - extern const string restore; -} - -//* Collection of escape codes and functions for terminal manipulation -namespace Term { - extern bool initialized; - extern bool resized; - extern uint width; - extern uint height; - extern string fg, bg, current_tty; - - //* Hide terminal cursor - extern const string hide_cursor; - - //* Show terminal cursor - extern const string show_cursor; - - //* Switch to alternate screen - extern const string alt_screen; - - //* Switch to normal screen - extern const string normal_screen; - - //* Clear screen and set cursor to position 0,0 - extern const string clear; - - //* Clear from cursor to end of screen - extern const string clear_end; - - //* Clear from cursor to beginning of screen - extern const string clear_begin; - - //* Enable reporting of mouse position on click and release - extern const string mouse_on; - - //* Disable mouse reporting - extern const string mouse_off; - - //* Enable reporting of mouse position at any movement - extern const string mouse_direct_on; - - //* Disable direct mouse reporting - extern const string mouse_direct_off; - - //* Refresh variables holding current terminal width and height and return true if resized - bool refresh(); - - //* Check for a valid tty, save terminal options and set new options - bool init(); - - //* Restore terminal options - void restore(); -} - -//? --------------------------------------------------- FUNCTIONS ----------------------------------------------------- - -namespace Tools { - const auto SSmax = std::numeric_limits::max(); - - //* Return number of UTF8 characters in a string with option to disregard escape sequences - size_t ulen(string str, const bool escape=false); - - //* Resize a string consisting of UTF8 characters (only reduces size) - string uresize(string str, const size_t len); - - //* Check if vector contains value - template - bool v_contains(vector& vec, T find_val) { - return std::ranges::find(vec, find_val) != vec.end(); - } - - //* Return index of from vector , returns size of if is not present - template - size_t v_index(vector& vec, T find_val) { - return std::ranges::distance(vec.begin(), std::ranges::find(vec, find_val)); - } - - //* Return current time since epoch in seconds - uint64_t time_s(); - - //* Return current time since epoch in milliseconds - uint64_t time_ms(); - - //* Return current time since epoch in microseconds - uint64_t time_micros(); - - //* Check if a string is a valid bool value - bool isbool(string& str); - - //* Convert string to bool, returning any value not equal to "true" or "True" as false - bool stobool(string& str); - - //* Check if a string is a valid integer value - bool isint(string& str); - - //* Left-trim from and return new string - string ltrim(const string& str, const string t_str = " "); - - //* Right-trim from and return new string - string rtrim(const string& str, const string t_str = " "); - - //* Left-right-trim from and return new string - string trim(const string& str, const string t_str = " "); - - //* Split at all occurrences of and return as vector of strings - vector ssplit(const string& str, const char delim = ' '); - - //* Put current thread to sleep for milliseconds - void sleep_ms(const uint& ms); - - //* Left justify string if is greater than length, limit return size to by default - string ljust(string str, const size_t x, bool utf=false, bool escape=false, bool lim=true); - - //* Right justify string if is greater than length, limit return size to by default - string rjust(string str, const size_t x, bool utf=false, bool escape=false, bool lim=true); - - //* Replace whitespaces " " with escape code for move right - string trans(const string& str); - - //* Convert seconds to format "Xd HH:MM:SS" and return string - string sec_to_dhms(uint sec); - - //* Scales up in steps of 1024 to highest possible unit and returns string with unit suffixed - //* bit=True or defaults to bytes - //* start=int to set 1024 multiplier starting unit - //* short=True always returns 0 decimals and shortens unit to 1 character - string floating_humanizer(uint64_t value, bool shorten=false, uint start=0, bool bit=false, bool per_second=false); - - //* Add std::string operator "*" : Repeat string number of times - std::string operator*(string str, size_t n); - - //* Return current time in format - string strf_time(string strf); - - //* Waits for to not be - void atomic_wait(std::atomic& atom, bool val=true); - - //* Waits for to not be and then sets it to again - void atomic_wait_set(std::atomic& atom, bool val=true); - -} - -//* Simple logging implementation -namespace Logger { - extern vector log_levels; - extern std::filesystem::path logfile; - - void set(string level); //* Set log level, valid arguments: "DISABLED", "ERROR", "WARNING", "INFO" and "DEBUG" - void log_write(uint level, string& msg); - void error(string msg); - void warning(string msg); - void info(string msg); - void debug(string msg); -} - diff --git a/src/btop_tools.hpp b/src/btop_tools.hpp new file mode 100644 index 0000000..c9fc091 --- /dev/null +++ b/src/btop_tools.hpp @@ -0,0 +1,245 @@ +/* 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 +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include + + +using std::string, std::vector; + + +//? ------------------------------------------------- NAMESPACES ------------------------------------------------------ + +namespace Global { + extern string Version; +} + +//* Collection of escape codes for text style and formatting +namespace Fx { + extern const string e; //* Escape sequence start + extern const string b; //* Bold on/off + extern const string ub; //* Bold off + extern const string d; //* Dark on + extern const string ud; //* Dark off + extern const string i; //* Italic on + extern const string ui; //* Italic off + extern const string ul; //* Underline on + extern const string uul; //* Underline off + extern const string bl; //* Blink on + extern const string ubl; //* Blink off + extern const string s; //* Strike/crossed-out on + extern const string us; //* Strike/crossed-out on/off + + //* Reset foreground/background color and text effects + extern const string reset_base; + + //* Reset text effects and restore theme foregrund and background color + extern string reset; + + //* Regex for matching color, style and curse move escape sequences + extern const std::regex escape_regex; + + //* Regex for matching only color and style escape sequences + extern const std::regex color_regex; + + //* Return a string with all colors and text styling removed + string uncolor(string& s); +} + +//* Collection of escape codes and functions for cursor manipulation +namespace Mv { + //* Move cursor to , + const string to(int line, int col); + + //* Move cursor right columns + const string r(int x); + + //* Move cursor left columns + const string l(int x); + + //* Move cursor up x lines + const string u(int x); + + //* Move cursor down x lines + const string d(int x); + + //* Save cursor position + extern const string save; + + //* Restore saved cursor postion + extern const string restore; +} + +//* Collection of escape codes and functions for terminal manipulation +namespace Term { + extern bool initialized; + extern bool resized; + extern uint width; + extern uint height; + extern string fg, bg, current_tty; + + //* Hide terminal cursor + extern const string hide_cursor; + + //* Show terminal cursor + extern const string show_cursor; + + //* Switch to alternate screen + extern const string alt_screen; + + //* Switch to normal screen + extern const string normal_screen; + + //* Clear screen and set cursor to position 0,0 + extern const string clear; + + //* Clear from cursor to end of screen + extern const string clear_end; + + //* Clear from cursor to beginning of screen + extern const string clear_begin; + + //* Enable reporting of mouse position on click and release + extern const string mouse_on; + + //* Disable mouse reporting + extern const string mouse_off; + + //* Enable reporting of mouse position at any movement + extern const string mouse_direct_on; + + //* Disable direct mouse reporting + extern const string mouse_direct_off; + + //* Refresh variables holding current terminal width and height and return true if resized + bool refresh(); + + //* Check for a valid tty, save terminal options and set new options + bool init(); + + //* Restore terminal options + void restore(); +} + +//? --------------------------------------------------- FUNCTIONS ----------------------------------------------------- + +namespace Tools { + const auto SSmax = std::numeric_limits::max(); + + //* Return number of UTF8 characters in a string with option to disregard escape sequences + size_t ulen(string str, const bool escape=false); + + //* Resize a string consisting of UTF8 characters (only reduces size) + string uresize(string str, const size_t len); + + //* Check if vector contains value + template + bool v_contains(vector& vec, T find_val) { + return std::ranges::find(vec, find_val) != vec.end(); + } + + //* Return index of from vector , returns size of if is not present + template + size_t v_index(vector& vec, T find_val) { + return std::ranges::distance(vec.begin(), std::ranges::find(vec, find_val)); + } + + //* Return current time since epoch in seconds + uint64_t time_s(); + + //* Return current time since epoch in milliseconds + uint64_t time_ms(); + + //* Return current time since epoch in microseconds + uint64_t time_micros(); + + //* Check if a string is a valid bool value + bool isbool(string& str); + + //* Convert string to bool, returning any value not equal to "true" or "True" as false + bool stobool(string& str); + + //* Check if a string is a valid integer value + bool isint(string& str); + + //* Left-trim from and return new string + string ltrim(const string& str, const string t_str = " "); + + //* Right-trim from and return new string + string rtrim(const string& str, const string t_str = " "); + + //* Left-right-trim from and return new string + string trim(const string& str, const string t_str = " "); + + //* Split at all occurrences of and return as vector of strings + vector ssplit(const string& str, const char delim = ' '); + + //* Put current thread to sleep for milliseconds + void sleep_ms(const uint& ms); + + //* Left justify string if is greater than length, limit return size to by default + string ljust(string str, const size_t x, bool utf=false, bool escape=false, bool lim=true); + + //* Right justify string if is greater than length, limit return size to by default + string rjust(string str, const size_t x, bool utf=false, bool escape=false, bool lim=true); + + //* Replace whitespaces " " with escape code for move right + string trans(const string& str); + + //* Convert seconds to format "Xd HH:MM:SS" and return string + string sec_to_dhms(uint sec); + + //* Scales up in steps of 1024 to highest possible unit and returns string with unit suffixed + //* bit=True or defaults to bytes + //* start=int to set 1024 multiplier starting unit + //* short=True always returns 0 decimals and shortens unit to 1 character + string floating_humanizer(uint64_t value, bool shorten=false, uint start=0, bool bit=false, bool per_second=false); + + //* Add std::string operator "*" : Repeat string number of times + std::string operator*(string str, size_t n); + + //* Return current time in format + string strf_time(string strf); + + //* Waits for to not be + void atomic_wait(std::atomic& atom, bool val=true); + + //* Waits for to not be and then sets it to again + void atomic_wait_set(std::atomic& atom, bool val=true); + +} + +//* Simple logging implementation +namespace Logger { + extern vector log_levels; + extern std::filesystem::path logfile; + + void set(string level); //* Set log level, valid arguments: "DISABLED", "ERROR", "WARNING", "INFO" and "DEBUG" + void log_write(uint level, string& msg); + void error(string msg); + void warning(string msg); + void info(string msg); + void debug(string msg); +} + -- cgit v1.2.3