Skip to content

Commit

Permalink
csvread use wcwidth (v2.3.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
markuskimius committed Oct 5, 2024
1 parent 62800d7 commit e2bd002
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions bin/csvread
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import errno
import getopts
import importlib
import importlib_resources
from wcwidth import wcswidth
from csvmagic import libcsv
from itertools import zip_longest

Expand Down Expand Up @@ -272,9 +273,9 @@ def csvread(file):

# Calculate the width of the column names
if has_name:
colwidth = max(len(cell.colname() or '') for cell in cell_list)
colwidth = max(wcswidth(cell.colname() or '') for cell in cell_list)
else:
colwidth = len(str(len(cell_list)))
colwidth = wcswidth(str(len(cell_list)))

# Calculate the width of the column values
if opts.diff == 2 and lastrow:
Expand Down Expand Up @@ -322,7 +323,7 @@ def csvread(file):


def width_of(multiline_text):
return max(len(line) for line in multiline_text.split('\n'))
return max(wcswidth(line) for line in multiline_text.split('\n'))


def height_of(multiline_text):
Expand All @@ -341,14 +342,17 @@ def print_cell(colwidth, colname, valwidth, fromvalue, tovalue, is_firstrow):
fromlines = []

for i, line in enumerate(zip_longest(fromlines, tolines, fillvalue='')):
coffset = len(str(colname)) - wcswidth(str(colname))
voffset = len(str(line[0])) - wcswidth(str(line[0]))

if showdiff and i == 0:
print('%-*s : %-*s => %s' % (colwidth, colname, valwidth, line[0], line[1]))
print('%-*s : %-*s => %s' % (colwidth+coffset, colname, valwidth+voffset, line[0], line[1]))
elif showdiff:
print('%-*s %-*s %s' % (colwidth, '', valwidth, line[0], line[1]))
print('%-*s %-*s %s' % (colwidth+coffset, '', valwidth+voffset, line[0], line[1]))
elif i == 0:
print('%-*s : %-*s' % (colwidth, colname, valwidth, line[1]))
print('%-*s : %-*s' % (colwidth+coffset, colname, valwidth+voffset, line[1]))
else:
print('%-*s %-*s' % (colwidth, '', valwidth, line[1]))
print('%-*s %-*s' % (colwidth+coffset, '', valwidth+voffset, line[1]))


class DefaultTranslatorModule(object):
Expand Down
4 changes: 2 additions & 2 deletions test/users.rsv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ID^FIRST_NAME^LAST_NAME^EMAIL^TEL
1001^"John"^"D�e"^"[email protected]"^"111-111-1111"
ID^이름^성^EMAIL^TEL
1001^"깅동"^"Døe"^"[email protected]"^"111-111-1111"
1002^"Jane"^"Smith"^"[email protected]"^"222-222-2222"

0 comments on commit e2bd002

Please sign in to comment.