[Console] Fix cmdline output and tests

This commit is contained in:
Calum Lind 2018-07-28 16:19:15 +01:00
parent ae4449642c
commit 8d90ae5ffb
3 changed files with 31 additions and 21 deletions

View File

@ -20,7 +20,7 @@ from twisted.internet import defer
import deluge import deluge
import deluge.component as component import deluge.component as component
import deluge.ui.web.server import deluge.ui.web.server
from deluge.common import get_localhost_auth, utf8_encode_structure, windows_check from deluge.common import get_localhost_auth, utf8_encode_structure, windows_check, PY2
from deluge.ui import ui_entry from deluge.ui import ui_entry
from deluge.ui.web.server import DelugeWeb from deluge.ui.web.server import DelugeWeb
@ -53,7 +53,10 @@ class StringFileDescriptor(object):
def write(self, *data, **kwargs): def write(self, *data, **kwargs):
# io.StringIO requires unicode strings. # io.StringIO requires unicode strings.
print(unicode(*data), file=self.out, end='') data_string = str(*data)
if PY2:
data_string = data_string.decode()
print(data_string, file=self.out, end='')
def flush(self): def flush(self):
self.out.flush() self.out.flush()

View File

@ -438,7 +438,7 @@ Please use commands from the command line, e.g.:\n
component.get('CmdLine').add_line(s, False) component.get('CmdLine').add_line(s, False)
self.events.append(s) self.events.append(s)
else: else:
print(colors.strip_colors(s.encode('utf8'))) print(colors.strip_colors(s))
def write_event(self, s): def write_event(self, s):
if self.interactive: if self.interactive:
@ -449,7 +449,7 @@ Please use commands from the command line, e.g.:\n
component.get('CmdLine').add_line(s, False) component.get('CmdLine').add_line(s, False)
self.events.append(s) self.events.append(s)
else: else:
print(colors.strip_colors(s.encode('utf8'))) print(colors.strip_colors(s))
def _migrate_config_1_to_2(self, config): def _migrate_config_1_to_2(self, config):
"""Create better structure by moving most settings out of dict root """Create better structure by moving most settings out of dict root

View File

@ -73,6 +73,10 @@ type_color = {
dict: '{!white,black,bold!}', dict: '{!white,black,bold!}',
} }
tab_char = '\t'
color_tag_start = '{!'
color_tag_end = '!}'
def get_color_pair(fg, bg): def get_color_pair(fg, bg):
return color_pairs[(fg, bg)] return color_pairs[(fg, bg)]
@ -108,14 +112,20 @@ class BadColorString(Exception):
pass pass
def check_tag_count(string):
"""Raise BadColorString if color tag open/close not equal."""
if string.count(color_tag_start) != string.count(color_tag_end):
raise BadColorString('Number of {! is not equal to number of !}')
def replace_tabs(line): def replace_tabs(line):
""" """
Returns a string with tabs replaced with spaces. Returns a string with tabs replaced with spaces.
""" """
for i in range(line.count('\t')): for i in range(line.count(tab_char)):
tab_length = 8 - (len(line[:line.find('\t')]) % 8) tab_length = 8 - (len(line[:line.find(tab_char)]) % 8)
line = line.replace('\t', ' ' * tab_length, 1) line = line.replace(tab_char, b' ' * tab_length, 1)
return line return line
@ -124,9 +134,13 @@ def strip_colors(line):
Returns a string with the color formatting removed. Returns a string with the color formatting removed.
""" """
check_tag_count(line)
# Remove all the color tags # Remove all the color tags
while line.find('{!') != -1: while line.find(color_tag_start) != -1:
line = line[:line.find('{!')] + line[line.find('!}') + 2:] tag_start = line.find(color_tag_start)
tag_end = line.find(color_tag_end) + 2
line = line[:tag_start] + line[tag_end:]
return line return line
@ -136,9 +150,6 @@ def get_line_length(line):
Returns the string length without the color formatting. Returns the string length without the color formatting.
""" """
if line.count('{!') != line.count('!}'):
raise BadColorString('Number of {! is not equal to number of !}')
# Remove all the color tags # Remove all the color tags
line = strip_colors(line) line = strip_colors(line)
@ -152,9 +163,6 @@ def get_line_width(line):
Get width of string considering double width characters Get width of string considering double width characters
""" """
if line.count('{!') != line.count('!}'):
raise BadColorString('Number of {! is not equal to number of !}')
# Remove all the color tags # Remove all the color tags
line = strip_colors(line) line = strip_colors(line)
@ -171,18 +179,17 @@ def parse_color_string(s, encoding='UTF-8'):
:param encoding: the encoding to use on output :param encoding: the encoding to use on output
""" """
if s.count('{!') != s.count('!}'): check_tag_count(s)
raise BadColorString('Number of {! is not equal to number of !}')
ret = [] ret = []
last_color_attr = None last_color_attr = None
# Keep track of where the strings # Keep track of where the strings
while s.find('{!') != -1: while s.find(color_tag_start) != -1:
begin = s.find('{!') begin = s.find(color_tag_start)
if begin > 0: if begin > 0:
ret.append((curses.color_pair(color_pairs[(schemes['input'][0], schemes['input'][1])]), s[:begin])) ret.append((curses.color_pair(color_pairs[(schemes['input'][0], schemes['input'][1])]), s[:begin]))
end = s.find('!}') end = s.find(color_tag_end)
if end == -1: if end == -1:
raise BadColorString('Missing closing "!}"') raise BadColorString('Missing closing "!}"')
@ -258,7 +265,7 @@ def parse_color_string(s, encoding='UTF-8'):
last_color_attr = color_pair last_color_attr = color_pair
# We need to find the text now, so lets try to find another {! and if # We need to find the text now, so lets try to find another {! and if
# there isn't one, then it's the rest of the string # there isn't one, then it's the rest of the string
next_begin = s.find('{!', end) next_begin = s.find(color_tag_start, end)
if next_begin == -1: if next_begin == -1:
ret.append((color_pair, replace_tabs(s[end + 2:]))) ret.append((color_pair, replace_tabs(s[end + 2:])))