summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authoraristocratos <gnmjpl@gmail.com>2021-06-02 21:33:26 +0200
committeraristocratos <gnmjpl@gmail.com>2021-06-02 21:33:26 +0200
commitce34cbb8d07e6bd427da2d8a6911fc0480d954e2 (patch)
tree325ff9a5fb831ac904af2965ac175b87ff80cb9e /src
parent9bf7da049b3b1b4e81f7c366dadac1df0cbf2f47 (diff)
Small improvements
Diffstat (limited to 'src')
-rw-r--r--src/btop_draw.h57
-rw-r--r--src/btop_input.h7
-rw-r--r--src/btop_linux.h1
-rw-r--r--src/btop_menu.h (renamed from src/btop_globs.h)10
-rw-r--r--src/btop_theme.h1
-rw-r--r--src/btop_tools.h58
6 files changed, 61 insertions, 73 deletions
diff --git a/src/btop_draw.h b/src/btop_draw.h
index 1f782d4..7d56f11 100644
--- a/src/btop_draw.h
+++ b/src/btop_draw.h
@@ -71,6 +71,8 @@ namespace Symbols {
namespace Draw {
+ using namespace Tools;
+
struct BoxConf {
uint x=0, y=0;
uint width=0, height=0;
@@ -101,9 +103,9 @@ namespace Draw {
//* Draw corners
out += Mv::to(c.y, c.x) + Symbols::left_up +
- Mv::to(c.y, c.x + c.width) + Symbols::right_up +
+ Mv::to(c.y, c.x + c.width - 1) + Symbols::right_up +
Mv::to(c.y + c.height - 1, c.x) + Symbols::left_down +
- Mv::to(c.y + c.height - 1, c.x + c.width) + Symbols::right_down;
+ Mv::to(c.y + c.height - 1, c.x + c.width - 1) + Symbols::right_down;
//* Draw titles if defined
if (!c.title.empty()){
@@ -115,12 +117,12 @@ namespace Draw {
Fx::ub + lcolor + Symbols::title_right;
}
- return out + Fx::reset + Mv::to(c.y + 1, c.x + 2);
+ return out + Fx::reset + Mv::to(c.y + 1, c.x + 1);
}
//* Class holding a percentage meter
class Meter {
- string out, color_gradient;
+ string color_gradient;
int width = 0;
bool invert = false;
vector<string> cache;
@@ -130,20 +132,18 @@ namespace Draw {
this->width = width;
this->color_gradient = color_gradient;
this->invert = invert;
- out.clear();
cache.clear();
cache.insert(cache.begin(), 101, "");
}
//* Return a string representation of the meter with given value
string operator()(int value) {
- if (width < 1) return out;
+ if (width < 1) return "";
value = clamp(value, 0, 100);
- if (!cache.at(value).empty()) return out = cache.at(value);
- out.clear();
- int y;
+ if (!cache.at(value).empty()) return cache.at(value);
+ string& out = cache.at(value);
for (int i : iota(1, width + 1)) {
- y = round((double)i * 100.0 / width);
+ int y = round((double)i * 100.0 / width);
if (value >= y)
out += Theme::g(color_gradient)[invert ? 100 - y : y] + Symbols::meter;
else {
@@ -152,28 +152,25 @@ namespace Draw {
}
}
out += Fx::reset;
- return cache.at(value) = out;
- }
-
- string operator()() {
return out;
}
+
};
//* Class holding a graph
class Graph {
string out, color_gradient;
- int width = 0, height = 0, lowest = 0;
+ int width = 0, height = 0;
long long last = 0, max_value = 0, offset = 0;
bool current = true, no_zero = false, invert = false;
unordered_flat_map<bool, vector<string>> graphs = { {true, {}}, {false, {}}};
- vector<string> graph_symbol;
//* Create two representations of the graph to switch between to represent two values for each braille character
void _create(const vector<long long>& data, int data_offset) {
const bool mult = (data.size() - data_offset > 1);
if (mult && (data.size() - data_offset) % 2 != 0) data_offset--;
+ auto& graph_symbol = (invert) ? Symbols::graph_down : Symbols::graph_up;
array<int, 2> result;
const float mod = (height == 1) ? 0.3 : 0.1;
long long data_value = 0;
@@ -183,10 +180,10 @@ namespace Draw {
}
//? Horizontal iteration over values in <data>
- for (int i = data_offset; i < (int)data.size(); i++) {
+ for (int i : iota(data_offset, (int)data.size())) {
+ current = !current;
if (i == -1) { data_value = 0; last = 0; }
else data_value = data[i];
- if (mult) current = !current;
if (max_value > 0) data_value = clamp((data_value + offset) * 100 / max_value, 0ll, 100ll);
//? Vertical iteration over height of graph
for (int horizon : iota(0, height)){
@@ -197,7 +194,7 @@ namespace Draw {
for (auto value : {last, data_value}) {
if (value >= cur_high)
result[ai] = 4;
- else if (value < cur_low)
+ else if (value <= cur_low)
result[ai] = 0;
else {
result[ai] = round((float)(value - cur_low) * 4 / (cur_high - cur_low) + mod);
@@ -233,40 +230,34 @@ namespace Draw {
this->invert = invert; this->offset = offset;
this->no_zero = no_zero; this->max_value = max_value;
this->color_gradient = color_gradient;
- // if (height == 1) graph_symbol = (invert) ? Symbols::graph_down_small : Symbols::graph_up_small;
- graph_symbol = (invert) ? Symbols::graph_down : Symbols::graph_up;
- if (no_zero) lowest = 1;
- // current = true;
int value_width = ceil((float)data.size() / 2);
int data_offset = 0;
if (value_width > width) data_offset = data.size() - width * 2;
- //? Populate the two switching graph vectors and fill empty space if width > data size
- for (int i : iota(0, height)) {
- (void) i;
- graphs[true].push_back((value_width < width) ? graph_symbol[0.0] * (width - value_width) : "");
- graphs[false].push_back((value_width < width) ? graph_symbol[0.0] * (width - value_width) : "");
+ //? Populate the two switching graph vectors and fill empty space if data size < width
+ auto& graph_symbol = (invert) ? Symbols::graph_down : Symbols::graph_up;
+ for (int i : iota(0, height * 2)) {
+ graphs[(i % 2 != 0)].push_back((value_width < width) ? ((height == 1) ? Mv::r(1) : graph_symbol[0]) * (width - value_width) : "");
}
if (data.size() == 0) return;
this->_create(data, data_offset);
}
//* Add last value from back of <data> and return string representation of graph
- string operator()(const vector<long long>& data, bool data_same = false) {
+ string& operator()(const vector<long long>& data, bool data_same = false) {
if (data_same) return out;
- current = !current;
//? Make room for new characters on graph
for (int i : iota(0, height)) {
- if (graphs[current][i].starts_with(Fx::e)) graphs[current][i].erase(0, 4);
- else graphs[current][i].erase(0, 3);
+ if (graphs[(!current)][i].starts_with(Fx::e)) graphs[current][i].erase(0, 4);
+ else graphs[(!current)][i].erase(0, 3);
}
this->_create(data, (int)data.size() - 1);
return out;
}
//* Return string representation of graph
- string operator()() {
+ string& operator()() {
return out;
}
};
diff --git a/src/btop_input.h b/src/btop_input.h
index a73c25d..2dc98f4 100644
--- a/src/btop_input.h
+++ b/src/btop_input.h
@@ -81,11 +81,10 @@ namespace Input {
//* Poll keyboard & mouse input for <timeout> ms and return input availabilty as a bool
bool poll(int timeout=0){
if (timeout < 1) return cin.rdbuf()->in_avail() > 0;
- auto timer = 0;
- while (timer * 10 <= timeout) {
+ int timer = 0;
+ while (timer++ * 10 <= timeout) {
if (cin.rdbuf()->in_avail() > 0) return true;
- sleep_ms(10);
- ++timer;
+ sleep_ms( (timer * 10 <= timeout) ? 10 : timeout % 10);
}
return false;
}
diff --git a/src/btop_linux.h b/src/btop_linux.h
index 8fcbdea..2854b9b 100644
--- a/src/btop_linux.h
+++ b/src/btop_linux.h
@@ -36,7 +36,6 @@ tab-size = 4
#include <unistd.h>
#include <btop_config.h>
-#include <btop_globs.h>
#include <btop_tools.h>
diff --git a/src/btop_globs.h b/src/btop_menu.h
index ea1efd8..d11177a 100644
--- a/src/btop_globs.h
+++ b/src/btop_menu.h
@@ -16,8 +16,8 @@ indent = tab
tab-size = 4
*/
-#ifndef _btop_globs_included_
-#define _btop_globs_included_ 1
+#ifndef _btop_menu_included_
+#define _btop_menu_included_ 1
#include <string>
#include <vector>
@@ -27,11 +27,9 @@ tab-size = 4
using std::string, std::vector, std::unordered_map, std::array, std::atomic, robin_hood::unordered_flat_map;
-namespace Global {
-
- atomic<bool> stop_all(false);
-
+namespace Menu {
+ atomic<bool> active(false);
const unordered_flat_map<string, unordered_map<string, vector<string>>> Menus = {
{ "options", {
diff --git a/src/btop_theme.h b/src/btop_theme.h
index 9573c2d..c354cc0 100644
--- a/src/btop_theme.h
+++ b/src/btop_theme.h
@@ -27,7 +27,6 @@ tab-size = 4
#include <ranges>
#include <algorithm>
-#include <btop_globs.h>
#include <btop_tools.h>
#include <btop_config.h>
diff --git a/src/btop_tools.h b/src/btop_tools.h
index cc91993..5d9eb59 100644
--- a/src/btop_tools.h
+++ b/src/btop_tools.h
@@ -38,9 +38,7 @@ tab-size = 4
#include <termios.h>
#include <sys/ioctl.h>
-#include <btop_globs.h>
-
-using std::string, std::vector, std::regex, std::max, std::to_string, std::cin, std::atomic, robin_hood::unordered_flat_map;
+using std::string, std::vector, std::array, std::regex, std::max, std::to_string, std::cin, std::atomic, robin_hood::unordered_flat_map;
namespace fs = std::filesystem;
//? ------------------------------------------------- NAMESPACES ------------------------------------------------------
@@ -95,19 +93,19 @@ namespace Fx {
//* Collection of escape codes and functions for cursor manipulation
namespace Mv {
//* Move cursor to <line>, <column>
- inline string to(int line, int col){ return Fx::e + to_string(line) + ";" + to_string(col) + "f";}
+ const string to(int line, int col){ return Fx::e + to_string(line) + ";" + to_string(col) + "f";}
//* Move cursor right <x> columns
- inline string r(int x){ return Fx::e + to_string(x) + "C";}
+ const string r(int x){ return Fx::e + to_string(x) + "C";}
//* Move cursor left <x> columns
- inline string l(int x){ return Fx::e + to_string(x) + "D";}
+ const 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";}
+ const 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";}
+ const string d(int x) { return Fx::e + to_string(x) + "B";}
//* Save cursor position
const string save = Fx::e + "s";
@@ -229,51 +227,51 @@ namespace Term {
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){
+ 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<unsigned char>(c) & 0xC0) != 0x80; } );
}
//* Return current time since epoch in seconds
- inline uint64_t time_s(){
+ uint64_t time_s(){
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
//* Return current time since epoch in milliseconds
- inline uint64_t time_ms(){
+ uint64_t time_ms(){
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
- //* Return current time since epoch in milliseconds
- inline uint64_t time_micros(){
+ //* Return current time since epoch in microseconds
+ uint64_t time_micros(){
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
//* Check if a string is a valid bool value
- inline bool isbool(string& str){
+ bool isbool(string& str){
return (str == "true") || (str == "false") || (str == "True") || (str == "False");
}
//* Check if a string is a valid integer value
- inline bool isint(string& str){
+ bool isint(string& str){
return all_of(str.begin(), str.end(), ::isdigit);
}
//* Left-trim <t_str> from <str> and return string
- inline string ltrim(string str, string t_str = " "){
+ string ltrim(string str, string t_str = " "){
while (str.starts_with(t_str)) str.erase(0, t_str.size());
return str;
}
//* Right-trim <t_str> from <str> and return string
- inline string rtrim(string str, string t_str = " "){
+ 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 <t_str> from <str> and return string
- inline string trim(string str, string t_str = " "){
+ string trim(string str, string t_str = " "){
return ltrim(rtrim(str, t_str), t_str);
}
@@ -452,8 +450,6 @@ namespace Tools {
namespace Logger {
namespace {
std::atomic<bool> busy (false);
- fs::path logfile;
- uint loglevel = 2;
bool first = true;
string tdf = "%Y/%m/%d (%T) | ";
unordered_flat_map<uint, string> log_levels = {
@@ -465,19 +461,25 @@ namespace Logger {
};
}
+ fs::path logfile;
+ uint loglevel = 2;
+
void log_write(uint level, string& msg){
- if (logfile.empty() || loglevel < level) return;
+ if (loglevel < level || logfile.empty()) return;
busy.wait(true); busy.store(true);
- if (fs::exists(logfile) && fs::file_size(logfile) > 1024 << 10) {
+ std::error_code ec;
+ if (fs::file_size(logfile, ec) > 1024 << 10) {
auto old_log = logfile;
old_log += ".1";
- if (fs::exists(old_log)) fs::remove(old_log);
- fs::rename(logfile, old_log);
+ if (fs::exists(old_log)) fs::remove(old_log, ec);
+ fs::rename(logfile, old_log, ec);
+ }
+ if (!ec) {
+ std::ofstream lwrite(logfile, std::ios::app);
+ if (first) { first = false; lwrite << "\n" << Tools::strf_time(tdf) << "===> btop++ v." << Global::Version << "\n";}
+ lwrite << Tools::strf_time(tdf) << log_levels[level] << ": " << msg << "\n";
+ lwrite.close();
}
- std::ofstream lwrite(logfile, std::ios::app);
- if (first) { first = false; lwrite << "\n" << Tools::strf_time(tdf) << "===> btop++ v." << Global::Version << "\n";}
- lwrite << Tools::strf_time(tdf) << log_levels[level] << ": " << msg << "\n";
- lwrite.close();
busy.store(false);
}