Patchwork [2,of,2] progress: use '%*d' to pad progress value

login
register
mail settings
Submitter Yuya Nishihara
Date Feb. 14, 2018, 12:56 p.m.
Message ID <d05d2aa5b8682da65928.1518613013@mimosa>
Download mbox | patch
Permalink /patch/27909/
State Accepted
Headers show

Comments

Yuya Nishihara - Feb. 14, 2018, 12:56 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1518611775 -32400
#      Wed Feb 14 21:36:15 2018 +0900
# Node ID d05d2aa5b8682da65928f0e2fe4a8ece0ed42490
# Parent  bc9daf3da6a6e53331997ea2145a85fd85417e77
progress: use '%*d' to pad progress value

Follows up 7f5108e58083. The problem of '% Nd' is that ' ' means we want
{' ' or '-'} as a sign character. We should instead write '%Nd' to pad up
to N characters with space, and N can be '*'.
Augie Fackler - Feb. 15, 2018, 1:21 a.m.
On Wed, Feb 14, 2018 at 09:56:53PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1518611775 -32400
> #      Wed Feb 14 21:36:15 2018 +0900
> # Node ID d05d2aa5b8682da65928f0e2fe4a8ece0ed42490
> # Parent  bc9daf3da6a6e53331997ea2145a85fd85417e77
> progress: use '%*d' to pad progress value

queued, thanks for cleaning up the mess I made

(I feel like every few years I learn some new obscure corner of
%-formatting. Sigh.)

Patch

diff --git a/mercurial/progress.py b/mercurial/progress.py
--- a/mercurial/progress.py
+++ b/mercurial/progress.py
@@ -119,12 +119,7 @@  class progbar(object):
                 add = topic
             elif indicator == 'number':
                 if total:
-                    padamount = '%d' % len(str(total))
-                    # '% 1d' % 1 adds an extra space compared to '% 1s' % 1.
-                    # To avoid this change in output, we convert to a string
-                    # first, then do the padding.
-                    spos = '%d' % pos
-                    add = ('% '+ padamount + 's/%d') % (spos, total)
+                    add = b'%*d/%d' % (len(str(total)), pos, total)
                 else:
                     add = str(pos)
             elif indicator.startswith('item') and item: