mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-30 09:36:15 +02:00
Make nix search
highlight all matches of a regex
This commit is contained in:
parent
92e8230215
commit
87fdd23025
1 changed files with 12 additions and 14 deletions
|
@ -10,6 +10,7 @@
|
||||||
#include "eval-cache.hh"
|
#include "eval-cache.hh"
|
||||||
#include "attr-path.hh"
|
#include "attr-path.hh"
|
||||||
|
|
||||||
|
#include <iterator>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
|
@ -48,7 +49,7 @@ std::string hilite_all(const std::string &s, std::vector<std::smatch> matches, s
|
||||||
for (size_t i = 0; i < matches.size(); i++) {
|
for (size_t i = 0; i < matches.size(); i++) {
|
||||||
auto m = matches[i];
|
auto m = matches[i];
|
||||||
size_t start = m.position();
|
size_t start = m.position();
|
||||||
out.append(m.prefix().str().substr(last_end));
|
out.append(s.substr(last_end, m.position() - last_end));
|
||||||
// Merge continous matches
|
// Merge continous matches
|
||||||
ssize_t end = start + m.length();
|
ssize_t end = start + m.length();
|
||||||
while(i + 1 < matches.size() && matches[i+1].position() <= end) {
|
while(i + 1 < matches.size() && matches[i+1].position() <= end) {
|
||||||
|
@ -150,21 +151,18 @@ struct CmdSearch : InstallableCommand, MixJSON
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
|
||||||
for (auto & regex : regexes) {
|
for (auto & regex : regexes) {
|
||||||
std::smatch tmp;
|
|
||||||
found = false;
|
found = false;
|
||||||
|
auto add_all = [&found](std::sregex_iterator it, std::vector<std::smatch>& vec){
|
||||||
|
const auto end = std::sregex_iterator();
|
||||||
|
while(it != end) {
|
||||||
|
vec.push_back(*it++);
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if(std::regex_search(attrPath2, tmp, regex)) {
|
add_all(std::sregex_iterator(attrPath2.begin(), attrPath2.end(), regex), attrPathMatches);
|
||||||
attrPathMatches.push_back(tmp);
|
add_all(std::sregex_iterator(name.name.begin(), name.name.end(), regex), nameMatches);
|
||||||
found = true;
|
add_all(std::sregex_iterator(description.begin(), description.end(), regex), descriptionMatches);
|
||||||
}
|
|
||||||
if(std::regex_search(name.name, tmp, regex)) {
|
|
||||||
nameMatches.push_back(tmp);
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
if(std::regex_search(description, tmp, regex)) {
|
|
||||||
descriptionMatches.push_back(tmp);
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!found)
|
if(!found)
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in a new issue