changeset 21859:be4270d27a7e

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.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Sun, 06 Jul 2014 02:56:41 +0900
parents 50fd3a36d166
children e382cf9ec30b
files hgext/progress.py tests/test-progress.t
diffstat 2 files changed, 42 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/progress.py	Sun Jul 06 02:56:41 2014 +0900
+++ b/hgext/progress.py	Sun Jul 06 02:56:41 2014 +0900
@@ -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()
 
--- a/tests/test-progress.t	Sun Jul 06 02:56:41 2014 +0900
+++ b/tests/test-progress.t	Sun Jul 06 02:56:41 2014 +0900
@@ -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)