MiscUtils word_wrap: Add option to trim only leading whitespace after wrapping (#2169)

* Update changelog.txt
develop
Ryan Williams 2022-05-30 19:58:46 -07:00 committed by GitHub
parent b1e118384e
commit bc0def4342
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 7 deletions

@ -47,6 +47,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## Documentation
## API
- ``word_wrap``: argument ``bool collapse_whitespace`` converted to enum ``word_wrap_whitespace_mode mode``, with valid modes ``WSMODE_KEEP_ALL``, ``WSMODE_COLLAPSE_ALL``, and ``WSMODE_TRIM_LEADING``.
## Lua
- ``widgets.HotkeyLabel``: the ``key_sep`` string is now configurable

@ -168,15 +168,15 @@ std::string to_search_normalized(const std::string &str)
return result;
}
bool word_wrap(std::vector<std::string> *out, const std::string &str,
size_t line_length, bool collapse_whitespace)
bool word_wrap(std::vector<std::string> *out, const std::string &str, size_t line_length,
word_wrap_whitespace_mode mode)
{
if (line_length == 0)
line_length = SIZE_MAX;
std::string line;
size_t break_pos = 0;
bool ignore_whitespace = false;
for (auto &c : str)
{
@ -185,19 +185,22 @@ bool word_wrap(std::vector<std::string> *out, const std::string &str,
out->push_back(line);
line.clear();
break_pos = 0;
ignore_whitespace = (mode == WSMODE_TRIM_LEADING);
continue;
}
if (isspace(c))
{
if (break_pos == line.length() && collapse_whitespace)
if (ignore_whitespace || (mode == WSMODE_COLLAPSE_ALL && break_pos == line.length()))
continue;
line.push_back(collapse_whitespace ? ' ' : c);
line.push_back((mode == WSMODE_COLLAPSE_ALL) ? ' ' : c);
break_pos = line.length();
}
else {
else
{
line.push_back(c);
ignore_whitespace = false;
}
if (line.length() > line_length)
@ -215,6 +218,7 @@ bool word_wrap(std::vector<std::string> *out, const std::string &str,
}
line = line.substr(break_pos);
break_pos = 0;
ignore_whitespace = (mode == WSMODE_TRIM_LEADING);
}
}
if (line.length())

@ -389,10 +389,16 @@ DFHACK_EXPORT std::string toUpper(const std::string &str);
DFHACK_EXPORT std::string toLower(const std::string &str);
DFHACK_EXPORT std::string to_search_normalized(const std::string &str);
enum word_wrap_whitespace_mode {
WSMODE_KEEP_ALL,
WSMODE_COLLAPSE_ALL,
WSMODE_TRIM_LEADING
};
DFHACK_EXPORT bool word_wrap(std::vector<std::string> *out,
const std::string &str,
size_t line_length = 80,
bool collapse_whitespace = false);
word_wrap_whitespace_mode mode = WSMODE_KEEP_ALL);
inline bool bits_match(unsigned required, unsigned ok, unsigned mask)
{