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

Commit

Permalink
fix(screens/index): correctly handle Unicode strings
Browse files Browse the repository at this point in the history
This fix is likely to apply elsewhere, but we'll tackle those issues
when we find them, so that we do not accidentaly introduce new bugs.

Resolves #83
  • Loading branch information
tmplt committed Jul 9, 2019
1 parent dd9ec9d commit c61c036
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org)
* Correctly look for `--author` option, not `--authors`.
* Screens/details: "Serie" -> "Series"
* Screens/details: Correctly print empty fields.
* Screens/index: Correctly print Unicode strings. #83

## [v0.8.0] - 2019-05-26

Expand Down
13 changes: 12 additions & 1 deletion src/tui/curses_wrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,18 @@ namespace bookwyrm::tui::curses {
colour clr = colour::none)
{
wattron(win, clr | attrs);
int retval = mvwaddnstr(win, y, x, str.c_str(), n);

/*
* Special handling for eventual Unicode strings.
* We want to print n characters, not n bytes.
*
* XXX: inefficient, must std::wstring be used?
*/
std::vector<wchar_t> wstr;
wstr.resize(std::max<int>(str.size(), n));
std::mbstowcs(wstr.data(), str.c_str(), n);
int retval = mvwaddnwstr(win, y, x, wstr.data(), n);

wattroff(win, clr | attrs);
return retval;
}
Expand Down
7 changes: 4 additions & 3 deletions src/tui/screens/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ namespace bookwyrm::tui::screen {
{
space = std::min<size_t>(space, get_width() - x);
curses::mvprintn(window_, x, y, str, space, attrs, clr);
const auto len = std::mbstowcs(nullptr, str.c_str(), str.size());

int truncd = 0;
if (str.length() > space) {
if (len > space) {
/* The whole string did not fit; indicate this to the user. */

/* From the substing written in the space, find the number of whitespaces from the end of the string to the first
Expand All @@ -73,8 +74,8 @@ namespace bookwyrm::tui::screen {
while (std::isspace(*(--ch)))
++whitespace;

truncd = str.length() - space + whitespace;
curses::mvprint(window_, x + str.length() - truncd - 1, y, "~", attrs, clr);
truncd = len - space + whitespace;
curses::mvprint(window_, x + len - truncd - 1, y, "~", attrs, clr);
}

return truncd;
Expand Down
3 changes: 2 additions & 1 deletion src/tui/screens/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ namespace bookwyrm::tui::screen {
* padding on the right side of it (e.g. up to and including the first char
* in the next column, hence the magic offset).
*/
const auto start = col.startx + str.length() - trunc_len, end = col.startx + col.width + 2;
const auto start = col.startx + std::mbstowcs(nullptr, str.c_str(), str.size()) - trunc_len,
end = col.startx + col.width + 2;
for (auto x = start; x <= end; x++)
print(x, y, " ", attrs);
}
Expand Down

0 comments on commit c61c036

Please sign in to comment.