Clean up btop_tools.{hpp,cpp} files

This commit is contained in:
Στέφανος 2022-10-02 18:52:18 +03:00
parent bc9b803189
commit 478a44cc57
2 changed files with 51 additions and 26 deletions

View file

@ -35,7 +35,15 @@ tab-size = 4
#include <btop_tools.hpp>
#include <btop_config.hpp>
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<bool> initialized = false;
atomic<int> width = 0;
atomic<int> height = 0;
atomic<bool> initialized{}; // defaults to false
atomic<int> width{}; // defaults to 0
atomic<int> 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<string>(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<string> {
@ -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()});
}
}
}

View file

@ -18,16 +18,17 @@ tab-size = 4
#pragma once
#include <string>
#include <vector>
#include <algorithm> // for std::ranges::count_if
#include <array>
#include <atomic>
#include <regex>
#include <chrono>
#include <filesystem>
#include <ranges>
#include <chrono>
#include <regex>
#include <string>
#include <thread>
#include <tuple>
#include <vector>
#include <pthread.h>
#include <limits.h>
#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 <line>, <column>
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 <x> 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 <x> 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<string>;
//* Put current thread to sleep for <ms> 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 <micros> 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 <str> if <x> is greater than <str> length, limit return size to <x> 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<bool> to true on construct, sets to false on destruct
class atomic_lock {
atomic<bool>& atom;
bool not_true = false;
bool not_true{}; // defaults to false
public:
atomic_lock(atomic<bool>& atom, bool wait=false);
~atomic_lock();