evtable and evform supports ansi, with some limitations due to wrap not being supported by ANSIString at this point.

This commit is contained in:
Griatch 2014-03-09 19:40:46 +01:00
parent 1886d455da
commit 021dca4ba7
3 changed files with 25 additions and 13 deletions

View file

@ -15,7 +15,7 @@ user.
"""
import re
from src.utils import utils
from src.utils.utils import to_str
from src.utils.utils import to_str, to_unicode
# ANSI definitions
@ -468,7 +468,7 @@ class ANSIString(unicode):
decoded = kwargs.get('decoded', False) or hasattr(string, '_raw_string')
if not decoded:
# Completely new ANSI String
clean_string = unicode(parser.parse_ansi(string, strip_ansi=True))
clean_string = to_unicode(parser.parse_ansi(string, strip_ansi=True))
string = parser.parse_ansi(string)
elif hasattr(string, '_clean_string'):
# It's already an ANSIString
@ -483,7 +483,7 @@ class ANSIString(unicode):
else:
# Do this to prevent recursive ANSIStrings.
string = unicode(string)
ansi_string = super(ANSIString, cls).__new__(ANSIString, clean_string)
ansi_string = super(ANSIString, cls).__new__(ANSIString, to_str(clean_string), "utf-8")
ansi_string._raw_string = string
ansi_string._clean_string = clean_string
return ansi_string

View file

@ -17,6 +17,11 @@ has markers in it to denounce fields to fill. The markers map the
absolute size of the field and will be filled with an evtable.Cell
object when displaying the form.
Note, when printing examples with ANSI color, you need to wrap
the output in unicode(), such as print unicode(form). This is
due to a bug in the Python parser and the print statement.
Example of input file testform.py:
FORMCHAR = "x"
@ -87,7 +92,9 @@ Use as follows:
# add the tables to the proper ids in the form
form.map(tables={"A": tableA,
"B": tableB}
print form
# unicode is required since the example contains non-ascii characters
print unicode(form)
This produces the following result:
@ -155,7 +162,6 @@ class EvForm(object):
mapping so as to populate the fields with fixed-width
Cell or Tablets.
"""
def __init__(self, filename=None, cells=None, tables=None, form=None, **kwargs):
"""
@ -401,6 +407,9 @@ class EvForm(object):
def __str__(self):
"Prints the form"
return "\n".join([to_str(line) for line in self.form])
return ANSIString("\n").join([line for line in self.form])
def __unicode__(self):
"prints the form"
return unicode(ANSIString("\n").join([line for line in self.form]))

View file

@ -6,10 +6,9 @@ EvTable
This is an advanced ASCII table creator. It was inspired
by prettytable but shares no code.
WARNING: UNDER DEVELOPMENT. Evtable does currently NOT support
colour ANSI markers in the table. Non-colour tables should
work fully (so make issues if they don't).
Note: to test ANSI colors on the command line you need to
call the printed table in a unicode() call, like print unicode(table).
This is due to a bug in the python interpreter and print.
Example usage:
@ -75,7 +74,7 @@ ANSI-coloured string types.
#from textwrap import wrap
from textwrap import TextWrapper
from copy import deepcopy, copy
from src.utils.utils import to_unicode, to_str
from src.utils.utils import to_unicode
from src.utils.ansi import ANSIString
def make_iter(obj):
@ -408,7 +407,7 @@ class Cell(object):
if 0 < width < len(line):
# replace_whitespace=False, expand_tabs=False is a
# fix for ANSIString not supporting expand_tabs/translate
adjusted_data.extend([ANSIString(part + "{n")
adjusted_data.extend([ANSIString(part )
for part in wrap(line, width=width, drop_whitespace=False)])
else:
adjusted_data.append(line)
@ -619,7 +618,11 @@ class Cell(object):
def __str__(self):
"returns cell contents on string form"
return "\n".join(self.formatted)
return ANSIString("\n").join(self.formatted)
def __unicode__(self):
"returns cell contents"
return unicode(ANSIString("\n").join(self.formatted))
# Main Evtable class