Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void term_resize(bool force) {
#else
if (intKey > 0 and intKey < 5) {
#endif
auto box = all_boxes.at(intKey);
const auto& box = all_boxes.at(intKey);
Config::current_preset = -1;
Config::toggle_box(box);
boxes = Config::getS("shown_boxes");
Expand Down
35 changes: 21 additions & 14 deletions src/btop_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ indent = tab
tab-size = 4
*/

#include <array>
#include <algorithm>
#include <array>
#include <cmath>
#include <ranges>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>

#include "btop_draw.hpp"
Expand All @@ -31,6 +32,7 @@ tab-size = 4
#include "btop_tools.hpp"
#include "btop_input.hpp"
#include "btop_menu.hpp"
#include <fmt/format.h>


using std::array;
Expand Down Expand Up @@ -146,12 +148,12 @@ namespace Draw {
}

TextEdit::TextEdit() {}
TextEdit::TextEdit(string text, bool numeric) : numeric(numeric), text(text) {
TextEdit::TextEdit(string text, bool numeric) : numeric(numeric), text(std::move(text)) {
pos = this->text.size();
upos = ulen(this->text);
}

bool TextEdit::command(const string& key) {
bool TextEdit::command(const std::string_view key) {
if (key == "left" and upos > 0) {
upos--;
pos = uresize(text, upos).size();
Expand Down Expand Up @@ -193,7 +195,7 @@ namespace Draw {
upos++;
}
else {
const string first = uresize(text, upos) + key;
const auto first = fmt::format("{}{}", uresize(text, upos), key);
text = first + text.substr(pos);
upos++;
pos = first.size();
Expand Down Expand Up @@ -245,9 +247,10 @@ namespace Draw {
this->text.clear();
}

string createBox(const int x, const int y, const int width,
const int height, string line_color, bool fill,
const string title, const string title2, const int num) {
string createBox(
const int x, const int y, const int width, const int height, string line_color, bool fill, const std::string_view title,
const std::string_view title2, const int num
) {
string out;

if (line_color.empty())
Expand Down Expand Up @@ -283,12 +286,16 @@ namespace Draw {

//? Draw titles if defined
if (not title.empty()) {
out += Mv::to(y, x + 2) + Symbols::title_left + Fx::b + numbering + Theme::c("title") + title
+ Fx::ub + line_color + Symbols::title_right;
out += fmt::format(
"{}{}{}{}{}{}{}{}{}", Mv::to(y, x + 2), Symbols::title_left, Fx::b, numbering, Theme::c("title"), title, Fx::ub,
line_color, Symbols::title_right
);
}
if (not title2.empty()) {
out += Mv::to(y + height - 1, x + 2) + Symbols::title_left_down + Fx::b + numbering + Theme::c("title") + title2
+ Fx::ub + line_color + Symbols::title_right_down;
out += fmt::format(
"{}{}{}{}{}{}{}{}{}", Mv::to(y, x + 2), Symbols::title_left, Fx::b, numbering, Theme::c("title"), title2, Fx::ub,
line_color, Symbols::title_right_down
);
Comment thread
aristocratos marked this conversation as resolved.
}

return out + Fx::reset + Mv::to(y + 1, x + 1);
Expand Down Expand Up @@ -1229,7 +1236,7 @@ namespace Mem {
out += Mv::to(y + 1, x + 2) + Theme::c("title") + Fx::b + "Total:" + rjust(floating_humanizer(totalMem), mem_width - 9) + Fx::ub + Theme::c("main_fg");
vector<string> comb_names (mem_names.begin(), mem_names.end());
if (show_swap and has_swap and not swap_disk) comb_names.insert(comb_names.end(), swap_names.begin(), swap_names.end());
for (auto name : comb_names) {
for (const auto& name : comb_names) {
if (cy > height - 4) break;
string title;
if (name == "swap_used") {
Expand Down Expand Up @@ -1482,7 +1489,7 @@ namespace Proc {

string box;

int selection(const string& cmd_key) {
int selection(const std::string_view cmd_key) {
auto start = Config::getI("proc_start");
auto selected = Config::getI("proc_selected");
auto last_selected = Config::getI("proc_last_selected");
Expand Down Expand Up @@ -1526,7 +1533,7 @@ namespace Proc {
if (selected > 0) selected = select_max;
}
else if (cmd_key.starts_with("mousey")) {
int mouse_y = std::stoi(cmd_key.substr(6));
int mouse_y = std::atoi(cmd_key.substr(6).data());
start = clamp((int)round((double)mouse_y * (numpids - select_max - 2) / (select_max - 2)), 0, max(0, numpids - select_max));
}

Expand Down
16 changes: 9 additions & 7 deletions src/btop_draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ tab-size = 4

#pragma once

#include <string>
#include <vector>
#include <array>
#include <unordered_map>
#include <deque>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

using std::array;
using std::deque;
Expand Down Expand Up @@ -72,15 +73,16 @@ namespace Draw {
string text;
TextEdit();
explicit TextEdit(string text, bool numeric=false);
bool command(const string& key);
bool command(const std::string_view key);
string operator()(const size_t limit=0);
void clear();
};

//* 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 = "", bool fill = false,
const string title = "", const string title2 = "", const int num = 0);
string createBox(
const int x, const int y, const int width, const int height, string line_color = "", bool fill = false,
const std::string_view title = "", const std::string_view title2 = "", const int num = 0
);

bool update_clock(bool force = false);

Expand Down
10 changes: 5 additions & 5 deletions src/btop_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tab-size = 4
#include <vector>
#include <thread>
#include <mutex>
#include <fmt/format.h>
#include <signal.h>
#include <sys/select.h>
#include <utility>
Expand Down Expand Up @@ -203,7 +204,7 @@ namespace Input {
// do not need it, actually
}

void process(const string& key) {
void process(const std::string_view key) {
if (key.empty()) return;
try {
auto filtering = Config::getB("proc_filtering");
Expand All @@ -213,7 +214,7 @@ namespace Input {
//? Global input actions
if (not filtering) {
bool keep_going = false;
if (str_to_lower(key) == "q") {
if (key == "q") {
clean_quit(0);
}
else if (is_in(key, "escape", "m")) {
Expand All @@ -229,7 +230,7 @@ namespace Input {
return;
}
else if (key.size() == 1 and isint(key)) {
auto intKey = stoi(key);
auto intKey = std::atoi(key.data());
#ifdef GPU_SUPPORT
static const array<string, 10> boxes = {"gpu5", "cpu", "mem", "net", "proc", "gpu0", "gpu1", "gpu2", "gpu3", "gpu4"};
if ((intKey == 0 and Gpu::count < 5) or (intKey >= 5 and intKey - 4 > Gpu::count))
Expand Down Expand Up @@ -540,8 +541,7 @@ namespace Input {
}

catch (const std::exception& e) {
throw std::runtime_error("Input::process(\"" + key + "\") : " + string{e.what()});
throw std::runtime_error { fmt::format(R"(Input::process("{}"))", e.what()) };
}
}

}
9 changes: 5 additions & 4 deletions src/btop_input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ tab-size = 4

#pragma once

#include <string>
#include <atomic>
#include <array>
#include <unordered_map>
#include <atomic>
#include <deque>
#include <string>
#include <string_view>
#include <unordered_map>

using std::array;
using std::atomic;
Expand Down Expand Up @@ -72,6 +73,6 @@ namespace Input {
void clear();

//* Process actions for input <key>
void process(const string& key);
void process(const std::string_view key);

}
30 changes: 16 additions & 14 deletions src/btop_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ indent = tab
tab-size = 4
*/

#include <deque>
#include <unordered_map>
#include <array>
#include <signal.h>
#include <errno.h>
#include <cmath>
#include <filesystem>

#include "btop_menu.hpp"
#include "btop_tools.hpp"

#include "btop_config.hpp"
#include "btop_theme.hpp"
#include "btop_draw.hpp"
#include "btop_shared.hpp"
#include "btop_theme.hpp"
#include "btop_tools.hpp"

#include <errno.h>
#include <signal.h>

#include <array>
#include <cmath>
#include <filesystem>
#include <unordered_map>
#include <utility>

using std::array;
using std::ceil;
Expand Down Expand Up @@ -798,7 +800,7 @@ namespace Menu {
};

msgBox::msgBox() {}
msgBox::msgBox(int width, int boxtype, const vector<string>& content, string title)
msgBox::msgBox(int width, int boxtype, const vector<string>& content, const std::string_view title)
: width(width), boxtype(boxtype) {
auto tty_mode = Config::getB("tty_mode");
auto rounded = Config::getB("rounded_corners");
Expand Down Expand Up @@ -838,7 +840,7 @@ namespace Menu {
}

//? Process input
int msgBox::input(string key) {
int msgBox::input(const string& key) {
if (key.empty()) return Invalid;

if (is_in(key, "escape", "backspace", "q") or key == "button2") {
Expand Down Expand Up @@ -1597,7 +1599,7 @@ static int optionsMenu(const string& key) {
};
bitset<8> menuMask;

void process(string key) {
void process(const std::string_view key) {
if (menuMask.none()) {
Menu::active = false;
Global::overlay.clear();
Expand Down Expand Up @@ -1627,7 +1629,7 @@ static int optionsMenu(const string& key) {

}

auto retCode = menuFunc.at(currentMenu)(key);
auto retCode = menuFunc.at(currentMenu)(key.data());
if (retCode == Closed) {
menuMask.reset(currentMenu);
mouse_mappings.clear();
Expand Down
11 changes: 6 additions & 5 deletions src/btop_menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ tab-size = 4

#pragma once

#include <string>
#include <atomic>
#include <vector>
#include <bitset>
#include <string>
#include <string_view>
#include <vector>

#include "btop_input.hpp"

Expand Down Expand Up @@ -61,13 +62,13 @@ namespace Menu {
Select
};
msgBox();
msgBox(int width, int boxtype, const vector<string>& content, string title);
msgBox(int width, int boxtype, const vector<string>& content, std::string_view title);

//? Draw and return box as a string
string operator()();

//? Process input and returns value from enum Ret
int input(string key);
int input(const string& key);

//? Clears content vector and private strings
void clear();
Expand All @@ -87,7 +88,7 @@ namespace Menu {
};

//* Handles redirection of input for menu functions and handles return codes
void process(string key="");
void process(const std::string_view key = "");

//* Show a menu from enum Menu::Menus
void show(int menu, int signal=-1);
Expand Down
2 changes: 1 addition & 1 deletion src/btop_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Cpu {
name = "Ryzen";
int tokens = 0;
for (auto i = ryz_pos + 1; i < name_vec.size() && tokens < 2; i++) {
string p = name_vec.at(i);
const std::string& p = name_vec.at(i);
if (p != "AI" && p != "PRO")
tokens++;
name += " " + p;
Expand Down
7 changes: 4 additions & 3 deletions src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ tab-size = 4
#include <deque>
#include <filesystem>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>
#include <unordered_map>
#include <vector>

#include <unistd.h>

// From `man 3 getifaddrs`: <net/if.h> must be included before <ifaddrs.h>
Expand Down Expand Up @@ -67,7 +69,6 @@ namespace Global {
}

namespace Runner {

extern atomic<bool> active;
extern atomic<bool> reading;
extern atomic<bool> stopping;
Expand Down Expand Up @@ -414,7 +415,7 @@ namespace Proc {
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);
int selection(const std::string_view cmd_key);

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