diff options
author | Steffen <steffen.winter@proton.me> | 2024-03-20 15:21:22 +0100 |
---|---|---|
committer | Steffen Winter <steffen.winter@proton.me> | 2024-04-30 17:09:15 +0200 |
commit | 255b77756305cf807a6d2b040ab71788f0139f0f (patch) | |
tree | 3b92ceaeadf7d18c76868ffcfed18d3fda9b045e /src | |
parent | 8c1a8ab4fac34b9439ba225544e463c61bef7b24 (diff) |
Add regex filtering
Filters starting with '!' will try to match processes pid, name, cmd and
user with extended regex as defined by the C++ standard. A single '!'
will not filter.
Diffstat (limited to 'src')
-rw-r--r-- | src/btop_shared.cpp | 23 | ||||
-rw-r--r-- | src/btop_shared.hpp | 2 | ||||
-rw-r--r-- | src/freebsd/btop_collect.cpp | 17 | ||||
-rw-r--r-- | src/linux/btop_collect.cpp | 17 | ||||
-rw-r--r-- | src/openbsd/btop_collect.cpp | 17 | ||||
-rw-r--r-- | src/osx/btop_collect.cpp | 2 |
6 files changed, 40 insertions, 38 deletions
diff --git a/src/btop_shared.cpp b/src/btop_shared.cpp index b159a97..f4d86c9 100644 --- a/src/btop_shared.cpp +++ b/src/btop_shared.cpp @@ -17,6 +17,8 @@ tab-size = 4 */ #include <ranges> +#include <regex> +#include <string> #include "btop_config.hpp" #include "btop_shared.hpp" @@ -111,6 +113,22 @@ namespace Proc { } } + bool matches_filter(const proc_info& proc, const std::string& filter) { + if (filter.starts_with("!")) { + if (filter.size() == 1) { + return true; + } + std::regex regex{filter.substr(1), std::regex::extended}; + return std::regex_search(std::to_string(proc.pid), regex) || + std::regex_search(proc.name, regex) || std::regex_match(proc.cmd, regex) || + std::regex_search(proc.user, regex); + } else { + return s_contains(std::to_string(proc.pid), filter) || + s_contains_ic(proc.name, filter) || s_contains_ic(proc.cmd, filter) || + s_contains_ic(proc.user, filter); + } + } + void _tree_gen(proc_info& cur_proc, vector<proc_info>& in_procs, vector<tree_proc>& out_procs, int cur_depth, bool collapsed, const string& filter, bool found, bool no_update, bool should_filter) { auto cur_pos = out_procs.size(); @@ -118,10 +136,7 @@ namespace Proc { //? If filtering, include children of matching processes if (not found and (should_filter or not filter.empty())) { - if (not s_contains(std::to_string(cur_proc.pid), filter) - and not s_contains_ic(cur_proc.name, filter) - and not s_contains_ic(cur_proc.cmd, filter) - and not s_contains_ic(cur_proc.user, filter)) { + if (!matches_filter(cur_proc, filter)) { filtering = true; cur_proc.filtered = true; filter_found++; diff --git a/src/btop_shared.hpp b/src/btop_shared.hpp index f927e05..0a8f452 100644 --- a/src/btop_shared.hpp +++ b/src/btop_shared.hpp @@ -424,6 +424,8 @@ namespace Proc { void tree_sort(vector<tree_proc>& proc_vec, const string& sorting, bool reverse, int& c_index, const int index_max, bool collapsed = false); + bool matches_filter(const proc_info& proc, const std::string& filter); + //* Generate process tree list void _tree_gen(proc_info& cur_proc, vector<proc_info>& in_procs, vector<tree_proc>& out_procs, int cur_depth, bool collapsed, const string& filter, diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index a99f8df..b4034c7 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -1239,18 +1239,13 @@ namespace Proc { filter_found = 0; for (auto& p : current_procs) { if (not tree and not filter.empty()) { - if (not s_contains_ic(to_string(p.pid), filter) - and not s_contains_ic(p.name, filter) - and not s_contains_ic(p.cmd, filter) - and not s_contains_ic(p.user, filter)) { - p.filtered = true; - filter_found++; - } - else { - p.filtered = false; - } + if (!matches_filter(p, filter)) { + p.filtered = true; + filter_found++; + } else { + p.filtered = false; } - else { + } else { p.filtered = false; } } diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index cd52200..6154725 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -2797,18 +2797,13 @@ namespace Proc { filter_found = 0; for (auto& p : current_procs) { if (not tree and not filter.empty()) { - if (not s_contains_ic(to_string(p.pid), filter) - and not s_contains_ic(p.name, filter) - and not s_contains_ic(p.cmd, filter) - and not s_contains_ic(p.user, filter)) { - p.filtered = true; - filter_found++; - } - else { - p.filtered = false; - } + if (!matches_filter(p, filter)) { + p.filtered = true; + filter_found++; + } else { + p.filtered = false; } - else { + } else { p.filtered = false; } } diff --git a/src/openbsd/btop_collect.cpp b/src/openbsd/btop_collect.cpp index 9b3e1f2..a8c395e 100644 --- a/src/openbsd/btop_collect.cpp +++ b/src/openbsd/btop_collect.cpp @@ -1171,18 +1171,13 @@ namespace Proc { filter_found = 0; for (auto& p : current_procs) { if (not tree and not filter.empty()) { - if (not s_contains_ic(to_string(p.pid), filter) - and not s_contains_ic(p.name, filter) - and not s_contains_ic(p.cmd, filter) - and not s_contains_ic(p.user, filter)) { - p.filtered = true; - filter_found++; - } - else { - p.filtered = false; - } + if (!matches_filter(p, filter)) { + p.filtered = true; + filter_found++; + } else { + p.filtered = false; } - else { + } else { p.filtered = false; } } diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index 1b752ad..8c65705 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -1305,7 +1305,7 @@ namespace Proc { filter_found = 0; for (auto &p : current_procs) { if (not tree and not filter.empty()) { - if (not s_contains_ic(to_string(p.pid), filter) and not s_contains_ic(p.name, filter) and not s_contains_ic(p.cmd, filter) and not s_contains_ic(p.user, filter)) { + if (!matches_filter(p, filter)) { p.filtered = true; filter_found++; } else { |