summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-05-10 17:07:17 +0100
committerLars Wirzenius <liw@liw.fi>2011-05-10 17:07:17 +0100
commitfdaec97979044db0d9cf36d5ed5344ca1745fa00 (patch)
tree34f27d8efa04ac7cb9479388ecd1632b7a3167bc
parente6e3ac799af4c4d9c22f86eead23c1dee9c2e528 (diff)
parent8b51375edb1bd40566b47fee2d34dd0aa5a33c37 (diff)
downloadextrautils-fdaec97979044db0d9cf36d5ed5344ca1745fa00.tar.gz
Merge changes to add humanify(1).
-rw-r--r--Makefile3
-rw-r--r--debian/changelog6
-rwxr-xr-xhumanify91
-rw-r--r--humanify.144
4 files changed, 143 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index fb2cbfd..ba10747 100644
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,8 @@ man1dir = $(mandir)/man1
progs = corrupt isascii
scripts = assert do-until errno minimify splitmboxdaily \
setuppy-debian-versions-match viewprof fix-shebang \
- musictomp3 test-flacs unpack-debian-sources mksparse
+ musictomp3 test-flacs unpack-debian-sources mksparse \
+ humanify
CFLAGS = -Wall -O2 --std=gnu99
diff --git a/debian/changelog b/debian/changelog
index 3c78d12..84a1d46 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+extrautils (1.14) squeeze; urgency=low
+
+ * Add humanify.
+
+ -- Lars Wirzenius <liw@liw.fi> Tue, 10 May 2011 17:06:18 +0100
+
extrautils (1.13) squeeze; urgency=low
* New upstream version.
diff --git a/humanify b/humanify
new file mode 100755
index 0000000..b7e69de
--- /dev/null
+++ b/humanify
@@ -0,0 +1,91 @@
+#!/usr/bin/python
+# Copyright 2011 Lars Wirzenius
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import cliapp
+import sys
+
+
+class Humanify(cliapp.Application):
+
+ iso_prefixes = [
+ (1000**8, 'Y'),
+ (1000**7, 'Z'),
+ (1000**6, 'E'),
+ (1000**5, 'P'),
+ (1000**4, 'T'),
+ (1000**3, 'G'),
+ (1000**2, 'M'),
+ (1000, 'k'),
+ (1, ''),
+ ]
+ iec_prefixes = [
+ (1024**8, 'Yi'),
+ (1024**7, 'Zi'),
+ (1024**6, 'Ei'),
+ (1024**5, 'Pi'),
+ (1024**4, 'Ti'),
+ (1024**3, 'Gi'),
+ (1024**2, 'MI'),
+ (1024, 'Ki'),
+ (1, ''),
+ ]
+
+ def add_settings(self):
+ self.settings.add_boolean_setting(['binary', 'iec'],
+ 'use 10-based, not 2-based prefixes '
+ '(ISO vs IEC)')
+ self.settings.add_boolean_setting(['bits'],
+ 'inputs give numbers of bits')
+
+ def process_args(self, args):
+ self.units = self.determine_units()
+
+ if args:
+ for arg in args:
+ self.humanify(arg)
+ else:
+ for line in sys.stdin:
+ self.humanify(float(line.strip()))
+
+ def determine_units(self):
+ if self.settings['binary']:
+ prefixes = self.iec_prefixes
+ else:
+ prefixes = self.iso_prefixes
+ if self.settings['bits']:
+ suffix = 'b'
+ else:
+ suffix = 'B'
+ return [(factor, prefix + suffix) for factor, prefix in prefixes]
+
+ def humanify(self, arg):
+ self.output.write('%s\n' % self._humanify(float(arg), self.units))
+
+ def _humanify(self, value, units):
+ assert units
+ if value >= units[0][0]:
+ return '%.0f %s' % (round(float(value) / units[0][0]),
+ units[0][1])
+ for factor, unit in units:
+ size = float(value) / float(factor)
+ if 1 <= size < 1000:
+ return '%.0f %s' % (round(size), unit)
+ return '%.0f %s' % (round(float(value) / units[-1][0]), units[-1][1])
+
+
+if __name__ == '__main__':
+ Humanify().run()
diff --git a/humanify.1 b/humanify.1
new file mode 100644
index 0000000..dcb80fb
--- /dev/null
+++ b/humanify.1
@@ -0,0 +1,44 @@
+.\" Copyright 2011 Lars Wirzenius <liw@liw.fi>
+.\"
+.\" This program is free software: you can redistribute it and/or modify
+.\" it under the terms of the GNU General Public License as published by
+.\" the Free Software Foundation, either version 3 of the License, or
+.\" (at your option) any later version.
+.\"
+.\" This program is distributed in the hope that it will be useful,
+.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
+.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+.\" GNU General Public License for more details.
+.\"
+.\" You should have received a copy of the GNU General Public License
+.\" along with this program. If not, see <http://www.gnu.org/licenses/>.
+.\"
+.TH HUMANIFY 1
+.SH NAME
+humanify \- format bit- and byte-sizes into human-readable form
+.SH SYNOPSIS
+.B humanify
+.RB [ \-\-binary ]
+.RI [ size ...]
+.SH DESCRIPTION
+.B humanify
+formats in human-readable form data sizes (number of bits or bytes).
+For example,
+it can turn
+.I 1234
+into
+.I "1 kB"
+or
+.IR "1 KiB" .
+Sizes are given on the command line,
+or one-per line in the standard input.
+Sizes are rounded to the nearest integer.
+.SH OPTIONS
+.TP
+.BR \-\-binary
+Use 2-based prefixes (from IEC),
+rather than 10-based ones (from ISO).
+In other words, "kibibyte" instead of "kilobyte".
+.TP
+.BR \-\-bits
+Inputs give number of bits, instead of bytes.