diff --git a/example1.py b/example1.py index daf1fbf9..b5b57b7b 100755 --- a/example1.py +++ b/example1.py @@ -6,7 +6,11 @@ from __future__ import print_function -from terminaltables import AsciiTable, DoubleTable, SingleTable +from terminaltables import AsciiTable, DoubleTable, SingleTable, UnicodeSingleTable +try: + from imp import reload +except: + pass # python 2 has it built-in TABLE_DATA = ( ('Platform', 'Years', 'Notes'), @@ -37,6 +41,20 @@ def main(): print(table_instance.table) print() + # UnicodeTable + table_instance = UnicodeSingleTable(TABLE_DATA, title) + table_instance.justify_columns[2] = 'right' + # table_instance.inner_row_border = True + print(table_instance.table) + print() + if __name__ == '__main__': + try: + import sys + reload(sys) + sys.setdefaultencoding('utf8') + except: + pass + main() diff --git a/terminaltables/__init__.py b/terminaltables/__init__.py index 6cea8138..93caf655 100644 --- a/terminaltables/__init__.py +++ b/terminaltables/__init__.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """Generate simple tables in terminals from a nested list of strings. Use SingleTable or DoubleTable instead of AsciiTable for box-drawing characters. @@ -10,8 +11,10 @@ from terminaltables.github_table import GithubFlavoredMarkdownTable # noqa from terminaltables.other_tables import DoubleTable # noqa from terminaltables.other_tables import SingleTable # noqa +from terminaltables.other_tables import UnicodeSingleTable # noqa from terminaltables.other_tables import PorcelainTable # noqa + __author__ = '@Robpol86' __license__ = 'MIT' __version__ = '3.1.0' diff --git a/terminaltables/ascii_table.py b/terminaltables/ascii_table.py index 36239184..aa688cfb 100644 --- a/terminaltables/ascii_table.py +++ b/terminaltables/ascii_table.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """AsciiTable is the main table class. To be inherited by other tables. Define convenience methods here.""" from terminaltables.base_table import BaseTable diff --git a/terminaltables/base_table.py b/terminaltables/base_table.py index 281d5a3d..4d29e81e 100644 --- a/terminaltables/base_table.py +++ b/terminaltables/base_table.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """Base table class. Define just the bare minimum to build tables.""" from terminaltables.build import build_border, build_row, flatten diff --git a/terminaltables/build.py b/terminaltables/build.py index 6b23b2f5..c0eab5fa 100644 --- a/terminaltables/build.py +++ b/terminaltables/build.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """Combine cells into rows.""" from terminaltables.width_and_alignment import visible_width diff --git a/terminaltables/github_table.py b/terminaltables/github_table.py index 7eb1be79..911e069f 100644 --- a/terminaltables/github_table.py +++ b/terminaltables/github_table.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """GithubFlavoredMarkdownTable class.""" from terminaltables.ascii_table import AsciiTable diff --git a/terminaltables/other_tables.py b/terminaltables/other_tables.py index 50c0bcdf..6bb68e25 100644 --- a/terminaltables/other_tables.py +++ b/terminaltables/other_tables.py @@ -1,9 +1,60 @@ +# coding=UTF-8 """Additional simple tables defined here.""" from terminaltables.ascii_table import AsciiTable from terminaltables.terminal_io import IS_WINDOWS +class UnicodeSingleTable(AsciiTable): + """Draw a table using UTF-8 border characters. Table borders won't have any gaps between lines. + + Similar to the tables shown on PC BIOS boot messages, but not double-lined. + + For Python 2.7 and before you might need to force the encoding on stdout to be UTF-8. + That can be done with the following commands, although they are not recommended. + reload(sys) + sys.setdefaultencoding('utf8') + + """ + + CHAR_F_INNER_HORIZONTAL = u'─' + CHAR_F_INNER_INTERSECT = u'┼' + CHAR_F_INNER_VERTICAL = u'│' + CHAR_F_OUTER_LEFT_INTERSECT = u'├' + CHAR_F_OUTER_LEFT_VERTICAL = u'┌' + CHAR_F_OUTER_RIGHT_INTERSECT = u'┤' + CHAR_F_OUTER_RIGHT_VERTICAL = u'┐' + CHAR_H_INNER_HORIZONTAL = u'─' + CHAR_H_INNER_INTERSECT = u'┼' + CHAR_H_INNER_VERTICAL = '│' + CHAR_H_OUTER_LEFT_INTERSECT = u'├' + CHAR_H_OUTER_LEFT_VERTICAL = u'│' + CHAR_H_OUTER_RIGHT_INTERSECT = u'┤' + CHAR_H_OUTER_RIGHT_VERTICAL = u'│' + CHAR_INNER_HORIZONTAL = u'─' + CHAR_INNER_INTERSECT = u'┼' + CHAR_INNER_VERTICAL = u'│' + CHAR_OUTER_BOTTOM_HORIZONTAL = u'─' + CHAR_OUTER_BOTTOM_INTERSECT = u'┴' + CHAR_OUTER_BOTTOM_LEFT = u'└' + CHAR_OUTER_BOTTOM_RIGHT = u'┘' + CHAR_OUTER_LEFT_INTERSECT = u'├' + CHAR_OUTER_LEFT_VERTICAL = u'│' + CHAR_OUTER_RIGHT_INTERSECT = u'┤' + CHAR_OUTER_RIGHT_VERTICAL = u'│' + CHAR_OUTER_TOP_HORIZONTAL = u'─' + CHAR_OUTER_TOP_INTERSECT = u'┬' + CHAR_OUTER_TOP_LEFT = u'┌' + CHAR_OUTER_TOP_RIGHT = u'┐' + + @property + def table(self): + """Return a large string of the entire table ready to be printed to the terminal.""" + ascii_table = super(UnicodeSingleTable, self).table + optimized = ascii_table.replace('\033(B\033(0', '') + return optimized + + class UnixTable(AsciiTable): """Draw a table using box-drawing characters on Unix platforms. Table borders won't have any gaps between lines. diff --git a/terminaltables/terminal_io.py b/terminaltables/terminal_io.py index 8b8c10dc..2f74ce82 100644 --- a/terminaltables/terminal_io.py +++ b/terminaltables/terminal_io.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """Get info about the current terminal window/screen buffer.""" import ctypes diff --git a/terminaltables/width_and_alignment.py b/terminaltables/width_and_alignment.py index 057e800f..03a42f2f 100644 --- a/terminaltables/width_and_alignment.py +++ b/terminaltables/width_and_alignment.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """Functions that handle alignment, padding, widths, etc.""" import re