Extract printSpace helper

This commit is contained in:
Rebecca Turner 2024-02-08 10:17:33 -08:00
parent 1c5f5d4291
commit 403c90ddf5
No known key found for this signature in database

View file

@ -181,6 +181,21 @@ private:
}
}
/**
* 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)
@ -324,11 +339,7 @@ private:
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");
@ -343,11 +354,7 @@ private:
}
decreaseIndent();
if (prettyPrint) {
output << "\n" << indent;
} else {
output << " ";
}
printSpace(prettyPrint);
output << "}";
} else {
output << "{ ... }";
@ -389,11 +396,7 @@ private:
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");
@ -409,11 +412,7 @@ private:
}
decreaseIndent();
if (prettyPrint) {
output << "\n" << indent;
} else {
output << " ";
}
printSpace(prettyPrint);
output << "]";
} else {
output << "[ ... ]";