[PATCH 7 of 8 STABLE] progress: use 'encoding.trim' to trim items in output line correctly

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Fri Jun 13 11:22:35 CDT 2014


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1402675901 -32400
#      Sat Jun 14 01:11:41 2014 +0900
# Branch stable
# Node ID 8a3053db8c90be14bcd81624479633098233550f
# Parent  0d4c198fa1eac9824eb691c7e89d59ef08b36720
progress: use 'encoding.trim' to trim items in output line correctly

Before this patch, 'progress' extension trims items in output line by
directly slicing byte sequence, but it may split at intermediate
multi-byte sequence.

This patch uses 'encoding.trim' to trim items in output line
correctly, even if it contains multi-byte characters.

diff --git a/hgext/progress.py b/hgext/progress.py
--- a/hgext/progress.py
+++ b/hgext/progress.py
@@ -139,9 +139,9 @@
                 else:
                     wid = 20
                 if slice == 'end':
-                    add = item[-wid:]
+                    add = encoding.trim(item, wid, headside=True)
                 else:
-                    add = item[:wid]
+                    add = encoding.trim(item, wid)
                 add += (wid - len(add)) * ' '
             elif indicator == 'bar':
                 add = ''
diff --git a/tests/test-progress.t b/tests/test-progress.t
--- a/tests/test-progress.t
+++ b/tests/test-progress.t
@@ -23,7 +23,7 @@
   >     loops = abs(loops)
   > 
   >     for i in range(loops):
-  >         ui.progress(topiclabel, i, 'loop.%d' % i, 'loopnum', total)
+  >         ui.progress(topiclabel, i, getloopitem(i), 'loopnum', total)
   >         if opts.get('parallel'):
   >             ui.progress('other', i, 'other.%d' % i, 'othernum', total)
   >         if nested:
@@ -38,6 +38,8 @@
   >     ui.progress(topiclabel, None, 'loop.done', 'loopnum', total)
   > 
   > topiclabel = 'loop'
+  > def getloopitem(i):
+  >     return 'loop.%d' % i
   > 
   > commands.norepo += " loop"
   > 
@@ -291,3 +293,41 @@
   \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88 [==>      ]\r (no-eol) (esc)
   \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88 [=====>   ]\r (no-eol) (esc)
                        \r (no-eol) (esc)
+
+test triming progress items, when they contain multi-byte characters,
+of which length of byte sequence and columns in display are different
+from each other.
+
+  $ rm -f loop.pyc
+  $ cat >> loop.py <<EOF
+  > loopitems = [
+  >     u'\u3042\u3044\u3046'.encode('utf-8'), # 2 x 3 = 6 columns
+  >     u'\u3042\u3044\u3046\u3048'.encode('utf-8'), # 2 x 4 = 8 columns
+  > ]
+  > def getloopitem(i):
+  >     return loopitems[i % len(loopitems)]
+  > EOF
+
+  $ cat >> $HGRCPATH <<EOF
+  > [progress]
+  > # trim at tail side
+  > format = item+6
+  > EOF
+
+  $ hg --encoding utf-8 -y loop --total 2 2
+  \r (no-eol) (esc)
+  \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\r (no-eol) (esc)
+  \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\r (no-eol) (esc)
+                       \r (no-eol) (esc)
+
+  $ cat >> $HGRCPATH <<EOF
+  > [progress]
+  > # trim at head side
+  > format = item-6
+  > EOF
+
+  $ hg --encoding utf-8 -y loop --total 2 2
+  \r (no-eol) (esc)
+  \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\r (no-eol) (esc)
+  \xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\r (no-eol) (esc)
+                       \r (no-eol) (esc)


More information about the Mercurial-devel mailing list