# HG changeset patch # User Bryan O'Sullivan # Date 1362084678 28800 # 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 -r b72697653306 -r 716cad930691 mercurial/util.py --- a/mercurial/util.py Thu Feb 28 21:34:44 2013 +0100 +++ b/mercurial/util.py Thu Feb 28 12:51:18 2013 -0800 @@ -1268,7 +1268,18 @@ 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 @@ (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('\\\\', '\\')