Fix displaying non-ascii strings in the console ui -- patch from Ian Martin

This commit is contained in:
Andrew Resch 2009-10-30 18:00:13 +00:00
parent 6d2d3c0fd0
commit 7c2a2af1f0
3 changed files with 21 additions and 3 deletions

View File

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

View File

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

View File

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