evtable and evform supports ansi, with some limitations due to wrap not being supported by ANSIString at this point.
This commit is contained in:
parent
1886d455da
commit
021dca4ba7
3 changed files with 25 additions and 13 deletions
|
|
@ -15,7 +15,7 @@ user.
|
||||||
"""
|
"""
|
||||||
import re
|
import re
|
||||||
from src.utils import utils
|
from src.utils import utils
|
||||||
from src.utils.utils import to_str
|
from src.utils.utils import to_str, to_unicode
|
||||||
|
|
||||||
# ANSI definitions
|
# ANSI definitions
|
||||||
|
|
||||||
|
|
@ -468,7 +468,7 @@ class ANSIString(unicode):
|
||||||
decoded = kwargs.get('decoded', False) or hasattr(string, '_raw_string')
|
decoded = kwargs.get('decoded', False) or hasattr(string, '_raw_string')
|
||||||
if not decoded:
|
if not decoded:
|
||||||
# Completely new ANSI String
|
# 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)
|
string = parser.parse_ansi(string)
|
||||||
elif hasattr(string, '_clean_string'):
|
elif hasattr(string, '_clean_string'):
|
||||||
# It's already an ANSIString
|
# It's already an ANSIString
|
||||||
|
|
@ -483,7 +483,7 @@ class ANSIString(unicode):
|
||||||
else:
|
else:
|
||||||
# Do this to prevent recursive ANSIStrings.
|
# Do this to prevent recursive ANSIStrings.
|
||||||
string = unicode(string)
|
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._raw_string = string
|
||||||
ansi_string._clean_string = clean_string
|
ansi_string._clean_string = clean_string
|
||||||
return ansi_string
|
return ansi_string
|
||||||
|
|
|
||||||
|
|
@ -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
|
absolute size of the field and will be filled with an evtable.Cell
|
||||||
object when displaying the form.
|
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:
|
Example of input file testform.py:
|
||||||
|
|
||||||
FORMCHAR = "x"
|
FORMCHAR = "x"
|
||||||
|
|
@ -87,7 +92,9 @@ Use as follows:
|
||||||
# add the tables to the proper ids in the form
|
# add the tables to the proper ids in the form
|
||||||
form.map(tables={"A": tableA,
|
form.map(tables={"A": tableA,
|
||||||
"B": tableB}
|
"B": tableB}
|
||||||
print form
|
|
||||||
|
# unicode is required since the example contains non-ascii characters
|
||||||
|
print unicode(form)
|
||||||
|
|
||||||
This produces the following result:
|
This produces the following result:
|
||||||
|
|
||||||
|
|
@ -155,7 +162,6 @@ class EvForm(object):
|
||||||
mapping so as to populate the fields with fixed-width
|
mapping so as to populate the fields with fixed-width
|
||||||
Cell or Tablets.
|
Cell or Tablets.
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def __init__(self, filename=None, cells=None, tables=None, form=None, **kwargs):
|
def __init__(self, filename=None, cells=None, tables=None, form=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
|
@ -401,6 +407,9 @@ class EvForm(object):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"Prints the form"
|
"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]))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,9 @@ EvTable
|
||||||
This is an advanced ASCII table creator. It was inspired
|
This is an advanced ASCII table creator. It was inspired
|
||||||
by prettytable but shares no code.
|
by prettytable but shares no code.
|
||||||
|
|
||||||
WARNING: UNDER DEVELOPMENT. Evtable does currently NOT support
|
Note: to test ANSI colors on the command line you need to
|
||||||
colour ANSI markers in the table. Non-colour tables should
|
call the printed table in a unicode() call, like print unicode(table).
|
||||||
work fully (so make issues if they don't).
|
This is due to a bug in the python interpreter and print.
|
||||||
|
|
||||||
|
|
||||||
Example usage:
|
Example usage:
|
||||||
|
|
||||||
|
|
@ -75,7 +74,7 @@ ANSI-coloured string types.
|
||||||
#from textwrap import wrap
|
#from textwrap import wrap
|
||||||
from textwrap import TextWrapper
|
from textwrap import TextWrapper
|
||||||
from copy import deepcopy, copy
|
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
|
from src.utils.ansi import ANSIString
|
||||||
|
|
||||||
def make_iter(obj):
|
def make_iter(obj):
|
||||||
|
|
@ -408,7 +407,7 @@ class Cell(object):
|
||||||
if 0 < width < len(line):
|
if 0 < width < len(line):
|
||||||
# replace_whitespace=False, expand_tabs=False is a
|
# replace_whitespace=False, expand_tabs=False is a
|
||||||
# fix for ANSIString not supporting expand_tabs/translate
|
# 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)])
|
for part in wrap(line, width=width, drop_whitespace=False)])
|
||||||
else:
|
else:
|
||||||
adjusted_data.append(line)
|
adjusted_data.append(line)
|
||||||
|
|
@ -619,7 +618,11 @@ class Cell(object):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"returns cell contents on string form"
|
"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
|
# Main Evtable class
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue