diff --git a/rtv/curses_helpers.py b/rtv/curses_helpers.py index 98604db..c465262 100644 --- a/rtv/curses_helpers.py +++ b/rtv/curses_helpers.py @@ -5,6 +5,8 @@ import curses from curses import textpad, ascii from contextlib import contextmanager +from kitchen.text.display import textual_width, textual_width_chop + from .docs import HELP from .helpers import strip_textpad, clean from .exceptions import EscapeInterrupt @@ -54,7 +56,6 @@ def add_line(window, text, row=None, col=None, attr=None): # (window, text, row, col) # (window, text, row, col, attr) - # Text must be unicode or ascii. Can't be UTF-8! text = clean(text) cursor_row, cursor_col = window.getyx() @@ -67,20 +68,12 @@ def add_line(window, text, row=None, col=None, attr=None): # Trying to draw outside of the screen bounds return - # We have n_cols available to draw the text. Add characters to a text buffer - # until we reach the end of the screen - buffer, space_left = [], n_cols - for char in text: - space_left -= unicode_width(char) - if space_left < 0: - break - buffer.append(char) + text = textual_width_chop(text, n_cols) - trimmed_text = ''.join(buffer) if attr is None: - window.addnstr(row, col, trimmed_text, n_cols) + window.addnstr(row, col, text, n_cols) else: - window.addnstr(row, col, trimmed_text, n_cols, attr) + window.addnstr(row, col, text, n_cols, attr) def show_notification(stdscr, message): diff --git a/rtv/helpers.py b/rtv/helpers.py index 44b99d3..7329c0d 100644 --- a/rtv/helpers.py +++ b/rtv/helpers.py @@ -79,6 +79,7 @@ def clean(string): string = string.encode('ascii', 'replace') return string + def wrap_text(text, width): """ Wrap text paragraphs to the given character width while preserving newlines. diff --git a/rtv/page.py b/rtv/page.py index 1262264..5e671d9 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -7,6 +7,7 @@ from contextlib import contextmanager import praw.errors import requests +from kitchen.text.display import textual_width from .helpers import open_editor from .curses_helpers import (Color, show_notification, show_help, text_input, @@ -474,8 +475,7 @@ class BasePage(object): if self.reddit.user is not None: username = self.reddit.user.name - # TODO: use unicode width here instead of length - s_col = (n_cols - len(username) - 1) + s_col = (n_cols - textual_width(username) - 1) # Only print username if it fits in the empty space on the right if (s_col - 1) >= len(sub_name): n = (n_cols - s_col - 1)