# HG changeset patch # User Bryan O'Sullivan # Date 2013-02-28 20:51:18 # Node ID 716cad9306914703d27cc3468036fdba4412ee90 # Parent b72697653306d3748c869a8ad2b8843cb972cfca util: generalize bytecount to unitcountfn This gives us a function we can reuse to count units of other kinds. diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -1268,7 +1268,18 @@ def ellipsis(text, maxlength=400): except (UnicodeDecodeError, UnicodeEncodeError): return _ellipsis(text, maxlength)[0] -_byteunits = ( +def unitcountfn(*unittable): + '''return a function that renders a readable count of some quantity''' + + def go(count): + for multiplier, divisor, format in unittable: + if count >= divisor * multiplier: + return format % (count / float(divisor)) + return unittable[-1][2] % count + + return go + +bytecount = unitcountfn( (100, 1 << 30, _('%.0f GB')), (10, 1 << 30, _('%.1f GB')), (1, 1 << 30, _('%.2f GB')), @@ -1281,14 +1292,6 @@ def ellipsis(text, maxlength=400): (1, 1, _('%.0f bytes')), ) -def bytecount(nbytes): - '''return byte count formatted as readable string, with units''' - - for multiplier, divisor, format in _byteunits: - if nbytes >= divisor * multiplier: - return format % (nbytes / float(divisor)) - return _byteunits[-1][2] % nbytes - def uirepr(s): # Avoid double backslash in Windows path repr() return repr(s).replace('\\\\', '\\')