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() void printRepeated()
{ {
if (options.ansiColors) if (options.ansiColors)
@ -324,11 +339,7 @@ private:
auto prettyPrint = shouldPrettyPrintAttrs(sorted); auto prettyPrint = shouldPrettyPrintAttrs(sorted);
for (auto & i : sorted) { for (auto & i : sorted) {
if (prettyPrint) { printSpace(prettyPrint);
output << "\n" << indent;
} else {
output << " ";
}
if (attrsPrinted >= options.maxAttrs) { if (attrsPrinted >= options.maxAttrs) {
printElided(sorted.size() - attrsPrinted, "attribute", "attributes"); printElided(sorted.size() - attrsPrinted, "attribute", "attributes");
@ -343,11 +354,7 @@ private:
} }
decreaseIndent(); decreaseIndent();
if (prettyPrint) { printSpace(prettyPrint);
output << "\n" << indent;
} else {
output << " ";
}
output << "}"; output << "}";
} else { } else {
output << "{ ... }"; output << "{ ... }";
@ -389,11 +396,7 @@ private:
auto listItems = v.listItems(); auto listItems = v.listItems();
auto prettyPrint = shouldPrettyPrintList(listItems); auto prettyPrint = shouldPrettyPrintList(listItems);
for (auto elem : listItems) { for (auto elem : listItems) {
if (prettyPrint) { printSpace(prettyPrint);
output << "\n" << indent;
} else {
output << " ";
}
if (listItemsPrinted >= options.maxListItems) { if (listItemsPrinted >= options.maxListItems) {
printElided(listItems.size() - listItemsPrinted, "item", "items"); printElided(listItems.size() - listItemsPrinted, "item", "items");
@ -409,11 +412,7 @@ private:
} }
decreaseIndent(); decreaseIndent();
if (prettyPrint) { printSpace(prettyPrint);
output << "\n" << indent;
} else {
output << " ";
}
output << "]"; output << "]";
} else { } else {
output << "[ ... ]"; output << "[ ... ]";