From 0e9e6d511ab6795f1fdcc01b7c5aa5edb66b5e7a Mon Sep 17 00:00:00 2001 From: Yorhel Date: Tue, 23 Jul 2013 10:33:24 +0200 Subject: [PATCH] Add --si flag for base 10 prefixes This is a slightly modified patch contributed at http://dev.yorhel.nl/ncdu/bug/35 --- doc/ncdu.pod | 6 ++++++ src/main.c | 4 ++++ src/util.c | 29 +++++++++++++++++++++-------- src/util.h | 3 +++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/doc/ncdu.pod b/doc/ncdu.pod index 89ecf82..d4b5bd8 100644 --- a/doc/ncdu.pod +++ b/doc/ncdu.pod @@ -97,6 +97,12 @@ option has no effect when C<-o> is used, because there will not be a browser interface in that case. It has no effect when C<-f> is used, either, because the deletion feature is disabled in that case anyway. +=item --si + +List sizes using base 10 prefixes, that is, powers of 1000 (KB, MB, etc), as +defined in the International System of Units (SI), instead of the usual base 2 +prefixes, that is, powers of 1024 (KiB, MiB, etc). + =back =head2 Scan Options diff --git a/src/main.c b/src/main.c index c795114..7d52b09 100644 --- a/src/main.c +++ b/src/main.c @@ -127,10 +127,12 @@ static void argv_parse(int argc, char **argv) { { 1, 1, "--exclude" }, { 'X', 1, "-X,--exclude-from" }, { 'C', 0, "--exclude-caches" }, + { 's', 0, "--si" }, {0,0,NULL} }; dir_ui = -1; + si = 0; yopt_init(&yopt, argc, argv, opts); while((v = yopt_next(&yopt, &val)) != -1) { @@ -146,6 +148,7 @@ static void argv_parse(int argc, char **argv) { printf(" -o FILE Export scanned directory to FILE\n"); printf(" -f FILE Import scanned directory from FILE\n"); printf(" -0,-1,-2 UI to use when scanning (0=none,2=full ncurses)\n"); + printf(" --si Use base 10 (SI) prefixes instead of base 2\n"); printf(" --exclude PATTERN Exclude files that match PATTERN\n"); printf(" -X, --exclude-from FILE Exclude files that match any pattern in FILE\n"); printf(" --exclude-caches Exclude directories containing CACHEDIR.TAG\n"); @@ -156,6 +159,7 @@ static void argv_parse(int argc, char **argv) { exit(0); case 'x': dir_scan_smfs = 1; break; case 'r': read_only = 1; break; + case 's': si = 1; break; case 'o': export = val; break; case 'f': import = val; break; case '0': dir_ui = 0; break; diff --git a/src/util.c b/src/util.c index 137d16d..6be3e70 100644 --- a/src/util.c +++ b/src/util.c @@ -32,6 +32,7 @@ int winrows, wincols; int subwinr, subwinc; +int si; char thou_sep; @@ -60,14 +61,26 @@ char *formatsize(int64_t from) { static char dat[9]; /* "xxx.xMiB" */ float r = from; char c = ' '; - if(r < 1000.0f) { } - else if(r < 1023e3f) { c = 'K'; r/=1024.0f; } - else if(r < 1023e6f) { c = 'M'; r/=1048576.0f; } - else if(r < 1023e9f) { c = 'G'; r/=1073741824.0f; } - else if(r < 1023e12f){ c = 'T'; r/=1099511627776.0f; } - else if(r < 1023e15f){ c = 'P'; r/=1125899906842624.0f; } - else { c = 'E'; r/=1152921504606846976.0f; } - sprintf(dat, "%5.1f%c%cB", r, c, c == ' ' ? ' ' : 'i'); + if (si) { + if(r < 1000.0f) { } + else if(r < 1e6f) { c = 'K'; r/=1e3f; } + else if(r < 1e9f) { c = 'M'; r/=1e6f; } + else if(r < 1e12f){ c = 'G'; r/=1e9f; } + else if(r < 1e15f){ c = 'T'; r/=1e12f; } + else if(r < 1e18f){ c = 'P'; r/=1e15f; } + else { c = 'E'; r/=1e18f; } + sprintf(dat, "%5.1f%cB", r, c); + } + else { + if(r < 1000.0f) { } + else if(r < 1023e3f) { c = 'K'; r/=1024.0f; } + else if(r < 1023e6f) { c = 'M'; r/=1048576.0f; } + else if(r < 1023e9f) { c = 'G'; r/=1073741824.0f; } + else if(r < 1023e12f){ c = 'T'; r/=1099511627776.0f; } + else if(r < 1023e15f){ c = 'P'; r/=1125899906842624.0f; } + else { c = 'E'; r/=1152921504606846976.0f; } + sprintf(dat, "%5.1f%c%cB", r, c, c == ' ' ? ' ' : 'i'); + } return dat; } diff --git a/src/util.h b/src/util.h index d3b417f..bbf0e53 100644 --- a/src/util.h +++ b/src/util.h @@ -35,6 +35,9 @@ extern int winrows, wincols; /* used by the nc* functions and macros */ extern int subwinr, subwinc; +/* used by formatsize to choose between base 2 or 10 prefixes */ +extern int si; + /* Instead of using several ncurses windows, we only draw to stdscr. * the functions nccreate, ncprint and the macros ncaddstr and ncaddch