Skip to content

Commit

Permalink
[crispy] CLI: Handle newlines in help descriptions when printed, pres…
Browse files Browse the repository at this point in the history
…erving indentation.
  • Loading branch information
christianparpart committed Jul 11, 2021
1 parent af81fdf commit 9c01cc0
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/crispy/CLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,15 +565,28 @@ namespace // {{{ helpers
return stylizer(style);
}

string_view wordWrapped(string_view _text, unsigned _margin, unsigned _cursor)
string_view wordWrapped(string_view _text, int _margin, int _cursor, bool* _trimLeadingWhitespaces)
{
auto const unwrappedLength = _cursor + _text.size();
auto const linefeed = _text.find('\n');
if (linefeed != string_view::npos)
{
auto i = linefeed - 1; // Position before the found LF and trim off right whitespaces.
while (i > 0 && _text[i] == ' ')
--i;
*_trimLeadingWhitespaces = false;
return _text.substr(0, i + 1);
}

*_trimLeadingWhitespaces = true;

auto const unwrappedLength = _cursor + int(_text.size());
if (unwrappedLength <= _margin)
return _text;

// Cut string at right margin, then shift left until we've hit a whitespace character.
auto i = _margin - _cursor + 1;
while (i > 0 && _text[i] != ' ')
auto const rightMargin = _margin - _cursor + 1;
auto i = rightMargin;
while (i > 0 && (_text[i] != ' ' && _text[i] != '\n'))
--i;

return _text.substr(0, i);
Expand All @@ -583,12 +596,14 @@ namespace // {{{ helpers
{
string output;
size_t i = 0;
bool trimLeadingWhitespaces = true;
for (;;)
{
while (i < _text.size() && _text[i] == ' ')
auto const trimChar = trimLeadingWhitespaces ? ' ' : '\n';
while (i < _text.size() && _text[i] == trimChar)
++i; // skip leading whitespaces

auto const chunk = wordWrapped(_text.substr(i), _margin, *_cursor);
auto const chunk = wordWrapped(_text.substr(i), _margin, *_cursor, &trimLeadingWhitespaces);

output += chunk;
*_cursor += static_cast<unsigned>(chunk.size());
Expand Down

0 comments on commit 9c01cc0

Please sign in to comment.