[PATCH 04 of 11 V2] progress: use 'encoding.trim' to trim output line correctly

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Sat Jul 5 13:00:03 CDT 2014


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1404583001 -32400
#      Sun Jul 06 02:56:41 2014 +0900
# Node ID dd109b0725790b2759ea2d6721a66030268693cd
# Parent  94f4e5d96bf8520766e456a140b586c638bb902e
progress: use 'encoding.trim' to trim output line correctly

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

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

"rm -f loop.pyc" before changing "loop.py" in "test-progress.t"
ensures that re-compilation of "loop.py", even if "loop.py" and
"loop.pyc" have same timestamp in seconds.

diff --git a/hgext/progress.py b/hgext/progress.py
--- a/hgext/progress.py
+++ b/hgext/progress.py
@@ -41,6 +41,8 @@
 from mercurial.i18n import _
 testedwith = 'internal'
 
+from mercurial import encoding
+
 def spacejoin(*args):
     return ' '.join(s for s in args if s)
 
@@ -180,7 +182,7 @@
             out = spacejoin(head, prog, tail)
         else:
             out = spacejoin(head, tail)
-        sys.stderr.write('\r' + out[:termwidth])
+        sys.stderr.write('\r' + encoding.trim(out, termwidth))
         self.lasttopic = topic
         sys.stderr.flush()
 
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('loop', i, 'loop.%d' % i, 'loopnum', total)
+  >         ui.progress(topiclabel, i, 'loop.%d' % i, 'loopnum', total)
   >         if opts.get('parallel'):
   >             ui.progress('other', i, 'other.%d' % i, 'othernum', total)
   >         if nested:
@@ -45,7 +45,9 @@
   >                   'nested', j, 'nested.%d' % j, 'nestnum', nested_steps)
   >             ui.progress(
   >               'nested', None, 'nested.done', 'nestnum', nested_steps)
-  >     ui.progress('loop', None, 'loop.done', 'loopnum', total)
+  >     ui.progress(topiclabel, None, 'loop.done', 'loopnum', total)
+  > 
+  > topiclabel = 'loop'
   > 
   > EOF
 
@@ -238,3 +240,38 @@
   loop [ <=>                                              ] 2\r (no-eol) (esc)
   loop [  <=>                                             ] 3\r (no-eol) (esc)
                                                               \r (no-eol) (esc)
+
+test line trimming by '[progress] width', when progress topic contains
+multi-byte characters, of which length of byte sequence and columns in
+display are different from each other.
+
+  $ cp $HGRCPATH.orig $HGRCPATH
+  $ cat >> $HGRCPATH <<EOF
+  > [extensions]
+  > progress=
+  > loop=`pwd`/loop.py
+  > [progress]
+  > assume-tty = 1
+  > delay = 0
+  > refresh = 0
+  > EOF
+
+  $ rm -f loop.pyc
+  $ cat >> loop.py <<EOF
+  > # use non-ascii characters as topic label of progress
+  > # 2 x 4 = 8 columns, but 3 x 4 = 12 bytes
+  > topiclabel = u'\u3042\u3044\u3046\u3048'.encode('utf-8')
+  > EOF
+
+  $ cat >> $HGRCPATH <<EOF
+  > [progress]
+  > format = topic number
+  > width= 12
+  > EOF
+
+  $ hg --encoding utf-8 -y loop --total 3 3
+  \r (no-eol) (esc)
+  \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88 0/3\r (no-eol) (esc)
+  \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88 1/3\r (no-eol) (esc)
+  \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88 2/3\r (no-eol) (esc)
+              \r (no-eol) (esc)


More information about the Mercurial-devel mailing list