mirror of
https://github.com/aristocratos/btop.git
synced 2024-09-29 16:51:40 +13:00
Clean up btop_tools.{hpp,cpp} files
This commit is contained in:
parent
bc9b803189
commit
478a44cc57
2 changed files with 51 additions and 26 deletions
|
@ -35,7 +35,15 @@ tab-size = 4
|
||||||
#include <btop_tools.hpp>
|
#include <btop_tools.hpp>
|
||||||
#include <btop_config.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 fs = std::filesystem;
|
||||||
namespace rng = std::ranges;
|
namespace rng = std::ranges;
|
||||||
|
|
||||||
|
@ -44,9 +52,9 @@ namespace rng = std::ranges;
|
||||||
//* Collection of escape codes and functions for terminal manipulation
|
//* Collection of escape codes and functions for terminal manipulation
|
||||||
namespace Term {
|
namespace Term {
|
||||||
|
|
||||||
atomic<bool> initialized = false;
|
atomic<bool> initialized{}; // defaults to false
|
||||||
atomic<int> width = 0;
|
atomic<int> width{}; // defaults to 0
|
||||||
atomic<int> height = 0;
|
atomic<int> height{}; // defaults to 0
|
||||||
string current_tty;
|
string current_tty;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -110,7 +118,7 @@ namespace Term {
|
||||||
initialized = (bool)isatty(STDIN_FILENO);
|
initialized = (bool)isatty(STDIN_FILENO);
|
||||||
if (initialized) {
|
if (initialized) {
|
||||||
tcgetattr(STDIN_FILENO, &initial_settings);
|
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
|
//? Disable stream sync
|
||||||
cin.sync_with_stdio(false);
|
cin.sync_with_stdio(false);
|
||||||
|
@ -253,14 +261,18 @@ namespace Tools {
|
||||||
|
|
||||||
string ltrim(const string& str, const string& t_str) {
|
string ltrim(const string& str, const string& t_str) {
|
||||||
string_view str_v = str;
|
string_view str_v = str;
|
||||||
while (str_v.starts_with(t_str)) str_v.remove_prefix(t_str.size());
|
while (str_v.starts_with(t_str))
|
||||||
return (string)str_v;
|
str_v.remove_prefix(t_str.size());
|
||||||
|
|
||||||
|
return string{str_v};
|
||||||
}
|
}
|
||||||
|
|
||||||
string rtrim(const string& str, const string& t_str) {
|
string rtrim(const string& str, const string& t_str) {
|
||||||
string_view str_v = str;
|
string_view str_v = str;
|
||||||
while (str_v.ends_with(t_str)) str_v.remove_suffix(t_str.size());
|
while (str_v.ends_with(t_str))
|
||||||
return (string)str_v;
|
str_v.remove_suffix(t_str.size());
|
||||||
|
|
||||||
|
return string{str_v};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto ssplit(const string& str, const char& delim) -> vector<string> {
|
auto ssplit(const string& str, const char& delim) -> vector<string> {
|
||||||
|
@ -318,7 +330,7 @@ namespace Tools {
|
||||||
newstr.append(Mv::r(x));
|
newstr.append(Mv::r(x));
|
||||||
oldstr.remove_prefix(pos + 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) {
|
string sec_to_dhms(size_t seconds, bool no_days, bool no_seconds) {
|
||||||
|
@ -447,7 +459,7 @@ namespace Tools {
|
||||||
string hostname() {
|
string hostname() {
|
||||||
char host[HOST_NAME_MAX];
|
char host[HOST_NAME_MAX];
|
||||||
gethostname(host, HOST_NAME_MAX);
|
gethostname(host, HOST_NAME_MAX);
|
||||||
return (string)host;
|
return string{host};
|
||||||
}
|
}
|
||||||
|
|
||||||
string username() {
|
string username() {
|
||||||
|
@ -497,14 +509,17 @@ namespace Logger {
|
||||||
}
|
}
|
||||||
if (not ec) {
|
if (not ec) {
|
||||||
std::ofstream lwrite(logfile, std::ios::app);
|
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";
|
lwrite << strf_time(tdf) << log_levels.at(level) << ": " << msg << "\n";
|
||||||
}
|
}
|
||||||
else logfile.clear();
|
else logfile.clear();
|
||||||
}
|
}
|
||||||
catch (const std::exception& e) {
|
catch (const std::exception& e) {
|
||||||
logfile.clear();
|
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()});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,16 +18,17 @@ tab-size = 4
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <algorithm> // for std::ranges::count_if
|
||||||
#include <vector>
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <regex>
|
#include <chrono>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <chrono>
|
#include <regex>
|
||||||
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
#include <vector>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#ifndef HOST_NAME_MAX
|
#ifndef HOST_NAME_MAX
|
||||||
|
@ -38,7 +39,12 @@ tab-size = 4
|
||||||
#endif
|
#endif
|
||||||
#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 ------------------------------------------------------
|
//? ------------------------------------------------- NAMESPACES ------------------------------------------------------
|
||||||
|
@ -80,19 +86,19 @@ namespace Fx {
|
||||||
//* Collection of escape codes and functions for cursor manipulation
|
//* Collection of escape codes and functions for cursor manipulation
|
||||||
namespace Mv {
|
namespace Mv {
|
||||||
//* Move cursor to <line>, <column>
|
//* 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
|
//* 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
|
//* 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
|
//* 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
|
//* 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
|
//* Save cursor position
|
||||||
const string save = Fx::e + "s";
|
const string save = Fx::e + "s";
|
||||||
|
@ -254,10 +260,14 @@ namespace Tools {
|
||||||
auto ssplit(const string& str, const char& delim = ' ') -> vector<string>;
|
auto ssplit(const string& str, const char& delim = ' ') -> vector<string>;
|
||||||
|
|
||||||
//* Put current thread to sleep for <ms> milliseconds
|
//* 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
|
//* 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
|
//* 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);
|
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
|
//* Sets atomic<bool> to true on construct, sets to false on destruct
|
||||||
class atomic_lock {
|
class atomic_lock {
|
||||||
atomic<bool>& atom;
|
atomic<bool>& atom;
|
||||||
bool not_true = false;
|
bool not_true{}; // defaults to false
|
||||||
public:
|
public:
|
||||||
atomic_lock(atomic<bool>& atom, bool wait=false);
|
atomic_lock(atomic<bool>& atom, bool wait=false);
|
||||||
~atomic_lock();
|
~atomic_lock();
|
||||||
|
|
Loading…
Reference in a new issue