Patchwork [07,of,11,V2] progress: use 'encoding.trim' to trim items in output line correctly

login
register
mail settings
Submitter Katsunori FUJIWARA
Date July 5, 2014, 6 p.m.
Message ID <b5808bf694d695668a6f.1404583206@feefifofum>
Download mbox | patch
Permalink /patch/5113/
State Accepted
Commit ba7f75e7f4e52b5abe565bb38aad19af04859637
Headers show

Comments

Katsunori FUJIWARA - July 5, 2014, 6 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1404583001 -32400
#      Sun Jul 06 02:56:41 2014 +0900
# Node ID b5808bf694d695668a6f666fdbb905108ba4d2ba
# Parent  280066d24ef9a639b0d93a92f291f853b43bb4da
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.

Patch

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, leftside=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
@@ -33,7 +33,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:
@@ -48,6 +48,8 @@ 
   >     ui.progress(topiclabel, None, 'loop.done', 'loopnum', total)
   > 
   > topiclabel = 'loop'
+  > def getloopitem(i):
+  >     return 'loop.%d' % i
   > 
   > EOF
 
@@ -293,3 +295,42 @@ 
   \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
+  > # use non-ascii characters as loop items of progress
+  > 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 left 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)