[PATCH 1 of 4] diffstat: scale adds/removes to exact graph width

Brodie Rao dackze at gmail.com
Thu Jul 30 10:06:24 CDT 2009


# HG changeset patch
# User Brodie Rao <me+hg at dackz.net>
# Date 1248966170 14400
# Node ID 4076e49a0bf9555116bf4b6e5bb1fe0d420d4814
# Parent  25255ce87bcfb753df078b2f8cabc6bcb5cb96ce
diffstat: scale adds/removes to exact graph width

The previous method of scaling had a tendency to go past the desired width.

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -9,7 +9,7 @@
 from i18n import _
 from node import hex, nullid, short
 import base85, cmdutil, mdiff, util, diffhelpers, copies
-import cStringIO, email.Parser, os, re, math
+import cStringIO, email.Parser, os, re
 import sys, tempfile, zlib
 
 gitre = re.compile('diff --git a/(.*) b/(.*)')
@@ -1418,18 +1418,21 @@ def diffstat(lines, width=80):
         maxtotal = max(maxtotal, adds+removes)
 
     countwidth = len(str(maxtotal))
-    graphwidth = width - countwidth - maxname
+    graphwidth = width - countwidth - maxname - 6
     if graphwidth < 10:
         graphwidth = 10
 
-    factor = max(int(math.ceil(float(maxtotal) / graphwidth)), 1)
+    def scale(i):
+        if maxtotal < 2:
+            return i
+        return ((i - 1) * (graphwidth - 1) + maxtotal - 1) / (maxtotal - 1)
 
     for filename, adds, removes in stats:
         # If diffstat runs out of room it doesn't print anything, which
         # isn't very useful, so always print at least one + or - if there
         # were at least some changes
-        pluses = '+' * max(adds // factor, int(bool(adds)))
-        minuses = '-' * max(removes // factor, int(bool(removes)))
+        pluses = '+' * max(scale(adds), int(bool(adds)))
+        minuses = '-' * max(scale(removes), int(bool(removes)))
         output.append(' %-*s |  %*.d %s%s\n' % (maxname, filename, countwidth,
                                                 adds+removes, pluses, minuses))
 


More information about the Mercurial-devel mailing list