From 7c2a2af1f0ab5209aeddc1a39afeda8e84176401 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Fri, 30 Oct 2009 18:00:13 +0000 Subject: [PATCH] Fix displaying non-ascii strings in the console ui -- patch from Ian Martin --- deluge/ui/console/commands/info.py | 2 +- deluge/ui/console/main.py | 11 ++++++++++- deluge/ui/console/screen.py | 11 ++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/deluge/ui/console/commands/info.py b/deluge/ui/console/commands/info.py index b4350f22e..9f69484a7 100644 --- a/deluge/ui/console/commands/info.py +++ b/deluge/ui/console/commands/info.py @@ -201,7 +201,7 @@ class Command(BaseCommand): s += peer["country"] + "\t" s += peer["ip"] - c = peer["client"].encode(sys.getdefaultencoding(), "replace") + c = peer["client"] s += "\t" + c if len(c) < 16: diff --git a/deluge/ui/console/main.py b/deluge/ui/console/main.py index 68de4f53f..248060734 100644 --- a/deluge/ui/console/main.py +++ b/deluge/ui/console/main.py @@ -37,6 +37,7 @@ import os, sys import optparse import shlex +import locale from twisted.internet import defer, reactor @@ -137,6 +138,14 @@ def load_commands(command_dir, exclude=[]): class ConsoleUI(component.Component): def __init__(self, args=None): component.Component.__init__(self, "ConsoleUI", 2) + + try: + locale.setlocale(locale.LC_ALL, '') + self.encoding = locale.getpreferredencoding() + except: + self.encoding = sys.getdefaultencoding() + + log.debug("Using encoding: %s", self.encoding) # Load all the commands self._commands = load_commands(os.path.join(UI_PATH, 'commands')) @@ -191,7 +200,7 @@ class ConsoleUI(component.Component): # We want to do an interactive session, so start up the curses screen and # pass it the function that handles commands colors.init_colors() - self.screen = screen.Screen(stdscr, self.do_command, self.tab_completer) + self.screen = screen.Screen(stdscr, self.do_command, self.tab_completer, self.encoding) self.statusbars = StatusBars() self.eventlog = EventLog() diff --git a/deluge/ui/console/screen.py b/deluge/ui/console/screen.py index 4b01680e8..4e53106a5 100644 --- a/deluge/ui/console/screen.py +++ b/deluge/ui/console/screen.py @@ -33,6 +33,7 @@ # # +import sys import curses import colors try: @@ -63,7 +64,7 @@ LINES_BUFFER_SIZE = 5000 INPUT_HISTORY_SIZE = 500 class Screen(CursesStdIO): - def __init__(self, stdscr, command_parser, tab_completer=None): + def __init__(self, stdscr, command_parser, tab_completer=None, encoding=None): """ A curses screen designed to run as a reader in a twisted reactor. @@ -110,6 +111,11 @@ class Screen(CursesStdIO): except Exception, e: log.debug("Unable to catch SIGWINCH signal!") + if not encoding: + self.encoding = sys.getdefaultencoding() + else: + self.encoding = encoding + # Do a refresh right away to draw the screen self.refresh() @@ -233,6 +239,9 @@ class Screen(CursesStdIO): if index + 1 == len(parsed): # This is the last string so lets append some " " to it s += " " * (self.cols - (col + len(s)) - 1) + if isinstance(s, unicode): + #Have to use replace as character counting has already been done + s = s.encode(self.encoding, 'replace') self.stdscr.addstr(row, col, s, color) col += len(s)