forked from lix-project/lix
Implement operator<<
for Suggestions
That way there’s no need to explicitely convert it to a string when printing it
This commit is contained in:
parent
fd45d85b41
commit
313bbc07a8
|
@ -285,7 +285,7 @@ std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool s
|
||||||
auto suggestions = einfo.suggestions.trim();
|
auto suggestions = einfo.suggestions.trim();
|
||||||
if (! suggestions.suggestions.empty()){
|
if (! suggestions.suggestions.empty()){
|
||||||
oss << "Did you mean " <<
|
oss << "Did you mean " <<
|
||||||
suggestions.trim().pretty_print() <<
|
suggestions.trim() <<
|
||||||
"?" << std::endl;
|
"?" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,27 +65,27 @@ Suggestions Suggestions::trim(int limit, int maxDistance) const
|
||||||
return Suggestions{res};
|
return Suggestions{res};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Suggestion::pretty_print() const
|
std::string Suggestion::to_string() const
|
||||||
{
|
{
|
||||||
return ANSI_WARNING + filterANSIEscapes(suggestion) + ANSI_NORMAL;
|
return ANSI_WARNING + filterANSIEscapes(suggestion) + ANSI_NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Suggestions::pretty_print() const
|
std::string Suggestions::to_string() const
|
||||||
{
|
{
|
||||||
switch (suggestions.size()) {
|
switch (suggestions.size()) {
|
||||||
case 0:
|
case 0:
|
||||||
return "";
|
return "";
|
||||||
case 1:
|
case 1:
|
||||||
return suggestions.begin()->pretty_print();
|
return suggestions.begin()->to_string();
|
||||||
default: {
|
default: {
|
||||||
std::string res = "one of ";
|
std::string res = "one of ";
|
||||||
auto iter = suggestions.begin();
|
auto iter = suggestions.begin();
|
||||||
res += iter->pretty_print(); // Iter can’t be end() because the container isn’t null
|
res += iter->to_string(); // Iter can’t be end() because the container isn’t null
|
||||||
iter++;
|
iter++;
|
||||||
auto last = suggestions.end(); last--;
|
auto last = suggestions.end(); last--;
|
||||||
for ( ; iter != suggestions.end() ; iter++) {
|
for ( ; iter != suggestions.end() ; iter++) {
|
||||||
res += (iter == last) ? " or " : ", ";
|
res += (iter == last) ? " or " : ", ";
|
||||||
res += iter->pretty_print();
|
res += iter->to_string();
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -101,4 +101,14 @@ Suggestions & Suggestions::operator+=(const Suggestions & other)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream & operator<<(std::ostream & str, const Suggestion & suggestion)
|
||||||
|
{
|
||||||
|
return str << suggestion.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream & operator<<(std::ostream & str, const Suggestions & suggestions)
|
||||||
|
{
|
||||||
|
return str << suggestions.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ public:
|
||||||
int distance; // The smaller the better
|
int distance; // The smaller the better
|
||||||
std::string suggestion;
|
std::string suggestion;
|
||||||
|
|
||||||
std::string pretty_print() const;
|
std::string to_string() const;
|
||||||
|
|
||||||
GENERATE_CMP(Suggestion, me->distance, me->suggestion)
|
GENERATE_CMP(Suggestion, me->distance, me->suggestion)
|
||||||
};
|
};
|
||||||
|
@ -25,7 +25,7 @@ class Suggestions {
|
||||||
public:
|
public:
|
||||||
std::set<Suggestion> suggestions;
|
std::set<Suggestion> suggestions;
|
||||||
|
|
||||||
std::string pretty_print() const;
|
std::string to_string() const;
|
||||||
|
|
||||||
Suggestions trim(
|
Suggestions trim(
|
||||||
int limit = 5,
|
int limit = 5,
|
||||||
|
@ -39,4 +39,7 @@ public:
|
||||||
|
|
||||||
Suggestions& operator+=(const Suggestions & other);
|
Suggestions& operator+=(const Suggestions & other);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::ostream & operator<<(std::ostream & str, const Suggestion &);
|
||||||
|
std::ostream & operator<<(std::ostream & str, const Suggestions &);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue