[diffoscope] 06/06: presenters: html: add a size-hint to the diff headers and lazy-load buttons

Ximin Luo infinity0 at debian.org
Thu Jul 6 10:51:09 CEST 2017


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch WIP/humungous-diffs
in repository diffoscope.

commit bf0a61f0ca60dac0489a747b646a9055feada669
Author: Ximin Luo <infinity0 at debian.org>
Date:   Thu Jul 6 10:34:35 2017 +0200

    presenters: html: add a size-hint to the diff headers and lazy-load buttons
---
 diffoscope/presenters/html/html.py      | 28 +++++++++++++++++-----------
 diffoscope/presenters/html/templates.py |  7 ++++++-
 diffoscope/presenters/utils.py          | 19 +++++++++++++++++++
 3 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/diffoscope/presenters/html/html.py b/diffoscope/presenters/html/html.py
index d92c49c..dd3ace8 100644
--- a/diffoscope/presenters/html/html.py
+++ b/diffoscope/presenters/html/html.py
@@ -49,7 +49,7 @@ from diffoscope.config import Config
 from diffoscope.diff import SideBySideDiff, DIFFON, DIFFOFF
 
 from ..icon import FAVICON_BASE64
-from ..utils import PrintLimitReached, DiffBlockLimitReached, \
+from ..utils import sizeof_fmt, PrintLimitReached, DiffBlockLimitReached, \
     create_limited_print_func, Presenter, make_printer, PartialString
 
 from . import templates
@@ -166,18 +166,20 @@ def output_node_frame(difference, path, indentstr, indentnum, body):
     dctrl_class, dctrl = ("diffcontrol", u'⊟') if difference.has_visible_children() else ("diffcontrol-nochildren", u'⊡')
     if difference.source1 == difference.source2:
         header = u"""{0[1]}<div class="{1}">{2}</div>
-{0[1]}<div><span class="source">{4}</span>
-{0[2]}<a class="anchor" href="#{3}" name="{3}">\xb6</a>
+{0[1]}<div><span class="diffsize">{3}</span></div>
+{0[1]}<div><span class="source">{5}</span>
+{0[2]}<a class="anchor" href="#{4}" name="{4}">\xb6</a>
 {0[1]}</div>
-""".format(indent, dctrl_class, dctrl, anchor,
+""".format(indent, dctrl_class, dctrl, sizeof_fmt(difference.size()), anchor,
         html.escape(difference.source1))
     else:
         header = u"""{0[1]}<div class="{1} diffcontrol-double">{2}</div>
-{0[1]}<div><span class="source">{4}</span> vs.</div>
-{0[1]}<div><span class="source">{5}</span>
-{0[2]}<a class="anchor" href="#{3}" name="{3}">\xb6</a>
+{0[1]}<div><span class="diffsize">{3}</span></div>
+{0[1]}<div><span class="source">{5}</span> vs.</div>
+{0[1]}<div><span class="source">{6}</span>
+{0[2]}<a class="anchor" href="#{4}" name="{4}">\xb6</a>
 {0[1]}</div>
-""".format(indent, dctrl_class, dctrl, anchor,
+""".format(indent, dctrl_class, dctrl, sizeof_fmt(difference.size()), anchor,
         html.escape(difference.source1),
         html.escape(difference.source2))
 
@@ -544,9 +546,13 @@ class HTMLPresenter(Presenter):
         del printers[node]
         del continuations[node]
 
-    def output_node_placeholder(self, anchor, lazy_load):
+    def output_node_placeholder(self, pagename, lazy_load, size=0):
         if lazy_load:
-            return templates.DIFFNODE_LAZY_LOAD % anchor
+            return templates.DIFFNODE_LAZY_LOAD % {
+                "pagename": pagename,
+                "pagesize": sizeof_fmt(Config().max_page_size_child),
+                "size": sizeof_fmt(size),
+            }
         else:
             return templates.DIFFNODE_LIMIT
 
@@ -593,7 +599,7 @@ class HTMLPresenter(Presenter):
             else:
                 # over limit (or root), new subpage or continue/break
                 if ancestor:
-                    placeholder = self.output_node_placeholder(pagename, make_new_subpage)
+                    placeholder = self.output_node_placeholder(pagename, make_new_subpage, node.size())
                     outputs[ancestor] = outputs[ancestor].pformat({node: placeholder})
                     self.maybe_print(ancestor, printers, outputs, continuations)
                     footer = output_footer()
diff --git a/diffoscope/presenters/html/templates.py b/diffoscope/presenters/html/templates.py
index 8ee04de..bc806ee 100644
--- a/diffoscope/presenters/html/templates.py
+++ b/diffoscope/presenters/html/templates.py
@@ -149,6 +149,9 @@ STYLES = u"""body.diffoscope {
 .diffoscope .coldiff {
   width: 99%;
 }
+.diffoscope .diffsize {
+  float: right;
+}
 """
 
 SCRIPTS = u"""<script src="%(jquery_url)s"></script>
@@ -208,7 +211,9 @@ $(function() {
 </script>
 """
 
-DIFFNODE_LAZY_LOAD = u"""<div class="ondemand-details">... <a href="%s.html">load details</a> ...</div>
+DIFFNODE_LAZY_LOAD = u"""<div class="ondemand-details" title="the size refers to the raw diff and includes all children;
+only the top %(pagesize)s of the HTML are loaded at a time">... <a
+href="%(pagename)s.html">load details (total %(size)s)</a> ...</div>
 """
 
 DIFFNODE_LIMIT = u"""<div class="error">Max report size reached</div>
diff --git a/diffoscope/presenters/utils.py b/diffoscope/presenters/utils.py
index 6cc4edc..bf505c5 100644
--- a/diffoscope/presenters/utils.py
+++ b/diffoscope/presenters/utils.py
@@ -25,6 +25,25 @@ import string
 import _string
 
 
+def round_sigfig(num, s):
+    # https://stackoverflow.com/questions/3410976/how-to-round-a-number-to-significant-figures-in-python
+    # This was too painful :/
+    x = float(('%%.%sg' % s) % num)
+    return x if abs(x) < (10**(s-1)) else int(x)
+
+
+def sizeof_fmt(num, suffix='B', sigfig=3):
+    # https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
+    # A more powerful version is python3-hurry.filesize but that's an extra dependency
+    for unit in ['','K','M','G','T','P','E','Z']:
+        if abs(num) < 1024.0:
+            break
+        num /= 1024.0
+    else:
+        unit = 'Y'
+    return "%s %s%s" % (round_sigfig(num, sigfig), unit, suffix)
+
+
 class Presenter(object):
     supports_visual_diffs = False
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list