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

Commit

Permalink
WIP: fix(screens/index): correctly print Unicode strings
Browse files Browse the repository at this point in the history
this fix should probably be applied elsewhere.
Fix inefficient code, etc.

Resolves #83
  • Loading branch information
tmplt committed Jul 8, 2019
1 parent dd9ec9d commit aea5273
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 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
7 changes: 6 additions & 1 deletion src/tui/curses_wrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ namespace bookwyrm::tui::curses {
colour clr = colour::none)
{
wattron(win, clr | attrs);
int retval = mvwaddnstr(win, y, x, str.c_str(), n);

// XXX: inefficient.
wchar_t wstr[256];
std::mbstowcs(wstr, str.c_str(), n);
int retval = mvwaddnwstr(win, y, x, wstr, n);

wattroff(win, clr | attrs);
return retval;
}
Expand Down
5 changes: 3 additions & 2 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,7 +74,7 @@ namespace bookwyrm::tui::screen {
while (std::isspace(*(--ch)))
++whitespace;

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

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 aea5273

Please sign in to comment.