From 46b21450aca8291f789aee3d5166a78e427f6b84 Mon Sep 17 00:00:00 2001
From: Michael Osipov <1983-01-06@gmx.net>
Date: Sat, 30 Jul 2016 22:51:25 +0200
Subject: [PATCH 1/2] size_trunc(): autoscale size with proper prefixes
An input in bytes will properly scale between bytes, kilobytes/kibibytes,
megabytes/mebibytes and gigabytes/gibibytes.
Scaled size in the range (0.05, 10) will be a float with one decimal precision.
The rest is formatted as integer.
---
lib/util.c | 41 ++++++++++++++++++++++++++---------------
src/filemanager/file.c | 2 +-
src/filemanager/panel.c | 6 +++---
3 files changed, 30 insertions(+), 19 deletions(-)
diff --git a/lib/util.c b/lib/util.c
index 304351d..8de5314 100644
a
|
b
|
size_trunc (uintmax_t size, gboolean use_si) |
346 | 346 | { |
347 | 347 | static char x[BUF_TINY]; |
348 | 348 | uintmax_t divisor = 1; |
349 | | const char *xtra = _("B"); |
| 349 | const char *scaled_symbol = _("B"); |
| 350 | double scaled_size; |
350 | 351 | |
351 | | if (size > 999999999UL) |
| 352 | if ((use_si && size >= 1000000000UL) || size >= (1024UL * 1024UL * 1024UL)) |
| 353 | { |
| 354 | divisor = use_si ? (1000 * 1000 * 1000) : (1024 * 1024 * 1024); |
| 355 | scaled_symbol = use_si ? _("GB") : _("GiB"); |
| 356 | } |
| 357 | else if ((use_si && size >= 1000000UL) || size >= (1024UL * 1024UL)) |
| 358 | { |
| 359 | divisor = use_si ? (1000 * 1000) : (1024 * 1024); |
| 360 | scaled_symbol = use_si ? _("MB") : _("MiB"); |
| 361 | } |
| 362 | else if ((use_si && size >= 1000UL) || size >= 1024UL) |
352 | 363 | { |
353 | 364 | divisor = use_si ? 1000 : 1024; |
354 | | xtra = use_si ? _("kB") : _("KiB"); |
| 365 | scaled_symbol = use_si ? _("kB") : _("KiB"); |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | g_snprintf (x, sizeof (x), "%" PRIuMAX " %s", size, scaled_symbol); |
| 370 | return x; |
| 371 | } |
355 | 372 | |
356 | | if (size / divisor > 999999999UL) |
357 | | { |
358 | | divisor = use_si ? (1000 * 1000) : (1024 * 1024); |
359 | | xtra = use_si ? _("MB") : _("MiB"); |
| 373 | scaled_size = 1.0 * size / divisor; |
| 374 | |
| 375 | if (scaled_size < 0.05 || scaled_size >= 10.0) |
| 376 | g_snprintf (x, sizeof (x), "%.0f %s", scaled_size, scaled_symbol); |
| 377 | else |
| 378 | g_snprintf (x, sizeof (x), "%.1f %s", scaled_size, scaled_symbol); |
360 | 379 | |
361 | | if (size / divisor > 999999999UL) |
362 | | { |
363 | | divisor = use_si ? (1000 * 1000 * 1000) : (1024 * 1024 * 1024); |
364 | | xtra = use_si ? _("GB") : _("GiB"); |
365 | | } |
366 | | } |
367 | | } |
368 | | g_snprintf (x, sizeof (x), "%.0f %s", 1.0 * size / divisor, xtra); |
369 | 380 | return x; |
370 | 381 | } |
371 | 382 | |
diff --git a/src/filemanager/file.c b/src/filemanager/file.c
index b9bee62..921099e 100644
a
|
b
|
dirsize_status_update_cb (status_msg_t * sm) |
2524 | 2524 | |
2525 | 2525 | /* update second (longer label) */ |
2526 | 2526 | label_set_textv (dsm->count_size, _("Directories: %zd, total size: %s"), |
2527 | | dsm->dir_count, size_trunc_sep (dsm->total_size, panels_options.kilobyte_si)); |
| 2527 | dsm->dir_count, size_trunc (dsm->total_size, panels_options.kilobyte_si)); |
2528 | 2528 | |
2529 | 2529 | /* enlarge dialog if required */ |
2530 | 2530 | if (WIDGET (dsm->count_size)->cols + 6 > wd->cols) |
diff --git a/src/filemanager/panel.c b/src/filemanager/panel.c
index b18cbac..38e740a 100644
a
|
b
|
display_total_marked_size (const WPanel * panel, int y, int x, gboolean size_onl |
1079 | 1079 | buf = size_only ? b_bytes : buffer; |
1080 | 1080 | cols = w->cols - 2; |
1081 | 1081 | |
1082 | | g_strlcpy (b_bytes, size_trunc_sep (panel->total, panels_options.kilobyte_si), |
| 1082 | g_strlcpy (b_bytes, size_trunc (panel->total, panels_options.kilobyte_si), |
1083 | 1083 | sizeof (b_bytes)); |
1084 | 1084 | |
1085 | 1085 | if (!size_only) |
… |
… |
show_dir (const WPanel * panel) |
1319 | 1319 | char buffer[BUF_SMALL]; |
1320 | 1320 | |
1321 | 1321 | g_snprintf (buffer, sizeof (buffer), " %s ", |
1322 | | size_trunc_sep (panel->dir.list[panel->selected].st.st_size, |
1323 | | panels_options.kilobyte_si)); |
| 1322 | size_trunc (panel->dir.list[panel->selected].st.st_size, |
| 1323 | panels_options.kilobyte_si)); |
1324 | 1324 | tty_setcolor (NORMAL_COLOR); |
1325 | 1325 | widget_move (w, w->lines - 1, 4); |
1326 | 1326 | tty_print_string (buffer); |