diff --git a/gitfame/_gitfame.py b/gitfame/_gitfame.py index 873a27e..e8d8871 100755 --- a/gitfame/_gitfame.py +++ b/gitfame/_gitfame.py @@ -73,9 +73,10 @@ fext, int_cast_or_len, merge_stats, - print_unicode, string_types, tqdm, + unilen, + uniprint, ) # version detector. Precedence: installed dist, git, 'UNKNOWN' @@ -212,7 +213,7 @@ def tabulate( if backend not in tabber.tabulate_formats: raise ValueError("Unknown backend:%s" % backend) log.debug("backend:tabulate:" + backend) - COL_LENS = [max(len(Str(i[j])) for i in [COL_NAMES] + tab) + COL_LENS = [max(unilen(Str(i[j])) for i in [COL_NAMES] + tab) for j in range(len(COL_NAMES))] COL_LENS[0] = min( TERM_WIDTH - sum(COL_LENS[1:]) - len(COL_LENS) * 3 - 4, @@ -490,7 +491,7 @@ def run(args): # extns.update([fext(i) for i in stats["files"]]) # log.debug(extns) - print_unicode(tabulate( + uniprint(tabulate( auth_stats, stats_tot, args.sort, args.bytype, args.format, cost, args.enum)) diff --git a/gitfame/_utils.py b/gitfame/_utils.py index 0dcf92e..678534d 100644 --- a/gitfame/_utils.py +++ b/gitfame/_utils.py @@ -4,6 +4,7 @@ import subprocess import sys from functools import partial +from unicodedata import east_asian_width from tqdm import tqdm as tqdm_std from tqdm.utils import _screen_shape_wrapper @@ -32,7 +33,8 @@ __date__ = "2016-2020" __licence__ = "[MPLv2.0](https://mozilla.org/MPL/2.0/)" __all__ = ["TERM_WIDTH", "int_cast_or_len", "Max", "fext", "_str", "tqdm", - "tighten", "check_output", "print_unicode", "StringIO", "Str"] + "tighten", "check_output", "uniprint", "unilen", + "StringIO", "Str"] __copyright__ = ' '.join(("Copyright (c)", __date__, __author__, __licence__)) __license__ = __licence__ # weird foreign language @@ -60,6 +62,10 @@ def blank_col(rows, i, blanks): return all(r[i] in blanks for r in rows) +def unilen(s): + return sum(2 if east_asian_width(ch) in 'FW' else 1 for ch in _str(s)) + + def tighten(t, max_width=256, blanks=' -=', seps='|+'): """Tighten (default: grid) table padding""" rows = t.strip().split('\n') @@ -131,7 +137,7 @@ def Max(it, empty_default=0): raise # pragma: no cover -def print_unicode(msg, end='\n', err='?'): +def uniprint(msg, end='\n', err='?'): """print `msg`, replacing unicode characters with `err` upon failure""" for c in msg: try: diff --git a/tests/tests_utils.py b/tests/tests_utils.py index cce5d3d..886f10d 100644 --- a/tests/tests_utils.py +++ b/tests/tests_utils.py @@ -60,4 +60,4 @@ def test_integer_stats(): def test_print(): """Test printing of unicode""" - _utils.print_unicode("\x81") + _utils.uniprint("\x81")