Added kitchen functions for chopping unicode text.

This commit is contained in:
Michael Lazar
2015-05-02 17:23:22 -07:00
parent f0ec9ba712
commit d548e65166
3 changed files with 8 additions and 14 deletions

View File

@@ -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):

View File

@@ -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.

View File

@@ -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)