Skip to content

Commit

Permalink
potential unicode width fix
Browse files Browse the repository at this point in the history
- fixes #53
casperdcl committed Sep 3, 2021
1 parent 6d4f3ea commit b67f61a
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 4 additions & 3 deletions gitfame/_gitfame.py
Original file line number Diff line number Diff line change
@@ -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))

10 changes: 8 additions & 2 deletions gitfame/_utils.py
Original file line number Diff line number Diff line change
@@ -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:
2 changes: 1 addition & 1 deletion tests/tests_utils.py
Original file line number Diff line number Diff line change
@@ -60,4 +60,4 @@ def test_integer_stats():

def test_print():
"""Test printing of unicode"""
_utils.print_unicode("\x81")
_utils.uniprint("\x81")

0 comments on commit b67f61a

Please sign in to comment.