From 478a44cc5723beadc11ff362b745b0ff1df704f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A3=CF=84=CE=AD=CF=86=CE=B1=CE=BD=CE=BF=CF=82?= Date: Sun, 2 Oct 2022 18:52:18 +0300 Subject: [PATCH] Clean up btop_tools.{hpp,cpp} files --- src/btop_tools.cpp | 41 ++++++++++++++++++++++++++++------------- src/btop_tools.hpp | 36 +++++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index 301e554..dd09696 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -35,7 +35,15 @@ tab-size = 4 #include #include -using std::string_view, std::max, std::floor, std::to_string, std::cin, std::cout, std::flush, robin_hood::unordered_flat_map; +using std::cin; +using std::cout; +using std::floor; +using std::flush; +using std::max; +using std::string_view; +using std::to_string; +using robin_hood::unordered_flat_map; + namespace fs = std::filesystem; namespace rng = std::ranges; @@ -44,9 +52,9 @@ namespace rng = std::ranges; //* Collection of escape codes and functions for terminal manipulation namespace Term { - atomic initialized = false; - atomic width = 0; - atomic height = 0; + atomic initialized{}; // defaults to false + atomic width{}; // defaults to 0 + atomic height{}; // defaults to 0 string current_tty; namespace { @@ -110,7 +118,7 @@ namespace Term { initialized = (bool)isatty(STDIN_FILENO); if (initialized) { tcgetattr(STDIN_FILENO, &initial_settings); - current_tty = (ttyname(STDIN_FILENO) != NULL ? (string)ttyname(STDIN_FILENO) : "unknown"); + current_tty = (ttyname(STDIN_FILENO) != NULL ? static_cast(ttyname(STDIN_FILENO)) : "unknown"); //? Disable stream sync cin.sync_with_stdio(false); @@ -253,14 +261,18 @@ namespace Tools { string ltrim(const string& str, const string& t_str) { string_view str_v = str; - while (str_v.starts_with(t_str)) str_v.remove_prefix(t_str.size()); - return (string)str_v; + while (str_v.starts_with(t_str)) + str_v.remove_prefix(t_str.size()); + + return string{str_v}; } string rtrim(const string& str, const string& t_str) { string_view str_v = str; - while (str_v.ends_with(t_str)) str_v.remove_suffix(t_str.size()); - return (string)str_v; + while (str_v.ends_with(t_str)) + str_v.remove_suffix(t_str.size()); + + return string{str_v}; } auto ssplit(const string& str, const char& delim) -> vector { @@ -318,7 +330,7 @@ namespace Tools { newstr.append(Mv::r(x)); oldstr.remove_prefix(pos + x); } - return (newstr.empty()) ? str : newstr + (string)oldstr; + return (newstr.empty()) ? str : newstr + string{oldstr}; } string sec_to_dhms(size_t seconds, bool no_days, bool no_seconds) { @@ -447,7 +459,7 @@ namespace Tools { string hostname() { char host[HOST_NAME_MAX]; gethostname(host, HOST_NAME_MAX); - return (string)host; + return string{host}; } string username() { @@ -497,14 +509,17 @@ namespace Logger { } if (not ec) { std::ofstream lwrite(logfile, std::ios::app); - if (first) { first = false; lwrite << "\n" << strf_time(tdf) << "===> btop++ v." << Global::Version << "\n";} + if (first) { + first = false; + lwrite << "\n" << strf_time(tdf) << "===> btop++ v." << Global::Version << "\n"; + } lwrite << strf_time(tdf) << log_levels.at(level) << ": " << msg << "\n"; } else logfile.clear(); } catch (const std::exception& e) { logfile.clear(); - throw std::runtime_error("Exception in Logger::log_write() : " + (string)e.what()); + throw std::runtime_error("Exception in Logger::log_write() : " + string{e.what()}); } } } diff --git a/src/btop_tools.hpp b/src/btop_tools.hpp index 862981a..c4d4279 100644 --- a/src/btop_tools.hpp +++ b/src/btop_tools.hpp @@ -18,16 +18,17 @@ tab-size = 4 #pragma once -#include -#include +#include // for std::ranges::count_if #include #include -#include +#include #include #include -#include +#include +#include #include #include +#include #include #include #ifndef HOST_NAME_MAX @@ -38,7 +39,12 @@ tab-size = 4 #endif #endif -using std::string, std::vector, std::atomic, std::to_string, std::tuple, std::array; +using std::array; +using std::atomic; +using std::string; +using std::to_string; +using std::tuple; +using std::vector; //? ------------------------------------------------- NAMESPACES ------------------------------------------------------ @@ -80,19 +86,19 @@ namespace Fx { //* Collection of escape codes and functions for cursor manipulation namespace Mv { //* Move cursor to , - inline string to(const int& line, const 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 columns - inline string r(const int& x) { return Fx::e + to_string(x) + 'C'; } + inline string r(int x) { return Fx::e + to_string(x) + 'C'; } //* Move cursor left columns - inline string l(const 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 - inline string u(const 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 - inline string d(const int& x) { return Fx::e + to_string(x) + 'B'; } + inline string d(int x) { return Fx::e + to_string(x) + 'B'; } //* Save cursor position const string save = Fx::e + "s"; @@ -254,10 +260,14 @@ namespace Tools { auto ssplit(const string& str, const char& delim = ' ') -> vector; //* Put current thread to sleep for milliseconds - inline void sleep_ms(const size_t& ms) { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); } + inline void sleep_ms(const size_t& ms) { + std::this_thread::sleep_for(std::chrono::milliseconds(ms)); + } //* Put current thread to sleep for microseconds - inline void sleep_micros(const size_t& micros) { std::this_thread::sleep_for(std::chrono::microseconds(micros)); } + inline void sleep_micros(const size_t& micros) { + std::this_thread::sleep_for(std::chrono::microseconds(micros)); + } //* Left justify string if is greater than length, limit return size to by default string ljust(string str, const size_t x, const bool utf=false, const bool wide=false, const bool limit=true); @@ -308,7 +318,7 @@ namespace Tools { //* Sets atomic to true on construct, sets to false on destruct class atomic_lock { atomic& atom; - bool not_true = false; + bool not_true{}; // defaults to false public: atomic_lock(atomic& atom, bool wait=false); ~atomic_lock();