changeset 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 ce95d8b87d22
children 0e789549271d
files mercurial/commands.py mercurial/patch.py tests/test-encoding-align tests/test-encoding-align.out
diffstat 4 files changed, 185 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Fri Jul 16 16:33:55 2010 +0900
+++ b/mercurial/commands.py	Sun Jul 18 01:06:50 2010 +0900
@@ -152,8 +152,9 @@
         for f in funcmap:
             l = [f(n) for n, dummy in lines]
             if l:
-                ml = max(map(len, l))
-                pieces.append(["%*s" % (ml, x) for x in l])
+                sized = [(x, encoding.colwidth(x)) for x in l]
+                ml = max([w for x, w in sized])
+                pieces.append(["%s%s" % (' ' * (ml - w), x) for x, w in sized])
 
         if pieces:
             for p, l in zip(zip(*pieces), lines):
@@ -2053,9 +2054,9 @@
     if multioccur:
         msg = _("\n[+] marked option can be specified multiple times")
         if ui.verbose and name != 'shortlist':
-            opt_output.append((msg, ()))
+            opt_output.append((msg, None))
         else:
-            opt_output.insert(-1, (msg, ()))
+            opt_output.insert(-1, (msg, None))
 
     if not name:
         ui.write(_("\nadditional help topics:\n\n"))
@@ -2067,16 +2068,20 @@
             ui.write(" %-*s  %s\n" % (topics_len, t, desc))
 
     if opt_output:
