Patchwork [08,of,11,V2] progress: use 'encoding.colwidth' to get column width of items correctly

login
register
mail settings
Submitter Katsunori FUJIWARA
Date July 5, 2014, 6 p.m.
Message ID <880eee6596c6d8ecb320.1404583207@feefifofum>
Download mbox | patch
Permalink /patch/5112/
State Accepted
Commit f9c91c638378f31eb73ee34a0c707bb42bfaed5a
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 880eee6596c6d8ecb320ae0a33a8eae94a076dfa
# Parent  b5808bf694d695668a6f666fdbb905108ba4d2ba
progress: use 'encoding.colwidth' to get column width of items correctly

Before this patch, 'progress' extension applies 'len' on byte sequence
to get column width of it, but it causes incorrect result, when length
of byte sequence and columns in display are different from each other
in multi-byte characters.

This patch uses 'encoding.colwidth' to get column width of 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
@@ -142,7 +142,7 @@ 
                     add = encoding.trim(item, wid, leftside=True)
                 else:
                     add = encoding.trim(item, wid)
-                add += (wid - len(add)) * ' '
+                add += (wid - encoding.colwidth(add)) * ' '
             elif indicator == 'bar':
                 add = ''
                 needprogress = True
diff --git a/tests/test-progress.t b/tests/test-progress.t
--- a/tests/test-progress.t
+++ b/tests/test-progress.t
@@ -304,6 +304,7 @@ 
   $ cat >> loop.py <<EOF
   > # use non-ascii characters as loop items of progress
   > loopitems = [
+  >     u'\u3042\u3044'.encode('utf-8'), # 2 x 2 = 4 columns
   >     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
   > ]
@@ -317,8 +318,9 @@ 
   > format = item+6
   > EOF
 
-  $ hg --encoding utf-8 -y loop --total 2 2
-  \r (no-eol) (esc)
+  $ hg --encoding utf-8 -y loop --total 3 3
+  \r (no-eol) (esc)
+  \xe3\x81\x82\xe3\x81\x84  \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)
@@ -329,8 +331,9 @@ 
   > format = item-6
   > EOF
 
-  $ hg --encoding utf-8 -y loop --total 2 2
-  \r (no-eol) (esc)
+  $ hg --encoding utf-8 -y loop --total 3 3
+  \r (no-eol) (esc)
+  \xe3\x81\x82\xe3\x81\x84  \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)