From d1e3dbc65e8130ba3334316526bf059eb5c28127 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Sun, 10 May 2015 21:55:54 -0700 Subject: [PATCH] Still not working. --- rtv/__main__.py | 3 ++- rtv/curses_helpers.py | 12 +++--------- rtv/helpers.py | 19 +++++++++++++++---- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/rtv/__main__.py b/rtv/__main__.py index 5322afd..6619a19 100644 --- a/rtv/__main__.py +++ b/rtv/__main__.py @@ -59,7 +59,8 @@ def command_line(): parser.add_argument('-s', dest='subreddit', help='subreddit name') parser.add_argument('-l', dest='link', help='full link to a submission') - parser.add_argument('--unicode', help='enable unicode (experimental)') + parser.add_argument('--unicode', action='store_const', const=False, + help='enable unicode (experimental)') parser.add_argument('--log', metavar='FILE', action='store', help='Log HTTP requests') diff --git a/rtv/curses_helpers.py b/rtv/curses_helpers.py index 0ed8924..3e1de6c 100644 --- a/rtv/curses_helpers.py +++ b/rtv/curses_helpers.py @@ -5,8 +5,6 @@ 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 @@ -66,13 +64,9 @@ def add_line(window, text, row=None, col=None, attr=None): # Trying to draw outside of the screen bounds return - text = clean(text) - text = textual_width_chop(text, n_cols) - - if attr is None: - window.addnstr(row, col, text, n_cols) - else: - window.addnstr(row, col, text, n_cols, attr) + text = clean(text, n_cols) + params = [] if attr is None else [attr] + window.addstr(row, col, text, *params) def show_notification(stdscr, message): diff --git a/rtv/helpers.py b/rtv/helpers.py index 7329c0d..058d8cb 100644 --- a/rtv/helpers.py +++ b/rtv/helpers.py @@ -5,7 +5,8 @@ from datetime import datetime from tempfile import NamedTemporaryFile # kitchen solves deficiencies in textwrap's handling of unicode characters -from kitchen.text.display import wrap +from kitchen.text.display import wrap, textual_width_chop +import six from . import config from .exceptions import ProgramError @@ -56,7 +57,7 @@ def open_browser(url): subprocess.check_call(args, stdout=null, stderr=null) -def clean(string): +def clean(string, n_cols=None): """ Required reading! http://nedbatchelder.com/text/unipain.html @@ -75,9 +76,19 @@ def clean(string): If utf-8 is passed in, addnstr will treat each 'byte' as a single character. """ + if n_cols is not None and n_cols <= 0: + return '' + if not config.unicode: - string = string.encode('ascii', 'replace') - return string + if six.PY3 or isinstance(string, unicode): + string = string.encode('ascii', 'replace') + return string[:n_cols] if n_cols else string + else: + if n_cols: + string = textual_width_chop(string, n_cols) + if six.PY3 or isinstance(string, unicode): + string = string.encode('utf-8') + return string def wrap_text(text, width):