Patchwork [4,of,8,STABLE] progress: use 'encoding.trim' to trim output line correctly

login
register
mail settings
Submitter Katsunori FUJIWARA
Date June 13, 2014, 4:22 p.m.
Message ID <919cf4020927a5f1e8ee.1402676552@feefifofum>
Download mbox | patch
Permalink /patch/4991/
State Superseded
Headers show

Comments

Katsunori FUJIWARA - June 13, 2014, 4:22 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1402675901 -32400
#      Sat Jun 14 01:11:41 2014 +0900
# Branch stable
# Node ID 919cf4020927a5f1e8ee5c65012b09dd86ae00e8
# Parent  d69844e871a87bc56cc4cc7c56306f79429a1c17
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.

Patch

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
@@ -23,7 +23,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:
@@ -35,7 +35,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'
   > 
   > commands.norepo += " loop"
   > 
@@ -237,3 +239,37 @@ 
   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
+  > # 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)