Ticket #3666: 0001-size_trunc-autoscale-size-with-proper-prefixes.patch

File 0001-size_trunc-autoscale-size-with-proper-prefixes.patch, 4.2 KB (added by michael-o, 8 years ago)
  • lib/util.c

    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) 
    346346{ 
    347347    static char x[BUF_TINY]; 
    348348    uintmax_t divisor = 1; 
    349     const char *xtra = _("B"); 
     349    const char *scaled_symbol = _("B"); 
     350    double scaled_size; 
    350351 
    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) 
    352363    { 
    353364        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    } 
    355372 
    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); 
    360379 
    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); 
    369380    return x; 
    370381} 
    371382 
  • src/filemanager/file.c

    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) 
    25242524 
    25252525    /* update second (longer label) */ 
    25262526    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)); 
    25282528 
    25292529    /* enlarge dialog if required */ 
    25302530    if (WIDGET (dsm->count_size)->cols + 6 > wd->cols) 
  • src/filemanager/panel.c

    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 
    10791079    buf = size_only ? b_bytes : buffer; 
    10801080    cols = w->cols - 2; 
    10811081 
    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), 
    10831083               sizeof (b_bytes)); 
    10841084 
    10851085    if (!size_only) 
    show_dir (const WPanel * panel) 
    13191319                char buffer[BUF_SMALL]; 
    13201320 
    13211321                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)); 
    13241324                tty_setcolor (NORMAL_COLOR); 
    13251325                widget_move (w, w->lines - 1, 4); 
    13261326                tty_print_string (buffer);