Skip to content
This repository was archived by the owner on Jun 23, 2022. It is now read-only.

make padding correct when it contains CJK character #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.pyc
.*.sw?
24 changes: 20 additions & 4 deletions ftplugin/rst_tables.vim
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,39 @@ def pad_fields(row, widths):
others.

"""
widths = map(lambda w: ' %-' + str(w) + 's ', widths)
#widths = map(lambda w: ' %-' + str(w) + 's ', widths)

# Pad all fields using the calculated widths
new_row = []
for i in range(len(row)):
col = row[i]
col = ' %s%s ' % (col.strip().encode('utf8'), ' ' * (widths[i] - get_string_width(col.strip())))
col = col.decode('utf8')
col = widths[i] % col.strip()
col = col.encode('utf8')
new_row.append(col)
return new_row


def _textwrap(s, width):
#FIXME: this is a correct but slow implement
result = []
length = 0
temp = []
for c in s:
length += get_string_width(c)
if length >= width:
result.append(''.join(temp))
temp = [c]
length = get_string_width(c)
else:
temp.append(c)
result.append(''.join(temp))
return result

def reflow_row_contents(row, widths):
new_row = []
for i, field in enumerate(row):
wrapped_lines = textwrap.wrap(field.replace('\n', ' '), widths[i])
field = field.decode('utf8')
wrapped_lines = _textwrap(field.replace('\n', ' '), widths[i])
new_row.append("\n".join(wrapped_lines))
return new_row

Expand Down