mercurial/util.py
changeset 16397 f0f7f3fab315
parent 16383 f5dd179bfa4a
child 16688 cfb6682961b8
child 16768 23a125545c3d
equal deleted inserted replaced
16393:ee163a9cf37c 16397:f0f7f3fab315
  1165             return text
  1165             return text
  1166         return utext.encode(encoding.encoding)
  1166         return utext.encode(encoding.encoding)
  1167     except (UnicodeDecodeError, UnicodeEncodeError):
  1167     except (UnicodeDecodeError, UnicodeEncodeError):
  1168         return _ellipsis(text, maxlength)[0]
  1168         return _ellipsis(text, maxlength)[0]
  1169 
  1169 
       
  1170 _byteunits = (
       
  1171     (100, 1 << 30, _('%.0f GB')),
       
  1172     (10, 1 << 30, _('%.1f GB')),
       
  1173     (1, 1 << 30, _('%.2f GB')),
       
  1174     (100, 1 << 20, _('%.0f MB')),
       
  1175     (10, 1 << 20, _('%.1f MB')),
       
  1176     (1, 1 << 20, _('%.2f MB')),
       
  1177     (100, 1 << 10, _('%.0f KB')),
       
  1178     (10, 1 << 10, _('%.1f KB')),
       
  1179     (1, 1 << 10, _('%.2f KB')),
       
  1180     (1, 1, _('%.0f bytes')),
       
  1181     )
       
  1182 
  1170 def bytecount(nbytes):
  1183 def bytecount(nbytes):
  1171     '''return byte count formatted as readable string, with units'''
  1184     '''return byte count formatted as readable string, with units'''
  1172 
  1185 
  1173     units = (
  1186     for multiplier, divisor, format in _byteunits:
  1174         (100, 1 << 30, _('%.0f GB')),
       
  1175         (10, 1 << 30, _('%.1f GB')),
       
  1176         (1, 1 << 30, _('%.2f GB')),
       
  1177         (100, 1 << 20, _('%.0f MB')),
       
  1178         (10, 1 << 20, _('%.1f MB')),
       
  1179         (1, 1 << 20, _('%.2f MB')),
       
  1180         (100, 1 << 10, _('%.0f KB')),
       
  1181         (10, 1 << 10, _('%.1f KB')),
       
  1182         (1, 1 << 10, _('%.2f KB')),
       
  1183         (1, 1, _('%.0f bytes')),
       
  1184         )
       
  1185 
       
  1186     for multiplier, divisor, format in units:
       
  1187         if nbytes >= divisor * multiplier:
  1187         if nbytes >= divisor * multiplier:
  1188             return format % (nbytes / float(divisor))
  1188             return format % (nbytes / float(divisor))
  1189     return units[-1][2] % nbytes
  1189     return units[-1][2] % nbytes
  1190 
  1190 
  1191 def uirepr(s):
  1191 def uirepr(s):