/* 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 */ #ifndef _btop_tools_included_ #define _btop_tools_included_ #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; //? ------------------------------------------------- NAMESPACES ------------------------------------------------------ //* Collection of escape codes for text style and formatting namespace Fx { //* Escape sequence start const string e = "\x1b["; //* Bold on const string b = e + "1m"; //* Dark on const string d = e + "2m"; //* Italic on const string i = e + "3m"; //* Underline on const string ul = e + "4m"; //* Blink on const string bl = e + "5m"; //* Strike / crossed-out on const string s = e + "9m"; //* 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; }; //* Collection of escape codes and functions for cursor manipulation 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"; }; //? --------------------------------------------------- FUNCTIONS ----------------------------------------------------- //* Return number of UTF8 characters in a string size_t ulen(string s){ return std::count_if(s.begin(), s.end(), [](char c) { return (static_cast(c) & 0xC0) != 0x80; } ); } //* Return current time since epoch in milliseconds uint64_t time_ms(){ return std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); } //* Check if a string is a valid bool value bool isbool(string str){ return (str == "true") || (str == "false") || (str == "True") || (str == "False"); } //* Check if a string is a valid integer value bool isint(string str){ return all_of(str.begin(), str.end(), ::isdigit); } //* Left-trim from and return string string ltrim(string str, string t_str = " "){ while (str.starts_with(t_str)) str.erase(0, t_str.size()); return str; } //* Right-trim from and return string string rtrim(string str, string t_str = " "){ while (str.ends_with(t_str)) str.resize(str.size() - t_str.size()); return str; } //* Left-right-trim from and return string string trim(string str, string t_str = " "){ return ltrim(rtrim(str, t_str), t_str); } //* Split at