-
Notifications
You must be signed in to change notification settings - Fork 20
/
bytes2human
executable file
·45 lines (35 loc) · 1.32 KB
/
bytes2human
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
import fileinput
import sys
import humanbytes
import re
args = sys.argv[1:]
if len(args) <= 0:
pass
getcol1 = re.compile(rb'^ (?P<match> [\d.]+ ) \s? (?P<rest> .*$)', re.VERBOSE)
r = re.compile(rb' (?P<num> [0-9]+ )[.](?P<digits> [0-9]+) (?P<suffix> [\w])', re.VERBOSE)
for line in fileinput.input(mode='rb'):
orig = line
line = line.strip()
m = getcol1.search(line)
if m:
inp = m.expand(rb'\g<match>')
rest = m.expand(rb'\g<rest>')
outp = humanbytes.bytes2human(inp.decode('utf-8'), format='%(value)0.1f%(symbol)s').encode('utf-8')
# if r.search(outp):
# outp = re.sub(r, b'\g<num>.\g<digits>\g<suffix>', outp)
# if there are multiple leading digits, strip off the decimals.
outp = re.sub(rb'([0-9][0-9]+)[.][0-9]+', rb'\1', outp)
# show bytes as plain numbers.
outp = re.sub(rb'([0-9]+)(?:[.][0-9]*)?\s*B', rb'\1', outp)
# add some padding.
outp = outp.rjust(4)
#sys.stdout.write("%s %s\n" % (outp, rest))
#sys.stdout.buffer.write("%b %b\n" % (outp, rest))
sys.stdout.buffer.write(outp)
sys.stdout.buffer.write(b" ")
sys.stdout.buffer.write(rest)
sys.stdout.buffer.write(b"\n")
sys.stdout.buffer.flush()
else:
sys.stdout.buffer.write(orig)