-        opts_len = max([len(line[0]) for line in opt_output if line[1]] or [0])
-        for first, second in opt_output:
-            if second:
-                initindent = ' %-*s  ' % (opts_len, first)
-                hangindent = ' ' * (opts_len + 3)
-                ui.write('%s\n' % (util.wrap(second,
+        colwidth = encoding.colwidth
+        # normalize: (opt or message, desc or None, width of opt)
+        entries = [desc and (opt, desc, colwidth(opt)) or (opt, None, 0)
+                   for opt, desc in opt_output]
+        hanging = max([e[2] for e in entries])
+        for opt, desc, width in entries:
+            if desc:
+                initindent = ' %s%s  ' % (opt, ' ' * (hanging - width))
+                hangindent = ' ' * (hanging + 3)
+                ui.write('%s\n' % (util.wrap(desc,
                                              initindent=initindent,
                                              hangindent=hangindent)))
             else:
-                ui.write("%s\n" % first)
+                ui.write("%s\n" % opt)
 
 def identify(ui, repo, source=None,
              rev=None, num=None, id=None, branch=None, tags=None):
--- a/mercurial/patch.py	Fri Jul 16 16:33:55 2010 +0900
+++ b/mercurial/patch.py	Sun Jul 18 01:06:50 2010 +0900
@@ -11,7 +11,7 @@
 
 from i18n import _
 from node import hex, nullid, short
-import base85, cmdutil, mdiff, util, diffhelpers, copies
+import base85, cmdutil, mdiff, util, diffhelpers, copies, encoding
 
 gitre = re.compile('diff --git a/(.*) b/(.*)')
 
@@ -1644,10 +1644,14 @@
     maxtotal, maxname = 0, 0
     totaladds, totalremoves = 0, 0
     hasbinary = False
-    for filename, adds, removes, isbinary in stats:
+
+    sized = [(filename, adds, removes, isbinary, encoding.colwidth(filename))
+             for filename, adds, removes, isbinary in stats]
+
+    for filename, adds, removes, isbinary, namewidth in sized:
         totaladds += adds
         totalremoves += removes
-        maxname = max(maxname, len(filename))
+        maxname = max(maxname, namewidth)
         maxtotal = max(maxtotal, adds + removes)
         if isbinary:
             hasbinary = True
@@ -1667,15 +1671,17 @@
         # if there were at least some changes.
         return max(i * graphwidth // maxtotal, int(bool(i)))
 
-    for filename, adds, removes, isbinary in stats:
+    for filename, adds, removes, isbinary, namewidth in sized:
         if git and isbinary:
             count = 'Bin'
         else:
             count = adds + removes
         pluses = '+' * scale(adds)
         minuses = '-' * scale(removes)
-        output.append(' %-*s |  %*s %s%s\n' % (maxname, filename, countwidth,
-                                               count, pluses, minuses))
+        output.append(' %s%s |  %*s %s%s\n' %
+                      (filename, ' ' * (maxname - namewidth),
+                       countwidth, count,
+                       pluses, minuses))
 
     if stats:
         output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n')
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-encoding-align	Sun Jul 18 01:06:50 2010 +0900
@@ -0,0 +1,123 @@
+#!/bin/sh
+
+########################################
+
+HGENCODING=utf-8
+export HGENCODING
+
+hg init t
+cd t
+
+python << EOF
+# (byte, width) = (6, 4)
+s = "\xe7\x9f\xad\xe5\x90\x8d"
+# (byte, width) = (7, 7): odd width is good for alignment test
+m = "MIDDLE_"
+# (byte, width) = (18, 12)
+l = "\xe9\x95\xb7\xe3\x81\x84\xe9\x95\xb7\xe3\x81\x84\xe5\x90\x8d\xe5\x89\x8d"
+
+f = file('s', 'w'); f.write(s); f.close()
+f = file('m', 'w'); f.write(m); f.close()
+f = file('l', 'w'); f.write(l); f.close()
+
+# instant extension to show list of options
+f = file('showoptlist.py', 'w'); f.write("""# encoding: utf-8
+def showoptlist(ui, repo, *pats, **opts):
+    '''dummy command to show option descriptions'''
+    return 0
+
+cmdtable = {
+    'showoptlist':
+        (showoptlist,
+         [('s', 'opt1', '', 'short width',  '""" + s + """'),
+          ('m', 'opt2', '', 'middle width', '""" + m + """'),
+          ('l', 'opt3', '', 'long width',   '""" + l + """')
+         ],
+         ""
+        )
+}
+""")
+f.close()
+EOF
+
+S=`cat s`
+M=`cat m`
+L=`cat l`
+
+########################################
+#### alignment of:
+####     - option descriptions in help
+
+cat <<EOF > .hg/hgrc
+[extensions]
+ja_ext = `pwd`/showoptlist.py
+EOF
+echo '% check alignment of option descriptions in help'
+hg help showoptlist
+
+########################################
+#### alignment of:
+####     - user names in annotate
+####     - file names in diffstat
+
+#### add files
+
+touch $S
+hg add $S
+touch $M
+hg add $M
+touch $L
+hg add $L
+
+#### commit(1)
+
+echo 'first line(1)' >> $S
+echo 'first line(2)' >> $M
+echo 'first line(3)' >> $L
+hg commit -m 'first commit' -u $S -d "1000000 0"
+
+#### commit(2)
+
+echo 'second line(1)' >> $S
+echo 'second line(2)' >> $M
+echo 'second line(3)' >> $L
+hg commit -m 'second commit' -u $M -d "1000000 0"
+
+#### commit(3)
+
+echo 'third line(1)' >> $S
+echo 'third line(2)' >> $M
+echo 'third line(3)' >> $L
+hg commit -m 'third commit' -u $L -d "1000000 0"
+
+#### check
+
+echo '% check alignment of user names in annotate'
+hg annotate -u $M
+echo '% check alignment of filenames in diffstat'
+hg diff -c tip --stat
+
+########################################
+#### alignment of:
+####     - branch names in list
+####     - tag names in list
+
+#### add branches/tags
+
+hg branch $S
+hg tag -d "1000000 0" $S
+hg branch $M
+hg tag -d "1000000 0" $M
+hg branch $L
+hg tag -d "1000000 0" $L
+
+#### check
+
+echo '% check alignment of branches'
+hg tags
+echo '% check alignment of tags'
+hg tags
+
+########################################
+
+exit 0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-encoding-align.out	Sun Jul 18 01:06:50 2010 +0900
@@ -0,0 +1,34 @@
+% check alignment of option descriptions in help
+hg showoptlist 
+
+dummy command to show option descriptions
+
+options:
+
+ -s --opt1 短名          short width
+ -m --opt2 MIDDLE_       middle width
+ -l --opt3 長い長い名前  long width
+
+use "hg -v help showoptlist" to show global options
+% check alignment of user names in annotate
+        短名: first line(2)
+     MIDDLE_: second line(2)
+長い長い名前: third line(2)
+% check alignment of filenames in diffstat
+ MIDDLE_      |  1 +
+ 短名         |  1 +
+ 長い長い名前 |  1 +
+ 3 files changed, 3 insertions(+), 0 deletions(-)
+marked working directory as branch 短名
+marked working directory as branch MIDDLE_
+marked working directory as branch 長い長い名前
+% check alignment of branches
+tip                                5:afc60d8eed19
+長い長い名前                       4:19fe74d09ba0
+MIDDLE_                            3:8a20997d2281
+短名                               2:0cc06ffa3461
+% check alignment of tags
+tip                                5:afc60d8eed19
+長い長い名前                       4:19fe74d09ba0
+MIDDLE_                            3:8a20997d2281
+短名                               2:0cc06ffa3461