From 5da3e37c41b7a80f6f97b3e496b46b3bfe08b0f6 Mon Sep 17 00:00:00 2001 From: Cristi Libotean Date: Wed, 13 Sep 2017 15:17:33 +0300 Subject: [PATCH 1/8] Added UnicodeSingleTable that uses UTF-8 characters for borders, works on both Linux and Windows systems. Also made sure the files use UTF-8 encoding --- example1.py | 9 ++++- terminaltables/__init__.py | 3 ++ terminaltables/ascii_table.py | 1 + terminaltables/base_table.py | 1 + terminaltables/build.py | 1 + terminaltables/github_table.py | 1 + terminaltables/other_tables.py | 51 +++++++++++++++++++++++++++ terminaltables/terminal_io.py | 1 + terminaltables/width_and_alignment.py | 1 + 9 files changed, 68 insertions(+), 1 deletion(-) diff --git a/example1.py b/example1.py index daf1fbf9..7aca65c3 100755 --- a/example1.py +++ b/example1.py @@ -6,7 +6,7 @@ from __future__ import print_function -from terminaltables import AsciiTable, DoubleTable, SingleTable +from terminaltables import AsciiTable, DoubleTable, SingleTable, UnicodeSingleTable TABLE_DATA = ( ('Platform', 'Years', 'Notes'), @@ -37,6 +37,13 @@ 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__': 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..b9d80841 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 From cb4b08e02fcf445ae33ef6fb8220087a9ebb3181 Mon Sep 17 00:00:00 2001 From: Cristi Libotean Date: Wed, 13 Sep 2017 15:20:21 +0300 Subject: [PATCH 2/8] Updated version --- terminaltables/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terminaltables/__init__.py b/terminaltables/__init__.py index 93caf655..98377e76 100644 --- a/terminaltables/__init__.py +++ b/terminaltables/__init__.py @@ -17,4 +17,4 @@ __author__ = '@Robpol86' __license__ = 'MIT' -__version__ = '3.1.0' +__version__ = '3.2.0' From 15fbf862580f6322f37a69e3c1da91f5919d2214 Mon Sep 17 00:00:00 2001 From: Cristi Libotean Date: Wed, 13 Sep 2017 15:36:11 +0300 Subject: [PATCH 3/8] Fixed example to work on python 2.7 --- example1.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/example1.py b/example1.py index 7aca65c3..f0cd240d 100755 --- a/example1.py +++ b/example1.py @@ -46,4 +46,11 @@ def main(): if __name__ == '__main__': + try: + import sys + reload(sys) + sys.setdefaultencoding('utf8') + except: + pass + main() From b7f8b8129bbbd8a7888d4a5543b140754f64e238 Mon Sep 17 00:00:00 2001 From: Cristi Libotean Date: Wed, 13 Sep 2017 15:41:34 +0300 Subject: [PATCH 4/8] Ooops, did not know the version string is checked against --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a9474446..c7c4c0c1 100755 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ INSTALL_REQUIRES = [] LICENSE = 'MIT' NAME = IMPORT = 'terminaltables' -VERSION = '3.1.0' +VERSION = '3.2.0' def readme(path='README.rst'): From 90db02745594ad6a5eba599b84e5265b957905d1 Mon Sep 17 00:00:00 2001 From: Cristi Libotean Date: Wed, 13 Sep 2017 15:45:27 +0300 Subject: [PATCH 5/8] Reverted version upgrade, Robpol86 should bump it --- setup.py | 2 +- terminaltables/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index c7c4c0c1..a9474446 100755 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ INSTALL_REQUIRES = [] LICENSE = 'MIT' NAME = IMPORT = 'terminaltables' -VERSION = '3.2.0' +VERSION = '3.1.0' def readme(path='README.rst'): diff --git a/terminaltables/__init__.py b/terminaltables/__init__.py index 98377e76..93caf655 100644 --- a/terminaltables/__init__.py +++ b/terminaltables/__init__.py @@ -17,4 +17,4 @@ __author__ = '@Robpol86' __license__ = 'MIT' -__version__ = '3.2.0' +__version__ = '3.1.0' From 1d08da539dc36f1e00791f80fe463a98cd766f3b Mon Sep 17 00:00:00 2001 From: Cristi Libotean Date: Wed, 13 Sep 2017 16:38:36 +0300 Subject: [PATCH 6/8] Attempt to fix lint --- example1.py | 6 +++++- terminaltables/other_tables.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/example1.py b/example1.py index f0cd240d..6a532721 100755 --- a/example1.py +++ b/example1.py @@ -7,6 +7,10 @@ from __future__ import print_function 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'), @@ -40,7 +44,7 @@ def main(): # UnicodeTable table_instance = UnicodeSingleTable(TABLE_DATA, title) table_instance.justify_columns[2] = 'right' - #table_instance.inner_row_border = True + # table_instance.inner_row_border = True print(table_instance.table) print() diff --git a/terminaltables/other_tables.py b/terminaltables/other_tables.py index b9d80841..504e1530 100644 --- a/terminaltables/other_tables.py +++ b/terminaltables/other_tables.py @@ -1,4 +1,4 @@ -#coding=UTF-8 +# coding=UTF-8 """Additional simple tables defined here.""" from terminaltables.ascii_table import AsciiTable From 1ccd6d55577a9cc2bd30577250f8bcc32b682749 Mon Sep 17 00:00:00 2001 From: Cristi Libotean Date: Wed, 13 Sep 2017 16:40:33 +0300 Subject: [PATCH 7/8] 2 spaces before comment --- example1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example1.py b/example1.py index 6a532721..b5b57b7b 100755 --- a/example1.py +++ b/example1.py @@ -10,7 +10,7 @@ try: from imp import reload except: - pass # python 2 has it built-in + pass # python 2 has it built-in TABLE_DATA = ( ('Platform', 'Years', 'Notes'), From 3e91b91398b4f23e8fea59d7db51a40b0fcdc0d2 Mon Sep 17 00:00:00 2001 From: Cristi Libotean Date: Wed, 13 Sep 2017 16:45:00 +0300 Subject: [PATCH 8/8] Last warning --- terminaltables/other_tables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terminaltables/other_tables.py b/terminaltables/other_tables.py index 504e1530..6bb68e25 100644 --- a/terminaltables/other_tables.py +++ b/terminaltables/other_tables.py @@ -12,7 +12,7 @@ class UnicodeSingleTable(AsciiTable): 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) + reload(sys) sys.setdefaultencoding('utf8') """