summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorluke@maurits.id.au <luke@maurits.id.au@0f58610c-415a-11de-9c03-5d6cfad8e937>2012-07-26 07:59:10 +0000
committerluke@maurits.id.au <luke@maurits.id.au@0f58610c-415a-11de-9c03-5d6cfad8e937>2012-07-26 07:59:10 +0000
commitdfeb0c6957b58ce3834e319701c70a8e4e14d8ab (patch)
tree0e2a9a99f5a3e8ca3e2679e276a926fdfbbc1d73
parente539f3a64cfa8b8906f664933508467556a27fb8 (diff)
downloadpython-prettytable-dfeb0c6957b58ce3834e319701c70a8e4e14d8ab.tar.gz
Made sure field name widths are calculated using the new Unicode-aware code so that e.g. Japanese field names are rendered correctly.
Added a table of Japanese text to the unit tests to help make sure alignment of CJK characters is handled appropriately. git-svn-id: http://prettytable.googlecode.com/svn/trunk@84 0f58610c-415a-11de-9c03-5d6cfad8e937
-rw-r--r--prettytable.py22
-rw-r--r--prettytable_test.py23
2 files changed, 38 insertions, 7 deletions
diff --git a/prettytable.py b/prettytable.py
index 1fbd42d..59d51a2 100644
--- a/prettytable.py
+++ b/prettytable.py
@@ -943,12 +943,7 @@ class PrettyTable(object):
fieldname = field.lower()
else:
fieldname = field
- if self._align[field] == "l":
- bits.append(" " * lpad + fieldname.ljust(width) + " " * rpad)
- elif self._align[field] == "r":
- bits.append(" " * lpad + fieldname.rjust(width) + " " * rpad)
- else:
- bits.append(" " * lpad + fieldname.center(width) + " " * rpad)
+ bits.append(" " * lpad + self._justify(fieldname, width, self._align[field]) + " " * rpad)
if options["border"]:
bits.append(options["vertical_char"])
if options["border"] and options["hrules"] != NONE:
@@ -1138,12 +1133,27 @@ def _char_block_width(char):
# Chinese, Japanese, Korean (common)
if 0x4e00 <= char <= 0x9fff:
return 2
+ # Hangul
+ if 0xac00 <= char <= 0xd7af:
+ return 2
# Combining?
if unicodedata.combining(uni_chr(char)):
return 0
# Hiragana and Katakana
if 0x3040 <= char <= 0x309f or 0x30a0 <= char <= 0x30ff:
return 2
+ # Full-width Latin characters
+ if 0xff01 <= char <= 0xff60:
+ return 2
+ # CJK punctuation
+ if 0x3000 <= char <= 0x303e:
+ return 2
+ # Backspace and delete
+ if char in (0x0008, 0x007f):
+ return -1
+ # Other control characters
+ elif char in (0x0000, 0x001f):
+ return 0
# Take a guess
return 1
diff --git a/prettytable_test.py b/prettytable_test.py
index e5a8696..2aee815 100644
--- a/prettytable_test.py
+++ b/prettytable_test.py
@@ -1,3 +1,5 @@
+# coding=UTF-8
+
import unittest
import sys
sys.path.append("../src/")
@@ -467,9 +469,28 @@ class HtmlOutputTests(unittest.TestCase):
</table>
""".strip()
-class PrintTest(CityDataTest):
+class PrintEnglishTest(CityDataTest):
+
+ def testPrint(self):
+ print()
+ print(self.x)
+
+class PrintJapanestTest(unittest.TestCase):
+
+ def setUp(self):
+
+ self.x = PrettyTable(["Kanji", "Hiragana", "English"])
+ self.x.add_row(["神戸", "こうべ", "Kobe"])
+ self.x.add_row(["京都", "きょうと", "Kyoto"])
+ self.x.add_row(["長崎", "ながさき", "Nagasaki"])
+ self.x.add_row(["名古屋", "なごや", "Nagoya"])
+ self.x.add_row(["大阪", "おおさか", "Osaka"])
+ self.x.add_row(["札幌", "さっぽろ", "Sapporo"])
+ self.x.add_row(["東京", "とうきょう", "Tokyo"])
+ self.x.add_row(["横浜", "よこはま", "Yokohama"])
def testPrint(self):
+ print()
print(self.x)
if __name__ == "__main__":