/* 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_ 1 #include #include #include #include #include #include #include #include #include #include #include using std::string, std::vector, std::map, std::regex, std::max, std::to_string, std::cin; //? ------------------------------------------------- NAMESPACES ------------------------------------------------------ //* Collection of escape codes for text style and formatting namespace Fx { //* Escape sequence start const string e = "\x1b["; //* Bold on/off const string b = e + "1m"; const string ub = e + "22m"; //* Dark on/off const string d = e + "2m"; const string ud = e + "22m"; //* Italic on/off const string i = e + "3m"; const string ui = e + "23m"; //* Underline on/off const string ul = e + "4m"; const string uul = e + "24m"; //* Blink on/off const string bl = e + "5m"; const string ubl = e + "25m"; //* Strike / crossed-out on/off const string s = e + "9m"; 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; //* Regex for matching color, style and curse move escape sequences const regex escape_regex("\033\\[\\d+;?\\d?;?\\d*;?\\d*;?\\d*(m|f|s|u|C|D|A|B){1}"); //* Regex for matching only color and style escape sequences const regex color_regex("\033\\[\\d+;?\\d?;?\\d*;?\\d*;?\\d*(m){1}"); //* Return a string with all colors and text styling removed string uncolor(string& s){ return regex_replace(s, color_regex, ""); } }; //* 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"; }; //* Collection of escape codes and functions for terminal manipulation namespace Term { bool initialized = false; bool resized = false; uint width = 0; uint height = 0; string fg, bg; namespace { struct termios initial_settings; //* Toggle terminal input echo bool echo(bool on=true){ struct termios settings; if (tcgetattr(STDIN_FILENO, &settings)) return false; if (on) settings.c_lflag |= ECHO; else settings.c_lflag &= ~(ECHO); return 0 == tcsetattr(STDIN_FILENO, TCSANOW, &settings); } //* Refresh variables holding current terminal width and height and return true if resized bool refresh(){ 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; return resized; } }; //* Hide terminal cursor const string hide_cursor = Fx::e + "?25l"; //* Show terminal cursor const string show_cursor = Fx::e + "?25h"; //* Switch to alternate screen const string alt_screen = Fx::e + "?1049h"; //* Switch to normal screen const string normal_screen = Fx::e + "?1049l"; //* Clear screen and set cursor to position 0,0 const string clear = Fx::e + "2J" + Fx::e + "0;0f"; //* Clear from cursor to end of screen const string clear_end = Fx::e + "0J"; //* Clear from cursor to beginning of screen const string clear_begin = Fx::e + "1J"; //* Enable reporting of mouse position on click and release const string mouse_on = Fx::e + "?1002h" + Fx::e + "?1015h" + Fx::e + "?1006h"; //* Disable mouse reporting const string mouse_off = Fx::e + "?1002l"; //* Enable reporting of mouse position at any movement const string mouse_direct_on = Fx::e + "?1003h"; //* Disable direct mouse reporting const string mouse_direct_off = Fx::e + "?1003l"; //* Toggle need for return key when reading input bool linebuffered(bool on=true){ struct termios settings; if (tcgetattr(STDIN_FILENO, &settings)) return false; if (on) settings.c_lflag |= ICANON; else settings.c_lflag &= ~(ICANON); if (tcsetattr(STDIN_FILENO, TCSANOW, &settings)) return false; if (on) setlinebuf(stdin); else setbuf(stdin, NULL); return true; } //* Check for a valid tty, save terminal options and set new options bool init(){ if (!initialized){ initialized = (bool)isatty(STDIN_FILENO); if (initialized) { initialized = (0 == tcgetattr(STDIN_FILENO, &initial_settings)); cin.sync_with_stdio(false); cin.tie(NULL); echo(false); linebuffered(false); refresh(); resized = false; } } return initialized; } //* Restore terminal options void restore(){ if (initialized) { echo(true); linebuffered(true); tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings); initialized = false; } } }; //? --------------------------------------------------- FUNCTIONS ----------------------------------------------------- namespace Tools { //* Return number of UTF8 characters in a string with option to disregard escape sequences inline size_t ulen(string s, bool escape=false){ if (escape) s = std::regex_replace(s, Fx::escape_regex, ""); 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