Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 18735:716cad930691
util: generalize bytecount to unitcountfn
This gives us a function we can reuse to count units of other kinds.
author | Bryan O'Sullivan <bryano@fb.com> |
---|---|
date | Thu, 28 Feb 2013 12:51:18 -0800 |
parents | 423eee0b0b14 |
children | af9ddea2cb99 |
comparison
equal
deleted
inserted
replaced
18734:b72697653306 | 18735:716cad930691 |
---|---|
1266 return text | 1266 return text |
1267 return utext.encode(encoding.encoding) | 1267 return utext.encode(encoding.encoding) |
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')), |
1275 (100, 1 << 20, _('%.0f MB')), | 1286 (100, 1 << 20, _('%.0f MB')), |
1276 (10, 1 << 20, _('%.1f MB')), | 1287 (10, 1 << 20, _('%.1f MB')), |
1278 (100, 1 << 10, _('%.0f KB')), | 1289 (100, 1 << 10, _('%.0f KB')), |
1279 (10, 1 << 10, _('%.1f KB')), | 1290 (10, 1 << 10, _('%.1f KB')), |
1280 (1, 1 << 10, _('%.2f KB')), | 1291 (1, 1 << 10, _('%.2f KB')), |
1281 (1, 1, _('%.0f bytes')), | 1292 (1, 1, _('%.0f bytes')), |
1282 ) | 1293 ) |
1283 | |
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 | 1294 |
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('\\\\', '\\') |
1295 | 1298 |