Skip to content

Commit f46a133

Browse files
authored
Merge pull request #1130 from imwints/push-pqvlpwzmzlyu
2 parents fd35ac1 + 2538d89 commit f46a133

12 files changed

Lines changed: 112 additions & 84 deletions

src/btop.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void term_resize(bool force) {
174174
#else
175175
if (intKey > 0 and intKey < 5) {
176176
#endif
177-
auto box = all_boxes.at(intKey);
177+
const auto& box = all_boxes.at(intKey);
178178
Config::current_preset = -1;
179179
Config::toggle_box(box);
180180
boxes = Config::getS("shown_boxes");

src/btop_draw.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ indent = tab
1616
tab-size = 4
1717
*/
1818

19-
#include <array>
2019
#include <algorithm>
20+
#include <array>
2121
#include <cmath>
2222
#include <ranges>
2323
#include <stdexcept>
2424
#include <string>
25+
#include <string_view>
2526
#include <utility>
2627

2728
#include "btop_draw.hpp"
@@ -31,6 +32,7 @@ tab-size = 4
3132
#include "btop_tools.hpp"
3233
#include "btop_input.hpp"
3334
#include "btop_menu.hpp"
35+
#include <fmt/format.h>
3436

3537

3638
using std::array;
@@ -146,12 +148,12 @@ namespace Draw {
146148
}
147149

148150
TextEdit::TextEdit() {}
149-
TextEdit::TextEdit(string text, bool numeric) : numeric(numeric), text(text) {
151+
TextEdit::TextEdit(string text, bool numeric) : numeric(numeric), text(std::move(text)) {
150152
pos = this->text.size();
151153
upos = ulen(this->text);
152154
}
153155

154-
bool TextEdit::command(const string& key) {
156+
bool TextEdit::command(const std::string_view key) {
155157
if (key == "left" and upos > 0) {
156158
upos--;
157159
pos = uresize(text, upos).size();
@@ -193,7 +195,7 @@ namespace Draw {
193195
upos++;
194196
}
195197
else {
196-
const string first = uresize(text, upos) + key;
198+
const auto first = fmt::format("{}{}", uresize(text, upos), key);
197199
text = first + text.substr(pos);
198200
upos++;
199201
pos = first.size();
@@ -245,9 +247,10 @@ namespace Draw {
245247
this->text.clear();
246248
}
247249

248-
string createBox(const int x, const int y, const int width,
249-
const int height, string line_color, bool fill,
250-
const string title, const string title2, const int num) {
250+
string createBox(
251+
const int x, const int y, const int width, const int height, string line_color, bool fill, const std::string_view title,
252+
const std::string_view title2, const int num
253+
) {
251254
string out;
252255

253256
if (line_color.empty())
@@ -283,12 +286,16 @@ namespace Draw {
283286

284287
//? Draw titles if defined
285288
if (not title.empty()) {
286-
out += Mv::to(y, x + 2) + Symbols::title_left + Fx::b + numbering + Theme::c("title") + title
287-
+ Fx::ub + line_color + Symbols::title_right;
289+
out += fmt::format(
290+
"{}{}{}{}{}{}{}{}{}", Mv::to(y, x + 2), Symbols::title_left, Fx::b, numbering, Theme::c("title"), title, Fx::ub,
291+
line_color, Symbols::title_right
292+
);
288293
}
289294
if (not title2.empty()) {
290-
out += Mv::to(y + height - 1, x + 2) + Symbols::title_left_down + Fx::b + numbering + Theme::c("title") + title2
291-
+ Fx::ub + line_color + Symbols::title_right_down;
295+
out += fmt::format(
296+
"{}{}{}{}{}{}{}{}{}", Mv::to(y, x + 2), Symbols::title_left, Fx::b, numbering, Theme::c("title"), title2, Fx::ub,
297+
line_color, Symbols::title_right_down
298+
);
292299
}
293300

294301
return out + Fx::reset + Mv::to(y + 1, x + 1);
@@ -1229,7 +1236,7 @@ namespace Mem {
12291236
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");
12301237
vector<string> comb_names (mem_names.begin(), mem_names.end());
12311238
if (show_swap and has_swap and not swap_disk) comb_names.insert(comb_names.end(), swap_names.begin(), swap_names.end());
1232-
for (auto name : comb_names) {
1239+
for (const auto& name : comb_names) {
12331240
if (cy > height - 4) break;
12341241
string title;
12351242
if (name == "swap_used") {
@@ -1482,7 +1489,7 @@ namespace Proc {
14821489

14831490
string box;
14841491

1485-
int selection(const string& cmd_key) {
1492+
int selection(const std::string_view cmd_key) {
14861493
auto start = Config::getI("proc_start");
14871494
auto selected = Config::getI("proc_selected");
14881495
auto last_selected = Config::getI("proc_last_selected");
@@ -1526,7 +1533,7 @@ namespace Proc {
15261533
if (selected > 0) selected = select_max;
15271534
}
15281535
else if (cmd_key.starts_with("mousey")) {
1529-
int mouse_y = std::stoi(cmd_key.substr(6));
1536+
int mouse_y = std::atoi(cmd_key.substr(6).data());
15301537
start = clamp((int)round((double)mouse_y * (numpids - select_max - 2) / (select_max - 2)), 0, max(0, numpids - select_max));
15311538
}
15321539

src/btop_draw.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ tab-size = 4
1818

1919
#pragma once
2020

21-
#include <string>
22-
#include <vector>
2321
#include <array>
24-
#include <unordered_map>
2522
#include <deque>
23+
#include <string>
24+
#include <string_view>
25+
#include <unordered_map>
26+
#include <vector>
2627

2728
using std::array;
2829
using std::deque;
@@ -72,15 +73,16 @@ namespace Draw {
7273
string text;
7374
TextEdit();
7475
explicit TextEdit(string text, bool numeric=false);
75-
bool command(const string& key);
76+
bool command(const std::string_view key);
7677
string operator()(const size_t limit=0);
7778
void clear();
7879
};
7980

8081
//* Create a box and return as a string
81-
string createBox(const int x, const int y, const int width,
82-
const int height, string line_color = "", bool fill = false,
83-
const string title = "", const string title2 = "", const int num = 0);
82+
string createBox(
83+
const int x, const int y, const int width, const int height, string line_color = "", bool fill = false,
84+
const std::string_view title = "", const std::string_view title2 = "", const int num = 0
85+
);
8486

8587
bool update_clock(bool force = false);
8688

src/btop_input.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ tab-size = 4
2121
#include <vector>
2222
#include <thread>
2323
#include <mutex>
24+
#include <fmt/format.h>
2425
#include <signal.h>
2526
#include <sys/select.h>
2627
#include <utility>
@@ -203,7 +204,7 @@ namespace Input {
203204
// do not need it, actually
204205
}
205206

206-
void process(const string& key) {
207+
void process(const std::string_view key) {
207208
if (key.empty()) return;
208209
try {
209210
auto filtering = Config::getB("proc_filtering");
@@ -213,7 +214,7 @@ namespace Input {
213214
//? Global input actions
214215
if (not filtering) {
215216
bool keep_going = false;
216-
if (str_to_lower(key) == "q") {
217+
if (key == "q") {
217218
clean_quit(0);
218219
}
219220
else if (is_in(key, "escape", "m")) {
@@ -229,7 +230,7 @@ namespace Input {
229230
return;
230231
}
231232
else if (key.size() == 1 and isint(key)) {
232-
auto intKey = stoi(key);
233+
auto intKey = std::atoi(key.data());
233234
#ifdef GPU_SUPPORT
234235
static const array<string, 10> boxes = {"gpu5", "cpu", "mem", "net", "proc", "gpu0", "gpu1", "gpu2", "gpu3", "gpu4"};
235236
if ((intKey == 0 and Gpu::count < 5) or (intKey >= 5 and intKey - 4 > Gpu::count))
@@ -540,8 +541,7 @@ namespace Input {
540541
}
541542

542543
catch (const std::exception& e) {
543-
throw std::runtime_error("Input::process(\"" + key + "\") : " + string{e.what()});
544+
throw std::runtime_error { fmt::format(R"(Input::process("{}"))", e.what()) };
544545
}
545546
}
546-
547547
}

src/btop_input.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ tab-size = 4
1818

1919
#pragma once
2020

21-
#include <string>
22-
#include <atomic>
2321
#include <array>
24-
#include <unordered_map>
22+
#include <atomic>
2523
#include <deque>
24+
#include <string>
25+
#include <string_view>
26+
#include <unordered_map>
2627

2728
using std::array;
2829
using std::atomic;
@@ -72,6 +73,6 @@ namespace Input {
7273
void clear();
7374

7475
//* Process actions for input <key>
75-
void process(const string& key);
76+
void process(const std::string_view key);
7677

7778
}

src/btop_menu.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ indent = tab
1616
tab-size = 4
1717
*/
1818

19-
#include <deque>
20-
#include <unordered_map>
21-
#include <array>
22-
#include <signal.h>
23-
#include <errno.h>
24-
#include <cmath>
25-
#include <filesystem>
26-
2719
#include "btop_menu.hpp"
28-
#include "btop_tools.hpp"
20+
2921
#include "btop_config.hpp"
30-
#include "btop_theme.hpp"
3122
#include "btop_draw.hpp"
3223
#include "btop_shared.hpp"
24+
#include "btop_theme.hpp"
25+
#include "btop_tools.hpp"
26+
27+
#include <errno.h>
28+
#include <signal.h>
29+
30+
#include <array>
31+
#include <cmath>
32+
#include <filesystem>
33+
#include <unordered_map>
34+
#include <utility>
3335

3436
using std::array;
3537
using std::ceil;
@@ -798,7 +800,7 @@ namespace Menu {
798800
};
799801

800802
msgBox::msgBox() {}
801-
msgBox::msgBox(int width, int boxtype, const vector<string>& content, string title)
803+
msgBox::msgBox(int width, int boxtype, const vector<string>& content, const std::string_view title)
802804
: width(width), boxtype(boxtype) {
803805
auto tty_mode = Config::getB("tty_mode");
804806
auto rounded = Config::getB("rounded_corners");
@@ -838,7 +840,7 @@ namespace Menu {
838840
}
839841

840842
//? Process input
841-
int msgBox::input(string key) {
843+
int msgBox::input(const string& key) {
842844
if (key.empty()) return Invalid;
843845

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

1600-
void process(string key) {
1602+
void process(const std::string_view key) {
16011603
if (menuMask.none()) {
16021604
Menu::active = false;
16031605
Global::overlay.clear();
@@ -1627,7 +1629,7 @@ static int optionsMenu(const string& key) {
16271629

16281630
}
16291631

1630-
auto retCode = menuFunc.at(currentMenu)(key);
1632+
auto retCode = menuFunc.at(currentMenu)(key.data());
16311633
if (retCode == Closed) {
16321634
menuMask.reset(currentMenu);
16331635
mouse_mappings.clear();

src/btop_menu.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ tab-size = 4
1818

1919
#pragma once
2020

21-
#include <string>
2221
#include <atomic>
23-
#include <vector>
2422
#include <bitset>
23+
#include <string>
24+
#include <string_view>
25+
#include <vector>
2526

2627
#include "btop_input.hpp"
2728

@@ -61,13 +62,13 @@ namespace Menu {
6162
Select
6263
};
6364
msgBox();
64-
msgBox(int width, int boxtype, const vector<string>& content, string title);
65+
msgBox(int width, int boxtype, const vector<string>& content, std::string_view title);
6566

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

6970
//? Process input and returns value from enum Ret
70-
int input(string key);
71+
int input(const string& key);
7172

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

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

9293
//* Show a menu from enum Menu::Menus
9394
void show(int menu, int signal=-1);

src/btop_shared.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace Cpu {
4242
name = "Ryzen";
4343
int tokens = 0;
4444
for (auto i = ryz_pos + 1; i < name_vec.size() && tokens < 2; i++) {
45-
string p = name_vec.at(i);
45+
const std::string& p = name_vec.at(i);
4646
if (p != "AI" && p != "PRO")
4747
tokens++;
4848
name += " " + p;

src/btop_shared.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ tab-size = 4
2323
#include <deque>
2424
#include <filesystem>
2525
#include <string>
26+
#include <string_view>
2627
#include <tuple>
27-
#include <vector>
2828
#include <unordered_map>
29+
#include <vector>
30+
2931
#include <unistd.h>
3032

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

6971
namespace Runner {
70-
7172
extern atomic<bool> active;
7273
extern atomic<bool> reading;
7374
extern atomic<bool> stopping;
@@ -414,7 +415,7 @@ namespace Proc {
414415
auto collect(bool no_update = false) -> vector<proc_info>&;
415416

416417
//* Update current selection and view, returns -1 if no change otherwise the current selection
417-
int selection(const string& cmd_key);
418+
int selection(const std::string_view cmd_key);
418419

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

0 commit comments

Comments
 (0)