Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Commit

Permalink
When wrapping, repeat indentation on each line
Browse files Browse the repository at this point in the history
Fixes #481.
  • Loading branch information
Minoru committed Sep 13, 2017
1 parent e650a69 commit 62080f9
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/textformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stflpp.h>
#include <assert.h>
#include <limits.h>
#include <algorithm>

namespace newsbeuter {

Expand Down Expand Up @@ -37,11 +38,29 @@ std::vector<std::string> wrap_line(
const std::string& line,
const size_t width)
{
if (line.empty()) {
return { "" };
}

std::vector<std::string> result;
std::vector<std::string> words = utils::tokenize_spaced(line);

std::string prefix;
auto iswhitespace =
[](const std::string& input)
{
return std::all_of(
input.cbegin(),
input.cend(),
[](std::string::value_type c) { return std::isspace(c); });
};
if (iswhitespace(words[0])) {
prefix = words[0];
}

std::string curline = "";

for (auto word : words){
for (auto word : words) {
size_t word_length = utils::strwidth_stfl(word);
size_t curline_length = utils::strwidth_stfl(curline);

Expand All @@ -52,18 +71,18 @@ std::vector<std::string> wrap_line(
curline.append(word.substr(0, space_left));
word.erase(0, space_left);
result.push_back(curline);
curline = "";
curline = prefix;

word_length = utils::strwidth_stfl(word);
curline_length = utils::strwidth_stfl(curline);
}

if ((curline_length + word_length) > width) {
result.push_back(curline);
if (word == " ") {
curline = "";
if (iswhitespace(word)) {
curline = prefix;
} else {
curline = word;
curline = prefix + word;
}
} else {
curline.append(word);
Expand Down

0 comments on commit 62080f9

Please sign in to comment.