##// END OF EJS Templates
util: generalize bytecount to unitcountfn...
Bryan O'Sullivan -
r18735:716cad93 default
parent child Browse files
Show More
@@ -1268,7 +1268,18 b' def ellipsis(text, maxlength=400):'
1268 except (UnicodeDecodeError, UnicodeEncodeError):
1268 except (UnicodeDecodeError, UnicodeEncodeError):
1269 return _ellipsis(text, maxlength)[0]
1269 return _ellipsis(text, maxlength)[0]
1270
1270
1271 _byteunits = (
1271 def unitcountfn(*unittable):
1272 '''return a function that renders a readable count of some quantity'''
1273
1274 def go(count):
1275 for multiplier, divisor, format in unittable:
1276 if count >= divisor * multiplier:
1277 return format % (count / float(divisor))
1278 return unittable[-1][2] % count
1279
1280 return go
1281
1282 bytecount = unitcountfn(
1272 (100, 1 << 30, _('%.0f GB')),
1283 (100, 1 << 30, _('%.0f GB')),
1273 (10, 1 << 30, _('%.1f GB')),
1284 (10, 1 << 30, _('%.1f GB')),
1274 (1, 1 << 30, _('%.2f GB')),
1285 (1, 1 << 30, _('%.2f GB')),
@@ -1281,14 +1292,6 b' def ellipsis(text, maxlength=400):'
1281 (1, 1, _('%.0f bytes')),
1292 (1, 1, _('%.0f bytes')),
1282 )
1293 )
1283
1294
1284 def bytecount(nbytes):
1285 '''return byte count formatted as readable string, with units'''
1286
1287 for multiplier, divisor, format in _byteunits:
1288 if nbytes >= divisor * multiplier:
1289 return format % (nbytes / float(divisor))
1290 return _byteunits[-1][2] % nbytes
1291
1292 def uirepr(s):
1295 def uirepr(s):
1293 # Avoid double backslash in Windows path repr()
1296 # Avoid double backslash in Windows path repr()
1294 return repr(s).replace('\\\\', '\\')
1297 return repr(s).replace('\\\\', '\\')
General Comments 0
You need to be logged in to leave comments. Login now