better unicode handling and trimming

This commit is contained in:
Nick Lanham 2011-02-22 18:04:16 +01:00
parent 9e4ea0a671
commit 62421080ef
1 changed files with 20 additions and 2 deletions

View File

@ -62,20 +62,38 @@ def format_priority(prio):
else: else:
return pstring return pstring
def trim_string(string, w): def trim_string(string, w, have_dbls):
if have_dbls:
# have to do this the slow way
chrs = []
width = 4
idx = 0
while width < w:
chrs.append(string[idx])
if unicodedata.east_asian_width(string[idx]) in ['W','F']:
width += 2
else:
width += 1
idx += 1
if width != w:
chrs.pop()
chrs.append('.')
return "%s... "%("".join(chrs))
else:
return "%s... "%(string[0:w-4]) return "%s... "%(string[0:w-4])
def format_column(col, lim): def format_column(col, lim):
dbls = 0 dbls = 0
if haveud and isinstance(col,unicode): if haveud and isinstance(col,unicode):
# might have some double width chars # might have some double width chars
col = unicodedata.normalize("NFC",col)
for c in col: for c in col:
if unicodedata.east_asian_width(c) in ['W','F']: if unicodedata.east_asian_width(c) in ['W','F']:
# found a wide/full char # found a wide/full char
dbls += 1 dbls += 1
size = len(col)+dbls size = len(col)+dbls
if (size >= lim - 1): if (size >= lim - 1):
return trim_string(col,lim) return trim_string(col,lim,dbls>0)
else: else:
return "%s%s"%(col," "*(lim-size)) return "%s%s"%(col," "*(lim-size))