diff options
Diffstat (limited to 'ranger/ext/human_readable.py')
-rw-r--r-- | ranger/ext/human_readable.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/ranger/ext/human_readable.py b/ranger/ext/human_readable.py new file mode 100644 index 00000000..4afa03cb --- /dev/null +++ b/ranger/ext/human_readable.py @@ -0,0 +1,21 @@ +ONE_KB = 1024 +UNITS = 'BKMGTP' +MAX_EXPONENT = len(UNITS) - 1 + +def human_readable(byte): + import math + + if not byte: + return '0 B' + + exponent = int(math.log(byte, 2) / 10) + flt = float(byte) / (1 << (10 * exponent)) + + if exponent > MAX_EXPONENT: + return '>9000' # off scale + + if int(flt) == flt: + return '%.0f %s' % (flt, UNITS[exponent]) + + else: + return '%.2f %s' % (flt, UNITS[exponent]) |