newline-as-prefix; no final newline in output.

This commit is contained in:
Ben Burdette 2020-06-03 14:47:00 -06:00
parent 156d4f8bc8
commit f97576c5d9

View file

@ -57,20 +57,20 @@ void printCodeLines(std::ostream &out, const string &prefix, const NixCode &nixC
{ {
// previous line of code. // previous line of code.
if (nixCode.prevLineOfCode.has_value()) { if (nixCode.prevLineOfCode.has_value()) {
out << fmt("%1% %|2$5d|| %3%", out << std::endl
<< fmt("%1% %|2$5d|| %3%",
prefix, prefix,
(nixCode.errPos.line - 1), (nixCode.errPos.line - 1),
*nixCode.prevLineOfCode) *nixCode.prevLineOfCode);
<< std::endl;
} }
if (nixCode.errLineOfCode.has_value()) { if (nixCode.errLineOfCode.has_value()) {
// line of code containing the error. // line of code containing the error.
out << fmt("%1% %|2$5d|| %3%", out << std::endl
<< fmt("%1% %|2$5d|| %3%",
prefix, prefix,
(nixCode.errPos.line), (nixCode.errPos.line),
*nixCode.errLineOfCode) *nixCode.errLineOfCode);
<< std::endl;
// error arrows for the column range. // error arrows for the column range.
if (nixCode.errPos.column > 0) { if (nixCode.errPos.column > 0) {
int start = nixCode.errPos.column; int start = nixCode.errPos.column;
@ -81,20 +81,21 @@ void printCodeLines(std::ostream &out, const string &prefix, const NixCode &nixC
std::string arrows("^"); std::string arrows("^");
out << fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL, out << std::endl
<< fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL,
prefix, prefix,
spaces, spaces,
arrows) << std::endl; arrows);
} }
} }
// next line of code. // next line of code.
if (nixCode.nextLineOfCode.has_value()) { if (nixCode.nextLineOfCode.has_value()) {
out << fmt("%1% %|2$5d|| %3%", out << std::endl
<< fmt("%1% %|2$5d|| %3%",
prefix, prefix,
(nixCode.errPos.line + 1), (nixCode.errPos.line + 1),
*nixCode.nextLineOfCode) *nixCode.nextLineOfCode);
<< std::endl;
} }
} }
@ -167,46 +168,50 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
levelString, levelString,
einfo.name, einfo.name,
dashes, dashes,
einfo.programName.value_or("")) einfo.programName.value_or(""));
<< std::endl;
else else
out << fmt("%1%%2%" ANSI_BLUE " -----%3% %4%" ANSI_NORMAL, out << fmt("%1%%2%" ANSI_BLUE " -----%3% %4%" ANSI_NORMAL,
prefix, prefix,
levelString, levelString,
dashes, dashes,
einfo.programName.value_or("")) einfo.programName.value_or(""));
<< std::endl;
// filename, line, column. bool nl = false; // intersperse newline between sections.
if (einfo.nixCode.has_value()) { if (einfo.nixCode.has_value()) {
if (einfo.nixCode->errPos.file != "") { if (einfo.nixCode->errPos.file != "") {
out << fmt("%1%in file: " ANSI_BLUE "%2% %3%" ANSI_NORMAL, // filename, line, column.
out << std::endl << fmt("%1%in file: " ANSI_BLUE "%2% %3%" ANSI_NORMAL,
prefix, prefix,
einfo.nixCode->errPos.file, einfo.nixCode->errPos.file,
showErrPos(einfo.nixCode->errPos)) << std::endl; showErrPos(einfo.nixCode->errPos));
out << prefix << std::endl;
} else { } else {
out << fmt("%1%from command line argument", prefix) << std::endl; out << std::endl << fmt("%1%from command line argument", prefix);
out << prefix << std::endl;
} }
nl = true;
} }
// description // description
if (einfo.description != "") { if (einfo.description != "") {
out << prefix << einfo.description << std::endl; if (nl)
out << prefix << std::endl; out << std::endl << prefix;
out << std::endl << prefix << einfo.description;
nl = true;
} }
// lines of code. // lines of code.
if (einfo.nixCode.has_value() && einfo.nixCode->errLineOfCode.has_value()) { if (einfo.nixCode.has_value() && einfo.nixCode->errLineOfCode.has_value()) {
if (nl)
out << std::endl << prefix;
printCodeLines(out, prefix, *einfo.nixCode); printCodeLines(out, prefix, *einfo.nixCode);
out << prefix << std::endl; nl = true;
} }
// hint // hint
if (einfo.hint.has_value()) { if (einfo.hint.has_value()) {
out << prefix << *einfo.hint << std::endl; if (nl)
out << prefix << std::endl; out << std::endl << prefix;
out << std::endl << prefix << *einfo.hint;
nl = true;
} }
return out; return out;