mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-29 17:16:15 +02:00
Make hilite_all
take an iterator of matches instead of a vector.
This commit is contained in:
parent
87fdd23025
commit
9510ad10c5
1 changed files with 13 additions and 13 deletions
|
@ -33,27 +33,27 @@ std::string hilite(const std::string & s, const std::smatch & m, std::string pos
|
||||||
+ std::string(m.suffix());
|
+ std::string(m.suffix());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string hilite_all(const std::string &s, std::vector<std::smatch> matches, std::string postfix) {
|
template<class Iter>
|
||||||
// Don't waste time on trivial highlights
|
std::string hilite_all(const std::string &s, Iter matches_first, Iter matches_last, std::string postfix) {
|
||||||
if (matches.size() == 0)
|
// Avoid copy on zero matches
|
||||||
|
if (matches_first == matches_last)
|
||||||
return s;
|
return s;
|
||||||
else if (matches.size() == 1)
|
|
||||||
return hilite(s, matches[0], postfix);
|
|
||||||
|
|
||||||
std::sort(matches.begin(), matches.end(), [](const auto &a, const auto &b) {
|
std::sort(matches_first, matches_last, [](const auto &a, const auto &b) {
|
||||||
return a.position() < b.position();
|
return a.position() < b.position();
|
||||||
});
|
});
|
||||||
|
|
||||||
std::string out;
|
std::string out;
|
||||||
ssize_t last_end = 0;
|
ssize_t last_end = 0;
|
||||||
for (size_t i = 0; i < matches.size(); i++) {
|
|
||||||
auto m = matches[i];
|
for (Iter it = matches_first; it != matches_last; ++it) {
|
||||||
|
auto m = *it;
|
||||||
size_t start = m.position();
|
size_t start = m.position();
|
||||||
out.append(s.substr(last_end, m.position() - 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(it + 1 != matches_last && (*(it + 1)).position() <= end) {
|
||||||
auto n = matches[++i];
|
auto n = *++it;
|
||||||
ssize_t nend = start + (n.position() - start + n.length());
|
ssize_t nend = start + (n.position() - start + n.length());
|
||||||
if(nend > end)
|
if(nend > end)
|
||||||
end = nend;
|
end = nend;
|
||||||
|
@ -177,15 +177,15 @@ struct CmdSearch : InstallableCommand, MixJSON
|
||||||
jsonElem.attr("version", name.version);
|
jsonElem.attr("version", name.version);
|
||||||
jsonElem.attr("description", description);
|
jsonElem.attr("description", description);
|
||||||
} else {
|
} else {
|
||||||
auto name2 = hilite_all(name.name, nameMatches, "\e[0;2m");
|
auto name2 = hilite_all(name.name, nameMatches.begin(), nameMatches.end(), "\e[0;2m");
|
||||||
if (results > 1) logger->cout("");
|
if (results > 1) logger->cout("");
|
||||||
logger->cout(
|
logger->cout(
|
||||||
"* %s%s",
|
"* %s%s",
|
||||||
wrap("\e[0;1m", hilite_all(attrPath2, attrPathMatches, "\e[0;1m")),
|
wrap("\e[0;1m", hilite_all(attrPath2, attrPathMatches.begin(), attrPathMatches.end(), "\e[0;1m")),
|
||||||
name.version != "" ? " (" + name.version + ")" : "");
|
name.version != "" ? " (" + name.version + ")" : "");
|
||||||
if (description != "")
|
if (description != "")
|
||||||
logger->cout(
|
logger->cout(
|
||||||
" %s", hilite_all(description, descriptionMatches, ANSI_NORMAL));
|
" %s", hilite_all(description, descriptionMatches.begin(), descriptionMatches.end(), ANSI_NORMAL));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue