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

Brodie Rao dackze at gmail.com
Mon Aug 10 09:24:09 CDT 2009


# HG changeset patch
# User Brodie Rao <me+hg at dackz.net>
# Date 1249913323 14400
# Node ID ea68b90e36f6794d001f983ef32f19a1a30e4a92
# Parent  648d6a1a1cf25c3471b57e07f52746276a2c81ee
diffstat: scale adds/removes proportionally to graph width

The previous method of scaling had a tendency to include graph lines that
went past the output width when the file with the most changes had a very
large number of changes.

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/(.*)')
@@ -1429,18 +1429,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 <= graphwidth:
+            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