From 62080f93ecc252fbd7e8fe5585b5ebc1a9b81793 Mon Sep 17 00:00:00 2001 From: Alexander Batischev Date: Thu, 14 Sep 2017 00:05:55 +0300 Subject: When wrapping, repeat indentation on each line Fixes #481. --- src/textformatter.cpp | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src/textformatter.cpp') diff --git a/src/textformatter.cpp b/src/textformatter.cpp index 0cfd2d35..cc357c6f 100644 --- a/src/textformatter.cpp +++ b/src/textformatter.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace newsbeuter { @@ -37,11 +38,29 @@ std::vector wrap_line( const std::string& line, const size_t width) { + if (line.empty()) { + return { "" }; + } + std::vector result; std::vector 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); @@ -52,7 +71,7 @@ std::vector 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); @@ -60,10 +79,10 @@ std::vector wrap_line( 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); -- cgit v1.2.3