Fix crash when string length makes line longer than terminal width
This commit is contained in:
parent
f876c17efd
commit
fd24e1c17c
|
@ -115,7 +115,7 @@ def strip_colors(line):
|
||||||
|
|
||||||
return line
|
return line
|
||||||
|
|
||||||
def get_line_length(line):
|
def get_line_length(line, encoding="UTF-8"):
|
||||||
"""
|
"""
|
||||||
Returns the string length without the color formatting.
|
Returns the string length without the color formatting.
|
||||||
|
|
||||||
|
@ -123,6 +123,8 @@ def get_line_length(line):
|
||||||
if line.count("{!") != line.count("!}"):
|
if line.count("{!") != line.count("!}"):
|
||||||
raise BadColorString("Number of {! is not equal to number of !}")
|
raise BadColorString("Number of {! is not equal to number of !}")
|
||||||
|
|
||||||
|
line = line.encode(encoding, "replace")
|
||||||
|
|
||||||
# Remove all the color tags
|
# Remove all the color tags
|
||||||
line = strip_colors(line)
|
line = strip_colors(line)
|
||||||
|
|
||||||
|
@ -130,16 +132,19 @@ def get_line_length(line):
|
||||||
line = replace_tabs(line)
|
line = replace_tabs(line)
|
||||||
return len(line)
|
return len(line)
|
||||||
|
|
||||||
def parse_color_string(s):
|
def parse_color_string(s, encoding="UTF-8"):
|
||||||
"""
|
"""
|
||||||
Parses a string and returns a list of 2-tuples (color, string).
|
Parses a string and returns a list of 2-tuples (color, string).
|
||||||
|
|
||||||
:param s:, string to parse
|
:param s:, string to parse
|
||||||
|
:param encoding: the encoding to use on output
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if s.count("{!") != s.count("!}"):
|
if s.count("{!") != s.count("!}"):
|
||||||
raise BadColorString("Number of {! is not equal to number of !}")
|
raise BadColorString("Number of {! is not equal to number of !}")
|
||||||
|
|
||||||
|
s = s.encode(encoding, "replace")
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
# Keep track of where the strings
|
# Keep track of where the strings
|
||||||
col_index = 0
|
col_index = 0
|
||||||
|
|
|
@ -185,6 +185,12 @@ class Command(BaseCommand):
|
||||||
s += "{!success!}"
|
s += "{!success!}"
|
||||||
|
|
||||||
s += " %s" % (fp)
|
s += " %s" % (fp)
|
||||||
|
# Check if this is too long for the screen and reduce the path
|
||||||
|
# if necessary
|
||||||
|
cols = self.console.screen.cols
|
||||||
|
slen = colors.get_line_length(s, self.console.screen.encoding)
|
||||||
|
if slen > cols:
|
||||||
|
s = s.replace(f["path"], f["path"][slen - cols + 1:])
|
||||||
self.console.write(s)
|
self.console.write(s)
|
||||||
|
|
||||||
self.console.write(" {!info!}::Peers")
|
self.console.write(" {!info!}::Peers")
|
||||||
|
|
|
@ -230,7 +230,7 @@ class Screen(CursesStdIO):
|
||||||
"""
|
"""
|
||||||
col = 0
|
col = 0
|
||||||
try:
|
try:
|
||||||
parsed = colors.parse_color_string(string)
|
parsed = colors.parse_color_string(string, self.encoding)
|
||||||
except colors.BadColorString, e:
|
except colors.BadColorString, e:
|
||||||
log.error("Cannot add bad color string %s: %s", string, e)
|
log.error("Cannot add bad color string %s: %s", string, e)
|
||||||
return
|
return
|
||||||
|
@ -239,9 +239,6 @@ class Screen(CursesStdIO):
|
||||||
if index + 1 == len(parsed):
|
if index + 1 == len(parsed):
|
||||||
# This is the last string so lets append some " " to it
|
# This is the last string so lets append some " " to it
|
||||||
s += " " * (self.cols - (col + len(s)) - 1)
|
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)
|
self.stdscr.addstr(row, col, s, color)
|
||||||
col += len(s)
|
col += len(s)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue