Skip to content

Commit

Permalink
Merge pull request #438 from stefanos82/main
Browse files Browse the repository at this point in the history
Further cleanup
  • Loading branch information
aristocratos committed Oct 5, 2022
2 parents 0cb31e7 + f0e413e commit d58a17a
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ namespace Runner {
//? ------------------------------------------ Secondary thread end -----------------------------------------------

//* Runs collect and draw in a secondary thread, unlocks and locks config to update cached values
void run(const string& box, const bool no_update, const bool force_redraw) {
void run(const string& box, bool no_update, bool force_redraw) {
atomic_wait_for(active, true, 5000);
if (active) {
Logger::error("Stall in Runner thread, restarting!");
Expand Down
37 changes: 20 additions & 17 deletions src/btop_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,13 @@ namespace Draw {
}

string createBox(const int x, const int y, const int width,
const int height, string line_color, const bool fill,
const int height, string line_color, bool fill,
const string title, const string title2, const int num) {
string out;
if (line_color.empty()) line_color = Theme::c("div_line");

if (line_color.empty())
line_color = Theme::c("div_line");

auto tty_mode = Config::getB("tty_mode");
auto rounded = Config::getB("rounded_corners");
const string numbering = (num == 0) ? "" : Theme::c("hi_fg") + (tty_mode ? std::to_string(num) : Symbols::superscript.at(clamp(num, 0, 9)));
Expand All @@ -243,12 +246,12 @@ namespace Draw {
out = Fx::reset + line_color;

//? Draw horizontal lines
for (const int& hpos : {y, y + height - 1}) {
for (const int& hpos : {y, y + height - 1}) {
out += Mv::to(hpos, x) + Symbols::h_line * (width - 1);
}

//? Draw vertical lines and fill if enabled
for (const int& hpos : iota(y + 1, y + height - 1)) {
for (const int& hpos : iota(y + 1, y + height - 1)) {
out += Mv::to(hpos, x) + Symbols::v_line
+ ((fill) ? string(width - 2, ' ') : Mv::r(width - 2))
+ Symbols::v_line;
Expand Down Expand Up @@ -340,7 +343,7 @@ namespace Draw {
//* Meter class ------------------------------------------------------------------------------------------------------------>
Meter::Meter() {}

Meter::Meter(const int width, const string& color_gradient, const bool invert)
Meter::Meter(const int width, const string& color_gradient, bool invert)
: width(width), color_gradient(color_gradient), invert(invert) {}

string Meter::operator()(int value) {
Expand All @@ -363,7 +366,7 @@ namespace Draw {

//* Graph class ------------------------------------------------------------------------------------------------------------>
void Graph::_create(const deque<long long>& data, int data_offset) {
const bool mult = (data.size() - data_offset > 1);
bool mult = (data.size() - data_offset > 1);
const auto& graph_symbol = Symbols::graph_symbols.at(symbol + '_' + (invert ? "down" : "up"));
array<int, 2> result;
const float mod = (height == 1) ? 0.3 : 0.1;
Expand Down Expand Up @@ -461,7 +464,7 @@ namespace Draw {
this->_create(data, data_offset);
}

string& Graph::operator()(const deque<long long>& data, const bool data_same) {
string& Graph::operator()(const deque<long long>& data, bool data_same) {
if (data_same) return out;

//? Make room for new characters on graph
Expand Down Expand Up @@ -500,7 +503,7 @@ namespace Cpu {
vector<Draw::Graph> core_graphs;
vector<Draw::Graph> temp_graphs;

string draw(const cpu_info& cpu, const bool force_redraw, const bool data_same) {
string draw(const cpu_info& cpu, bool force_redraw, bool data_same) {
if (Runner::stopping) return "";
if (force_redraw) redraw = true;
bool show_temps = (Config::getB("check_temp") and got_sensors);
Expand Down Expand Up @@ -722,7 +725,7 @@ namespace Mem {
unordered_flat_map<string, Draw::Meter> disk_meters_free;
unordered_flat_map<string, Draw::Graph> io_graphs;

string draw(const mem_info& mem, const bool force_redraw, const bool data_same) {
string draw(const mem_info& mem, bool force_redraw, bool data_same) {
if (Runner::stopping) return "";
if (force_redraw) redraw = true;
auto show_swap = Config::getB("show_swap");
Expand Down Expand Up @@ -881,7 +884,7 @@ namespace Mem {
if (show_disks) {
const auto& disks = mem.disks;
cx = mem_width; cy = 0;
const bool big_disk = disks_width >= 25;
bool big_disk = disks_width >= 25;
divider = Mv::l(1) + Theme::c("div_line") + Symbols::div_left + Symbols::h_line * disks_width + Theme::c("mem_box") + Fx::ub + Symbols::div_right + Mv::l(disks_width);
const string hu_div = Theme::c("div_line") + Symbols::h_line + Theme::c("main_fg");
if (io_mode) {
Expand Down Expand Up @@ -976,7 +979,7 @@ namespace Net {
unordered_flat_map<string, Draw::Graph> graphs;
string box;

string draw(const net_info& net, const bool force_redraw, const bool data_same) {
string draw(const net_info& net, bool force_redraw, bool data_same) {
if (Runner::stopping) return "";
if (force_redraw) redraw = true;
auto net_sync = Config::getB("net_sync");
Expand Down Expand Up @@ -1145,11 +1148,11 @@ namespace Proc {
return (not changed ? -1 : selected);
}

string draw(const vector<proc_info>& plist, const bool force_redraw, const bool data_same) {
string draw(const vector<proc_info>& plist, bool force_redraw, bool data_same) {
if (Runner::stopping) return "";
auto proc_tree = Config::getB("proc_tree");
const bool show_detailed = (Config::getB("show_detailed") and cmp_equal(Proc::detailed.last_pid, Config::getI("detailed_pid")));
const bool proc_gradient = (Config::getB("proc_gradient") and not Config::getB("lowcolor") and Theme::gradients.contains("proc"));
bool show_detailed = (Config::getB("show_detailed") and cmp_equal(Proc::detailed.last_pid, Config::getI("detailed_pid")));
bool proc_gradient = (Config::getB("proc_gradient") and not Config::getB("lowcolor") and Theme::gradients.contains("proc"));
auto proc_colors = Config::getB("proc_colors");
auto tty_mode = Config::getB("tty_mode");
auto& graph_symbol = (tty_mode ? "tty" : Config::getS("graph_symbol_proc"));
Expand Down Expand Up @@ -1191,7 +1194,7 @@ namespace Proc {

//? Detailed box
if (show_detailed) {
const bool alive = detailed.status != "Dead";
bool alive = detailed.status != "Dead";
dgraph_x = x;
dgraph_width = max(width / 3, width - 121);
d_width = width - dgraph_width - 1;
Expand Down Expand Up @@ -1344,7 +1347,7 @@ namespace Proc {

//? Draw details box if shown
if (show_detailed) {
const bool alive = detailed.status != "Dead";
bool alive = detailed.status != "Dead";
const int item_fit = floor((double)(d_width - 2) / 10);
const int item_width = floor((double)(d_width - 2) / min(item_fit, 8));

Expand Down Expand Up @@ -1403,7 +1406,7 @@ namespace Proc {
}

//? Update graphs for processes with above 0.0% cpu usage, delete if below 0.1% 10x times
const bool has_graph = show_graphs ? p_counters.contains(p.pid) : false;
bool has_graph = show_graphs ? p_counters.contains(p.pid) : false;
if (show_graphs and ((p.cpu_p > 0 and not has_graph) or (not data_same and has_graph))) {
if (not has_graph) {
p_graphs[p.pid] = Draw::Graph{5, 1, "", {}, graph_symbol};
Expand Down
10 changes: 5 additions & 5 deletions src/btop_draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ namespace Draw {

//* Create a box and return as a string
string createBox(const int x, const int y, const int width,
const int height, string line_color="", const bool fill=false,
const string title="", const string title2="", const int num=0);
const int height, string line_color = "", bool fill = false,
const string title = "", const string title2 = "", const int num = 0);

bool update_clock(bool force=false);
bool update_clock(bool force = false);

//* Class holding a percentage meter
class Meter {
Expand All @@ -93,7 +93,7 @@ namespace Draw {
array<string, 101> cache;
public:
Meter();
Meter(const int width, const string& color_gradient, const bool invert = false);
Meter(const int width, const string& color_gradient, bool invert = false);

//* Return a string representation of the meter with given value
string operator()(int value);
Expand Down Expand Up @@ -123,7 +123,7 @@ namespace Draw {
long long max_value=0, long long offset=0);

//* Add last value from back of <data> and return string representation of graph
string& operator()(const deque<long long>& data, const bool data_same=false);
string& operator()(const deque<long long>& data, bool data_same=false);

//* Return string representation of graph
string& operator()();
Expand Down
6 changes: 3 additions & 3 deletions src/btop_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using namespace Tools;


namespace Proc {
void proc_sorter(vector<proc_info>& proc_vec, const string& sorting, const bool reverse, const bool tree) {
void proc_sorter(vector<proc_info>& proc_vec, const string& sorting, bool reverse, bool tree) {
if (reverse) {
switch (v_index(sort_vector, sorting)) {
case 0: rng::stable_sort(proc_vec, rng::less{}, &proc_info::pid); break;
Expand Down Expand Up @@ -70,7 +70,7 @@ namespace Proc {
}
}

void tree_sort(vector<tree_proc>& proc_vec, const string& sorting, const bool reverse, int& c_index, const int index_max, const bool collapsed) {
void tree_sort(vector<tree_proc>& proc_vec, const string& sorting, bool reverse, int& c_index, const int index_max, bool collapsed) {
if (proc_vec.size() > 1) {
if (reverse) {
switch (v_index(sort_vector, sorting)) {
Expand Down Expand Up @@ -99,7 +99,7 @@ namespace Proc {
}

void _tree_gen(proc_info& cur_proc, vector<proc_info>& in_procs, vector<tree_proc>& out_procs,
int cur_depth, const bool collapsed, const string& filter, bool found, const bool no_update, const bool should_filter) {
int cur_depth, bool collapsed, const string& filter, bool found, bool no_update, bool should_filter) {
auto cur_pos = out_procs.size();
bool filtering = false;

Expand Down
27 changes: 13 additions & 14 deletions src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace Runner {
extern bool pause_output;
extern string debug_bg;

void run(const string& box="", const bool no_update=false, const bool force_redraw=false);
void run(const string& box="", bool no_update = false, bool force_redraw = false);
void stop();

}
Expand Down Expand Up @@ -115,10 +115,10 @@ namespace Cpu {
};

//* Collect cpu stats and temperatures
auto collect(const bool no_update=false) -> cpu_info&;
auto collect(bool no_update = false) -> cpu_info&;

//* Draw contents of cpu box using <cpu> as source
string draw(const cpu_info& cpu, const bool force_redraw=false, const bool data_same=false);
string draw(const cpu_info& cpu, bool force_redraw = false, bool data_same = false);

//* Parse /proc/cpu info for mapping of core ids
auto get_core_mapping() -> unordered_flat_map<int, int>;
Expand Down Expand Up @@ -168,10 +168,10 @@ namespace Mem {
uint64_t get_totalMem();

//* Collect mem & disks stats
auto collect(const bool no_update=false) -> mem_info&;
auto collect(bool no_update = false) -> mem_info&;

//* Draw contents of mem box using <mem> as source
string draw(const mem_info& mem, const bool force_redraw=false, const bool data_same=false);
string draw(const mem_info& mem, bool force_redraw = false, bool data_same = false);

}

Expand Down Expand Up @@ -204,10 +204,10 @@ namespace Net {
extern unordered_flat_map<string, net_info> current_net;

//* Collect net upload/download stats
auto collect(const bool no_update=false) -> net_info&;
auto collect(bool no_update=false) -> net_info&;

//* Draw contents of net box using <net> as source
string draw(const net_info& net, const bool force_redraw=false, const bool data_same=false);
string draw(const net_info& net, bool force_redraw = false, bool data_same = false);
}

namespace Proc {
Expand Down Expand Up @@ -287,29 +287,28 @@ namespace Proc {
extern detail_container detailed;

//* Collect and sort process information from /proc
auto collect(const bool no_update=false) -> vector<proc_info>&;
auto collect(bool no_update = false) -> vector<proc_info>&;

//* Update current selection and view, returns -1 if no change otherwise the current selection
int selection(const string& cmd_key);

//* Draw contents of proc box using <plist> as data source
string draw(const vector<proc_info>& plist, const bool force_redraw=false, const bool data_same=false);
string draw(const vector<proc_info>& plist, bool force_redraw = false, bool data_same = false);

struct tree_proc {
std::reference_wrapper<proc_info> entry;
vector<tree_proc> children;
};

//* Sort vector of proc_info's
void proc_sorter(vector<proc_info>& proc_vec, const string& sorting,
const bool reverse, const bool tree = false);
void proc_sorter(vector<proc_info>& proc_vec, const string& sorting, bool reverse, bool tree = false);

//* Recursive sort of process tree
void tree_sort(vector<tree_proc>& proc_vec, const string& sorting,
const bool reverse, int& c_index, const int index_max, const bool collapsed = false);
bool reverse, int& c_index, const int index_max, bool collapsed = false);

//* Generate process tree list
void _tree_gen(proc_info& cur_proc, vector<proc_info>& in_procs, vector<tree_proc>& out_procs,
int cur_depth, const bool collapsed, const string& filter,
bool found=false, const bool no_update=false, const bool should_filter=false);
int cur_depth, bool collapsed, const string& filter,
bool found = false, bool no_update = false, bool should_filter = false);
}
Loading

0 comments on commit d58a17a

Please sign in to comment.