Patchwork [05,of,11,V2] progress: use 'encoding.colwidth' to get column width of output line correctly

login
register
mail settings
Submitter Katsunori FUJIWARA
Date July 5, 2014, 6 p.m.
Message ID <779dd32974bd62564798.1404583204@feefifofum>
Download mbox | patch
Permalink /patch/5111/
State Accepted
Commit e382cf9ec30baaaf841a55d78380b1293b476795
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 779dd32974bd625647980c0c8049c32add7ba29b
# Parent  dd109b0725790b2759ea2d6721a66030268693cd
progress: use 'encoding.colwidth' to get column width of output line 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 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
@@ -159,9 +159,9 @@ 
         if needprogress:
             used = 0
             if head:
-                used += len(head) + 1
+                used += encoding.colwidth(head) + 1
             if tail:
-                used += len(tail) + 1
+                used += encoding.colwidth(tail) + 1
             progwidth = termwidth - used - 3
             if total and pos <= total:
                 amt = pos * progwidth // total
diff --git a/tests/test-progress.t b/tests/test-progress.t
--- a/tests/test-progress.t
+++ b/tests/test-progress.t
@@ -275,3 +275,21 @@ 
   \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)
+
+test calculation of bar width, when progress topic contains multi-byte
+characters, of which length of byte sequence and columns in display
+are different from each other.
+
+  $ cat >> $HGRCPATH <<EOF
+  > [progress]
+  > format = topic bar
+  > width= 21
+  > # progwidth should be 9 (= 21 - (8+1) - 3)
+  > 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 [         ]\r (no-eol) (esc)
+  \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)