From 5562b59e19bacd21eb3e3ec28f5dff2e99085b4d Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Fri, 24 Nov 2017 18:40:27 +0100 Subject: print file size in groups of three digits when -G is used We use a custom printing function as %'jd is locale-dependent (and not implemented in musl). --- NEWS.md | 1 + lr.c | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8a43628..14f56c7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ * Feature: new option `-P` to quote filenames with `$'...'` syntax. * Feature: invalid UTF-8 filenames are quoted now. +* Feature: colorized file size output now uses groups of three digits. ## 1.2 (2017-11-17) diff --git a/lr.c b/lr.c index 7f446f9..06b70ca 100644 --- a/lr.c +++ b/lr.c @@ -1631,6 +1631,23 @@ color_size_on(off_t s) fg256(c); } +static void +print_comma(int len, intmax_t i) +{ + char buf[64]; + char *s = buf + sizeof buf; + + *--s = 0; + while (1) { + *--s = (i % 10) + '0'; if ((i /= 10) <= 0) break; + *--s = (i % 10) + '0'; if ((i /= 10) <= 0) break; + *--s = (i % 10) + '0'; if ((i /= 10) <= 0) break; + *--s = ','; + } + + printf("%*.*s", len+len/3, len+len/3, s); +} + static void print_human(intmax_t i) { @@ -1956,8 +1973,12 @@ print_format(struct fileinfo *fi) case 's': if (!hflag) { color_size_on(fi->sb.st_size); - printf("%*jd", intlen(maxsize), - (intmax_t)fi->sb.st_size); + if (Gflag) + print_comma(intlen(maxsize), + (intmax_t)fi->sb.st_size); + else + printf("%*jd", intlen(maxsize), + (intmax_t)fi->sb.st_size); fgdefault(); break; } -- cgit 1.4.1