about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2017-11-24 18:40:27 +0100
committerLeah Neukirchen <leah@vuxu.org>2017-11-24 18:40:27 +0100
commit5562b59e19bacd21eb3e3ec28f5dff2e99085b4d (patch)
treea283c8cb8bcdca76a5275d81a498221b0694a25d
parent8191e3d50e3459f035e6eea510dd4acdbc709fc2 (diff)
downloadlr-5562b59e19bacd21eb3e3ec28f5dff2e99085b4d.tar.gz
lr-5562b59e19bacd21eb3e3ec28f5dff2e99085b4d.tar.xz
lr-5562b59e19bacd21eb3e3ec28f5dff2e99085b4d.zip
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).
-rw-r--r--NEWS.md1
-rw-r--r--lr.c25
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
@@ -1632,6 +1632,23 @@ color_size_on(off_t s)
 }
 
 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)
 {
 	double d = 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;
 			}