Partially port EvTable for Py3.

This commit is contained in:
Ryan Stein 2017-10-29 22:14:22 -04:00
parent a4b902108c
commit 8d0d3a942f

View file

@ -115,13 +115,12 @@ table string.
""" """
from builtins import object, range
from future.utils import listitems from future.utils import listitems
from django.conf import settings from django.conf import settings
from textwrap import TextWrapper from textwrap import TextWrapper
from copy import deepcopy, copy from copy import deepcopy, copy
from evennia.utils.utils import to_unicode, m_len from evennia.utils.utils import to_unicode, m_len, is_iter
from evennia.utils.ansi import ANSIString from evennia.utils.ansi import ANSIString
_DEFAULT_WIDTH = settings.CLIENT_DEFAULT_WIDTH _DEFAULT_WIDTH = settings.CLIENT_DEFAULT_WIDTH
@ -135,7 +134,7 @@ def _to_ansi(obj):
obj (str): Convert incoming text to obj (str): Convert incoming text to
be ANSI aware ANSIStrings. be ANSI aware ANSIStrings.
""" """
if hasattr(obj, "__iter__"): if is_iter(obj):
return [_to_ansi(o) for o in obj] return [_to_ansi(o) for o in obj]
else: else:
return ANSIString(to_unicode(obj)) return ANSIString(to_unicode(obj))
@ -187,13 +186,20 @@ class ANSITextWrapper(TextWrapper):
'use', ' ', 'the', ' ', '-b', ' ', option!' 'use', ' ', 'the', ' ', '-b', ' ', option!'
otherwise. otherwise.
""" """
# only use unicode wrapper # NOTE-PYTHON3: The following code only roughly approximates what this
if self.break_on_hyphens: # function used to do. Regex splitting on ANSIStrings is
pat = self.wordsep_re_uni # dropping ANSI codes, so we're using ANSIString.split
else: # for the time being.
pat = self.wordsep_simple_re_uni #
chunks = pat.split(_to_ansi(text)) # A less hackier solution would be appreciated.
return [chunk for chunk in chunks if chunk] # remove empty chunks chunks = _to_ansi(text).split()
chunks = [chunk+' ' for chunk in chunks if chunk] # remove empty chunks
if len(chunks) > 1:
chunks[-1] = chunks[-1][0:-1]
return chunks
def _wrap_chunks(self, chunks): def _wrap_chunks(self, chunks):
"""_wrap_chunks(chunks : [string]) -> [string] """_wrap_chunks(chunks : [string]) -> [string]