Fix up formatting of peers list
This commit is contained in:
parent
c846d584a0
commit
42242efe66
|
@ -74,6 +74,16 @@ def init_colors():
|
||||||
class BadColorString(Exception):
|
class BadColorString(Exception):
|
||||||
pass
|
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):
|
def get_line_length(line):
|
||||||
"""
|
"""
|
||||||
Returns the string length without the color formatting.
|
Returns the string length without the color formatting.
|
||||||
|
@ -82,9 +92,12 @@ def get_line_length(line):
|
||||||
if line.count("{{") != line.count ("}}"):
|
if line.count("{{") != line.count ("}}"):
|
||||||
raise BadColorString("Number of {{ does not equal number of }}")
|
raise BadColorString("Number of {{ does not equal number of }}")
|
||||||
|
|
||||||
|
# Remove all the color tags
|
||||||
while line.find("{{") != -1:
|
while line.find("{{") != -1:
|
||||||
line = line[:line.find("{{")] + line[line.find("}}") + 2:]
|
line = line[:line.find("{{")] + line[line.find("}}") + 2:]
|
||||||
|
|
||||||
|
# Replace tabs with the appropriate amount of spaces
|
||||||
|
line = replace_tabs(line)
|
||||||
return len(line)
|
return len(line)
|
||||||
|
|
||||||
def parse_color_string(s):
|
def parse_color_string(s):
|
||||||
|
@ -97,6 +110,7 @@ def parse_color_string(s):
|
||||||
if s.count("{{") != s.count ("}}"):
|
if s.count("{{") != s.count ("}}"):
|
||||||
raise BadColorString("Number of {{ does not equal number of }}")
|
raise BadColorString("Number of {{ does not equal number of }}")
|
||||||
|
|
||||||
|
|
||||||
ret = []
|
ret = []
|
||||||
# Keep track of where the strings
|
# Keep track of where the strings
|
||||||
col_index = 0
|
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
|
# 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("{{", end)
|
||||||
|
|
||||||
if next_begin == -1:
|
if next_begin == -1:
|
||||||
ret.append((color_pair, s[end+2:]))
|
ret.append((color_pair, replace_tabs(s[end+2:])))
|
||||||
break
|
break
|
||||||
else:
|
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:]
|
s = s[next_begin:]
|
||||||
|
|
||||||
if not ret:
|
if not ret:
|
||||||
|
|
|
@ -175,16 +175,25 @@ class Command(BaseCommand):
|
||||||
s = ""
|
s = ""
|
||||||
for peer in status["peers"]:
|
for peer in status["peers"]:
|
||||||
if peer["seed"]:
|
if peer["seed"]:
|
||||||
s += "%sSeed{{input}}" % colors.state_color["Seeding"]
|
s += "%sSeed\t{{input}}" % colors.state_color["Seeding"]
|
||||||
else:
|
else:
|
||||||
s += "%sPeer{{input}}" % colors.state_color["Downloading"]
|
s += "%sPeer\t{{input}}" % colors.state_color["Downloading"]
|
||||||
|
|
||||||
s += " " + peer["country"]
|
s += peer["country"] + "\t"
|
||||||
s += " " + peer["ip"]
|
s += peer["ip"]
|
||||||
s += "\t" + peer["client"].encode(sys.getdefaultencoding(), "replace")
|
|
||||||
|
|
||||||
|
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"
|
s += "\n"
|
||||||
|
|
||||||
self.console.write(s[:-1])
|
self.console.write(s[:-1])
|
||||||
|
|
Loading…
Reference in New Issue