From 32d31cd47b27215dd2fdb8272b0adb0b3b2f3892 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 10 May 2011 17:05:38 +0100 Subject: Add humanify. --- humanify | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ humanify.1 | 44 ++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100755 humanify create mode 100644 humanify.1 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 . + + +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 +.\" +.\" 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 . +.\" +.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. -- cgit v1.2.1 From acd06b0d3ce147cbc5375aba90972df85bde7cbf Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 10 May 2011 17:05:55 +0100 Subject: Add humanify to Makefile. --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 -- cgit v1.2.1 From 8b51375edb1bd40566b47fee2d34dd0aa5a33c37 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 10 May 2011 17:06:41 +0100 Subject: Add debian/changelog entry. --- debian/changelog | 6 ++++++ 1 file changed, 6 insertions(+) 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 Tue, 10 May 2011 17:06:18 +0100 + extrautils (1.13) squeeze; urgency=low * New upstream version. -- cgit v1.2.1