comparison mercurial/patch.py @ 11611:4f5a6df2af92 stable

i18n: use encoding.colwidth() for correct column width Some encoding and language combinations (e.g.: UTF-8 and Japanese) cause encoding characters into sequence of bytes more than column width of them. So, encoding.colwidth() should be applied instread of len() on i18n strings. In addition to it, formatting by '%*s'/'%-*s' also uses "number of bytes" to calculate space padding size, and should be fixed, too.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Sun, 18 Jul 2010 01:06:50 +0900
parents 9916263d9a60
children 88b89ace643b
comparison
equal deleted inserted replaced
11605:ce95d8b87d22 11611:4f5a6df2af92
9 import cStringIO, email.Parser, os, re 9 import cStringIO, email.Parser, os, re
10 import tempfile, zlib 10 import tempfile, zlib
11 11
12 from i18n import _ 12 from i18n import _
13 from node import hex, nullid, short 13 from node import hex, nullid, short
14 import base85, cmdutil, mdiff, util, diffhelpers, copies 14 import base85, cmdutil, mdiff, util, diffhelpers, copies, encoding
15 15
16 gitre = re.compile('diff --git a/(.*) b/(.*)') 16 gitre = re.compile('diff --git a/(.*) b/(.*)')
17 17
18 class PatchError(Exception): 18 class PatchError(Exception):
19 pass 19 pass
1642 stats = list(diffstatdata(lines)) 1642 stats = list(diffstatdata(lines))
1643 1643
1644 maxtotal, maxname = 0, 0 1644 maxtotal, maxname = 0, 0
1645 totaladds, totalremoves = 0, 0 1645 totaladds, totalremoves = 0, 0
1646 hasbinary = False 1646 hasbinary = False
1647 for filename, adds, removes, isbinary in stats: 1647
1648 sized = [(filename, adds, removes, isbinary, encoding.colwidth(filename))
1649 for filename, adds, removes, isbinary in stats]
1650
1651 for filename, adds, removes, isbinary, namewidth in sized:
1648 totaladds += adds 1652 totaladds += adds
1649 totalremoves += removes 1653 totalremoves += removes
1650 maxname = max(maxname, len(filename)) 1654 maxname = max(maxname, namewidth)
1651 maxtotal = max(maxtotal, adds + removes) 1655 maxtotal = max(maxtotal, adds + removes)
1652 if isbinary: 1656 if isbinary:
1653 hasbinary = True 1657 hasbinary = True
1654 1658
1655 countwidth = len(str(maxtotal)) 1659 countwidth = len(str(maxtotal))
1665 # If diffstat runs out of room it doesn't print anything, 1669 # If diffstat runs out of room it doesn't print anything,
1666 # which isn't very useful, so always print at least one + or - 1670 # which isn't very useful, so always print at least one + or -
1667 # if there were at least some changes. 1671 # if there were at least some changes.
1668 return max(i * graphwidth // maxtotal, int(bool(i))) 1672 return max(i * graphwidth // maxtotal, int(bool(i)))
1669 1673
1670 for filename, adds, removes, isbinary in stats: 1674 for filename, adds, removes, isbinary, namewidth in sized:
1671 if git and isbinary: 1675 if git and isbinary:
1672 count = 'Bin' 1676 count = 'Bin'
1673 else: 1677 else:
1674 count = adds + removes 1678 count = adds + removes
1675 pluses = '+' * scale(adds) 1679 pluses = '+' * scale(adds)
1676 minuses = '-' * scale(removes) 1680 minuses = '-' * scale(removes)
1677 output.append(' %-*s | %*s %s%s\n' % (maxname, filename, countwidth, 1681 output.append(' %s%s | %*s %s%s\n' %
1678 count, pluses, minuses)) 1682 (filename, ' ' * (maxname - namewidth),
1683 countwidth, count,
1684 pluses, minuses))
1679 1685
1680 if stats: 1686 if stats:
1681 output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n') 1687 output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n')
1682 % (len(stats), totaladds, totalremoves)) 1688 % (len(stats), totaladds, totalremoves))
1683 1689