aboutsummaryrefslogtreecommitdiff
path: root/src/nix/search.cc
diff options
context:
space:
mode:
authorFishhh <hubert.gluchowski19@gmail.com>2022-06-05 18:45:58 +0200
committerHubert GÅ‚uchowski <hubert.gluchowski19@gmail.com>2022-06-07 18:25:48 +0200
commitb42358b9bec12dfdc419136f32ded2a4f7d7dea7 (patch)
tree3f0983ceed54905ddfa3b3884db67b396f41e34f /src/nix/search.cc
parentd137ceccefe08250106dcede1f30c270b0f9cf19 (diff)
Add `--exclude` flag to `nix search`
If a package's attribute path, description or name contains matches for any of the regexes specified via `-e` or `--exclude` that package is excluded from the final output.
Diffstat (limited to 'src/nix/search.cc')
-rw-r--r--src/nix/search.cc23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/nix/search.cc b/src/nix/search.cc
index 87dc1c0de..62ad98999 100644
--- a/src/nix/search.cc
+++ b/src/nix/search.cc
@@ -18,16 +18,24 @@ using namespace nix;
std::string wrap(std::string prefix, std::string s)
{
- return prefix + s + ANSI_NORMAL;
+ return concatStrings(prefix, s, ANSI_NORMAL);
}
struct CmdSearch : InstallableCommand, MixJSON
{
std::vector<std::string> res;
+ std::vector<std::string> excludeRes;
CmdSearch()
{
expectArgs("regex", &res);
+ addFlag(Flag {
+ .longName = "exclude",
+ .shortName = 'e',
+ .description = "Hide packages whose attribute path, name or description contain *regex*.",
+ .labels = {"regex"},
+ .handler = Handler(&excludeRes),
+ });
}
std::string description() override
@@ -62,11 +70,16 @@ struct CmdSearch : InstallableCommand, MixJSON
res.push_back("^");
std::vector<std::regex> regexes;
+ std::vector<std::regex> excludeRegexes;
regexes.reserve(res.size());
+ excludeRegexes.reserve(excludeRes.size());
for (auto & re : res)
regexes.push_back(std::regex(re, std::regex::extended | std::regex::icase));
+ for (auto & re : excludeRes)
+ excludeRegexes.emplace_back(re, std::regex::extended | std::regex::icase);
+
auto state = getEvalState();
auto jsonOut = json ? std::make_unique<JSONObject>(std::cout) : nullptr;
@@ -106,6 +119,14 @@ struct CmdSearch : InstallableCommand, MixJSON
std::vector<std::smatch> nameMatches;
bool found = false;
+ for (auto & regex : excludeRegexes) {
+ if (
+ std::regex_search(attrPath2, regex)
+ || std::regex_search(name.name, regex)
+ || std::regex_search(description, regex))
+ return;
+ }
+
for (auto & regex : regexes) {
found = false;
auto addAll = [&found](std::sregex_iterator it, std::vector<std::smatch> & vec) {