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