Fix bug in ANSIString
This commit is contained in:
parent
458df00272
commit
642f17be68
6 changed files with 32 additions and 45 deletions
|
|
@ -187,20 +187,27 @@ class CmdHelp(Command):
|
||||||
respectively. You can override this method to return a
|
respectively. You can override this method to return a
|
||||||
custom display of the list of commands and topics.
|
custom display of the list of commands and topics.
|
||||||
"""
|
"""
|
||||||
|
category_clr = "|w"
|
||||||
|
topic_clr = "|G"
|
||||||
width = self.client_width()
|
width = self.client_width()
|
||||||
grid = []
|
grid = []
|
||||||
verbatim_elements = []
|
verbatim_elements = []
|
||||||
for category in sorted(set(list(hdict_cmds.keys()) + list(hdict_db.keys()))):
|
for category in sorted(set(list(hdict_cmds.keys()) + list(hdict_db.keys()))):
|
||||||
|
|
||||||
category_str = f"-- {category.title()} "
|
category_str = f"-- {category.title()} "
|
||||||
grid.append(ANSIString("|w" + category_str + "-" * (width - len(category_str)) + "|G"))
|
grid.append(ANSIString(
|
||||||
|
category_clr
|
||||||
|
+ category_str
|
||||||
|
+ "-" * (width - len(category_str))
|
||||||
|
+ topic_clr))
|
||||||
verbatim_elements.append(len(grid) - 1)
|
verbatim_elements.append(len(grid) - 1)
|
||||||
|
|
||||||
entries = sorted(set(hdict_cmds.get(category, []) + hdict_db.get(category, [])))
|
entries = sorted(set(hdict_cmds.get(category, []) + hdict_db.get(category, [])))
|
||||||
grid.extend(entries)
|
grid.extend(entries)
|
||||||
|
|
||||||
gridrows = format_grid(grid, width, sep=" ", verbatim_elements=verbatim_elements)
|
gridrows = format_grid(grid, width, sep=" ", verbatim_elements=verbatim_elements)
|
||||||
return "\n".join(gridrows)
|
gridrows = ANSIString("\n").join(gridrows)
|
||||||
|
return gridrows
|
||||||
|
|
||||||
def check_show_help(self, cmd, caller):
|
def check_show_help(self, cmd, caller):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -266,7 +266,7 @@ class TestGeneral(CommandTest):
|
||||||
|
|
||||||
class TestHelp(CommandTest):
|
class TestHelp(CommandTest):
|
||||||
def test_help(self):
|
def test_help(self):
|
||||||
self.call(help.CmdHelp(), "", "Command help entries", cmdset=CharacterCmdSet())
|
self.call(help.CmdHelp(), "", "Admin", cmdset=CharacterCmdSet())
|
||||||
|
|
||||||
def test_set_help(self):
|
def test_set_help(self):
|
||||||
self.call(
|
self.call(
|
||||||
|
|
|
||||||
|
|
@ -822,7 +822,7 @@ class ANSIString(str, metaclass=ANSIMeta):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if not offset:
|
if not offset:
|
||||||
return []
|
return iterable
|
||||||
return [i + offset for i in iterable]
|
return [i + offset for i in iterable]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ from django.conf import settings
|
||||||
from django.db.models.query import QuerySet
|
from django.db.models.query import QuerySet
|
||||||
from evennia import Command, CmdSet
|
from evennia import Command, CmdSet
|
||||||
from evennia.commands import cmdhandler
|
from evennia.commands import cmdhandler
|
||||||
|
from evennia.utils.ansi import ANSIString
|
||||||
from evennia.utils.utils import make_iter, inherits_from, justify
|
from evennia.utils.utils import make_iter, inherits_from, justify
|
||||||
|
|
||||||
_CMD_NOMATCH = cmdhandler.CMD_NOMATCH
|
_CMD_NOMATCH = cmdhandler.CMD_NOMATCH
|
||||||
|
|
@ -42,6 +43,8 @@ _SCREEN_HEIGHT = settings.CLIENT_DEFAULT_HEIGHT
|
||||||
|
|
||||||
_EVTABLE = None
|
_EVTABLE = None
|
||||||
|
|
||||||
|
_LBR = ANSIString("\n")
|
||||||
|
|
||||||
# text
|
# text
|
||||||
|
|
||||||
_DISPLAY = """{text}
|
_DISPLAY = """{text}
|
||||||
|
|
@ -332,7 +335,7 @@ class EvMore(object):
|
||||||
# no justification. Simple division by line
|
# no justification. Simple division by line
|
||||||
lines = text.split("\n")
|
lines = text.split("\n")
|
||||||
|
|
||||||
self._data = ["\n".join(lines[i: i + self.height])
|
self._data = [_LBR.join(lines[i: i + self.height])
|
||||||
for i in range(0, len(lines), self.height)]
|
for i in range(0, len(lines), self.height)]
|
||||||
self._npages = len(self._data)
|
self._npages = len(self._data)
|
||||||
self._paginator = self.paginator_index
|
self._paginator = self.paginator_index
|
||||||
|
|
|
||||||
|
|
@ -99,30 +99,6 @@ class TestMLen(TestCase):
|
||||||
self.assertEqual(utils.m_len({"hello": True, "Goodbye": False}), 2)
|
self.assertEqual(utils.m_len({"hello": True, "Goodbye": False}), 2)
|
||||||
|
|
||||||
|
|
||||||
class TestANSIString(TestCase):
|
|
||||||
"""
|
|
||||||
Verifies that ANSIString's string-API works as intended.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.example_raw = "|relectric |cboogaloo|n"
|
|
||||||
self.example_ansi = ANSIString(self.example_raw)
|
|
||||||
self.example_str = "electric boogaloo"
|
|
||||||
self.example_output = "\x1b[1m\x1b[31melectric \x1b[1m\x1b[36mboogaloo\x1b[0m"
|
|
||||||
|
|
||||||
def test_length(self):
|
|
||||||
self.assertEqual(len(self.example_ansi), 17)
|
|
||||||
|
|
||||||
def test_clean(self):
|
|
||||||
self.assertEqual(self.example_ansi.clean(), self.example_str)
|
|
||||||
|
|
||||||
def test_raw(self):
|
|
||||||
self.assertEqual(self.example_ansi.raw(), self.example_output)
|
|
||||||
|
|
||||||
def test_format(self):
|
|
||||||
self.assertEqual(f"{self.example_ansi:0<20}", self.example_output + "000")
|
|
||||||
|
|
||||||
|
|
||||||
class TestTimeformat(TestCase):
|
class TestTimeformat(TestCase):
|
||||||
"""
|
"""
|
||||||
Default function header from utils.py:
|
Default function header from utils.py:
|
||||||
|
|
@ -287,24 +263,27 @@ class TestFormatGrid(TestCase):
|
||||||
def test_even_grid(self):
|
def test_even_grid(self):
|
||||||
"""Grid with small variations"""
|
"""Grid with small variations"""
|
||||||
elements = self._generate_elements(3, 1, 30)
|
elements = self._generate_elements(3, 1, 30)
|
||||||
result = utils.format_grid(elements, width=78)
|
rows = utils.format_grid(elements, width=78)
|
||||||
rows = result.split("\n")
|
for row in rows:
|
||||||
|
print(f"'{row}'", len(row))
|
||||||
self.assertEqual(len(rows), 3)
|
self.assertEqual(len(rows), 3)
|
||||||
self.assertTrue(all(len(row) == 78 for row in rows))
|
self.assertTrue(all(len(row) == 78 for row in rows))
|
||||||
|
|
||||||
def test_disparate_grid(self):
|
def test_disparate_grid(self):
|
||||||
"""Grid with big variations"""
|
"""Grid with big variations"""
|
||||||
elements = self._generate_elements(3, 15, 30)
|
elements = self._generate_elements(3, 15, 30)
|
||||||
result = utils.format_grid(elements, width=82, sep=" ")
|
rows = utils.format_grid(elements, width=82, sep=" ")
|
||||||
rows = result.split("\n")
|
for row in rows:
|
||||||
|
print(f"'{row}'", len(row))
|
||||||
self.assertEqual(len(rows), 8)
|
self.assertEqual(len(rows), 8)
|
||||||
self.assertTrue(all(len(row) == 82 for row in rows))
|
self.assertTrue(all(len(row) == 82 for row in rows))
|
||||||
|
|
||||||
def test_huge_grid(self):
|
def test_huge_grid(self):
|
||||||
"""Grid with very long strings"""
|
"""Grid with very long strings"""
|
||||||
elements = self._generate_elements(70, 20, 30)
|
elements = self._generate_elements(70, 20, 30)
|
||||||
result = utils.format_grid(elements, width=78)
|
rows = utils.format_grid(elements, width=78)
|
||||||
rows = result.split("\n")
|
for row in rows:
|
||||||
|
print(f"'{row}'", len(row))
|
||||||
self.assertEqual(len(rows), 30)
|
self.assertEqual(len(rows), 30)
|
||||||
self.assertTrue(all(len(row) == 78 for row in rows))
|
self.assertTrue(all(len(row) == 78 for row in rows))
|
||||||
|
|
||||||
|
|
@ -313,21 +292,20 @@ class TestFormatGrid(TestCase):
|
||||||
elements = ("alias", "batchcode", "batchcommands", "cmdsets",
|
elements = ("alias", "batchcode", "batchcommands", "cmdsets",
|
||||||
"copy", "cpattr", "desc", "destroy", "dig",
|
"copy", "cpattr", "desc", "destroy", "dig",
|
||||||
"examine", "find", "force", "lock")
|
"examine", "find", "force", "lock")
|
||||||
result = utils.format_grid(elements, width=78)
|
rows = utils.format_grid(elements, width=78)
|
||||||
rows = result.split("\n")
|
for row in rows:
|
||||||
|
print(f"'{row}'", len(row))
|
||||||
self.assertEqual(len(rows), 2)
|
self.assertEqual(len(rows), 2)
|
||||||
for element in elements:
|
for element in elements:
|
||||||
self.assertTrue(element in result, f"element {element} is missing.")
|
self.assertTrue(element in rows, f"element {element} is missing.")
|
||||||
|
|
||||||
def test_breakline(self):
|
def test_breakline(self):
|
||||||
"""Grid with line-long elements in middle"""
|
"""Grid with line-long elements in middle"""
|
||||||
elements = self._generate_elements(6, 4, 30)
|
elements = self._generate_elements(6, 4, 30)
|
||||||
elements[10] = elements[20] = "-" * 78
|
elements[10] = elements[20] = "-" * 78
|
||||||
# from pudb import debugger
|
rows = utils.format_grid(elements, width=78)
|
||||||
# debugger.Debugger().set_trace()
|
for row in rows:
|
||||||
result = utils.format_grid(elements, width=78)
|
print(f"'{row}'", len(row))
|
||||||
rows = result.split("\n")
|
|
||||||
self.assertEqual(len(rows), 8)
|
self.assertEqual(len(rows), 8)
|
||||||
for element in elements:
|
for element in elements:
|
||||||
self.assertTrue(element in result, f"element {element} is missing.")
|
self.assertTrue(element in rows, f"element {element} is missing.")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -153,7 +153,6 @@ def crop(text, width=None, suffix="[...]"):
|
||||||
if ltext <= width:
|
if ltext <= width:
|
||||||
return text
|
return text
|
||||||
else:
|
else:
|
||||||
from evennia import set_trace;set_trace()
|
|
||||||
lsuffix = len(suffix)
|
lsuffix = len(suffix)
|
||||||
text = text[:width] if lsuffix >= width else "%s%s" % (text[: width - lsuffix], suffix)
|
text = text[:width] if lsuffix >= width else "%s%s" % (text[: width - lsuffix], suffix)
|
||||||
return to_str(text)
|
return to_str(text)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue