diff --git a/src/utils/ansi.py b/src/utils/ansi.py index 0ebdf6a9ff..43d930c0e1 100644 --- a/src/utils/ansi.py +++ b/src/utils/ansi.py @@ -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 diff --git a/src/utils/evform.py b/src/utils/evform.py index 7a7ec457f3..465cbd1633 100644 --- a/src/utils/evform.py +++ b/src/utils/evform.py @@ -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])) diff --git a/src/utils/evtable.py b/src/utils/evtable.py index 8f24011ca6..45cb709233 100644 --- a/src/utils/evtable.py +++ b/src/utils/evtable.py @@ -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