Skip to content

Commit

Permalink
Updated console formatting operator - now splits the string
Browse files Browse the repository at this point in the history
by line to allow trimming of excess white space. This permits
the use of raw `R"()"` string definitions that will not mess
up the display formatting if they themselves have been formatted.
  • Loading branch information
parnham committed Sep 12, 2024
1 parent 853c5b2 commit 6072b81
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions include/emergent/Console.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <emergent/String.hpp>
#include <string>
#include <string_view>
#include <iomanip>
Expand Down Expand Up @@ -58,32 +59,29 @@ namespace emergent
}

const auto remaining = f.width - f.padding;
const auto size = f.text.size();
size_t pos = 0;
auto padding = 0;

dst << std::setfill(' ');

while (pos < size)
for (auto line : String::explode(f.text, "\n"))
{
const auto line = f.text.find_first_of('\n', pos) - pos;
const auto last = size - pos < remaining
? std::string::npos
: f.text.find_last_of(" .,(/-", pos + remaining - 1);

const auto next = line < remaining
? f.text.substr(pos, line)
: f.text.substr(pos, last == std::string::npos || last < pos
? remaining
: last + 1 - pos
);

if (pos > 0)
line = String::trim(line, " \t");

while (line.length() >= remaining)
{
dst << std::setw(f.padding) << ' ';
const auto split = std::min(
line.find_last_of(" .,(/-", remaining - 1),
remaining
);

dst << std::setw(padding) << "" << line.substr(0, split) << '\n';

line = line.substr(split + 1);
padding = f.padding;
}

dst << next << '\n';
pos += next.size() + (line < remaining ? 1 : 0);
dst << std::setw(padding) << "" << line <<'\n';
padding = f.padding;
}

return dst;
Expand Down

0 comments on commit 6072b81

Please sign in to comment.