Fix up formatting of peers list

This commit is contained in:
Andrew Resch 2009-04-24 21:35:37 +00:00
parent c846d584a0
commit 42242efe66
2 changed files with 32 additions and 8 deletions

View File

@ -74,6 +74,16 @@ def init_colors():
class BadColorString(Exception):
pass
def replace_tabs(line):
"""
Returns a string with tabs replaced with spaces.
"""
for i in range(line.count("\t")):
tab_length = 8 - (len(line[:line.find("\t")]) % 8)
line = line.replace("\t", " " * tab_length, 1)
return line
def get_line_length(line):
"""
Returns the string length without the color formatting.
@ -82,9 +92,12 @@ def get_line_length(line):
if line.count("{{") != line.count ("}}"):
raise BadColorString("Number of {{ does not equal number of }}")
# Remove all the color tags
while line.find("{{") != -1:
line = line[:line.find("{{")] + line[line.find("}}") + 2:]
# Replace tabs with the appropriate amount of spaces
line = replace_tabs(line)
return len(line)
def parse_color_string(s):
@ -97,6 +110,7 @@ def parse_color_string(s):
if s.count("{{") != s.count ("}}"):
raise BadColorString("Number of {{ does not equal number of }}")
ret = []
# Keep track of where the strings
col_index = 0
@ -144,11 +158,12 @@ def parse_color_string(s):
# 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
next_begin = s.find("{{", end)
if next_begin == -1:
ret.append((color_pair, s[end+2:]))
ret.append((color_pair, replace_tabs(s[end+2:])))
break
else:
ret.append((color_pair, s[end+2:next_begin]))
ret.append((color_pair, replace_tabs(s[end+2:next_begin])))
s = s[next_begin:]
if not ret:

View File

@ -175,16 +175,25 @@ class Command(BaseCommand):
s = ""
for peer in status["peers"]:
if peer["seed"]:
s += "%sSeed{{input}}" % colors.state_color["Seeding"]
s += "%sSeed\t{{input}}" % colors.state_color["Seeding"]
else:
s += "%sPeer{{input}}" % colors.state_color["Downloading"]
s += "%sPeer\t{{input}}" % colors.state_color["Downloading"]
s += " " + peer["country"]
s += " " + peer["ip"]
s += "\t" + peer["client"].encode(sys.getdefaultencoding(), "replace")
s += peer["country"] + "\t"
s += peer["ip"]
c = peer["client"].encode(sys.getdefaultencoding(), "replace")
s += "\t" + c
s += "{{input}}\t%s\t%s" % (common.fspeed(peer["up_speed"]), common.fspeed(peer["down_speed"]))
if len(c) < 16:
s += "\t\t"
else:
s += "\t"
s += "%s%s\t%s%s" % (
colors.state_color["Seeding"],
common.fspeed(peer["up_speed"]),
colors.state_color["Downloading"],
common.fspeed(peer["down_speed"]))
s += "\n"
self.console.write(s[:-1])