Skip to content

Commit

Permalink
Extract printSpace helper
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed Feb 8, 2024
1 parent 1c5f5d4 commit 403c90d
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/libexpr/print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ class Printer
}
}

/**
* Print a space (for separating items or attributes).
*
* If pretty-printing is enabled, a newline and the current `indent` is
* printed instead.
*/
void printSpace(bool prettyPrint)
{
if (prettyPrint) {
output << "\n" << indent;
} else {
output << " ";
}
}

void printRepeated()
{
if (options.ansiColors)
Expand Down Expand Up @@ -324,11 +339,7 @@ class Printer
auto prettyPrint = shouldPrettyPrintAttrs(sorted);

for (auto & i : sorted) {
if (prettyPrint) {
output << "\n" << indent;
} else {
output << " ";
}
printSpace(prettyPrint);

if (attrsPrinted >= options.maxAttrs) {
printElided(sorted.size() - attrsPrinted, "attribute", "attributes");
Expand All @@ -343,11 +354,7 @@ class Printer
}

decreaseIndent();
if (prettyPrint) {
output << "\n" << indent;
} else {
output << " ";
}
printSpace(prettyPrint);
output << "}";
} else {
output << "{ ... }";
Expand Down Expand Up @@ -389,11 +396,7 @@ class Printer
auto listItems = v.listItems();
auto prettyPrint = shouldPrettyPrintList(listItems);
for (auto elem : listItems) {
if (prettyPrint) {
output << "\n" << indent;
} else {
output << " ";
}
printSpace(prettyPrint);

if (listItemsPrinted >= options.maxListItems) {
printElided(listItems.size() - listItemsPrinted, "item", "items");
Expand All @@ -409,11 +412,7 @@ class Printer
}

decreaseIndent();
if (prettyPrint) {
output << "\n" << indent;
} else {
output << " ";
}
printSpace(prettyPrint);
output << "]";
} else {
output << "[ ... ]";
Expand Down

0 comments on commit 403c90d

Please sign in to comment.