[Xfce4-commits] <xfce4-netload-plugin:master> Bug 8280: Format units automatically per Byte/KiB/MiB/GiB

Mike Massonnet noreply at xfce.org
Sun Jan 8 14:12:01 CET 2012


Updating branch refs/heads/master
         to e7f63558e4dfc70624f73427fe1800c60a2c1fda (commit)
       from 6256c6ce1aaad43cbddc90e6f553c106e60c5eda (commit)

commit e7f63558e4dfc70624f73427fe1800c60a2c1fda
Author: Harald Judt <h.judt at gmx.at>
Date:   Sun Jan 8 14:08:20 2012 +0100

    Bug 8280: Format units automatically per Byte/KiB/MiB/GiB

 panel-plugin/netload.c |   20 ++++++++++----------
 panel-plugin/utils.c   |   33 +++++++++++++++++++++++++++++----
 panel-plugin/utils.h   |   13 ++++++++-----
 3 files changed, 47 insertions(+), 19 deletions(-)

diff --git a/panel-plugin/netload.c b/panel-plugin/netload.c
index 42f1634..8d9b92d 100644
--- a/panel-plugin/netload.c
+++ b/panel-plugin/netload.c
@@ -236,26 +236,26 @@ static gboolean update_monitors(t_global_monitor *global)
         if (global->monitor->options.show_bars)
             gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(global->monitor->status[i]), temp);
 
-        format_with_thousandssep( buffer[i], BUFSIZ, display[i] / 1024.0, 2 );
+        format_byte_humanreadable( buffer[i], BUFSIZ, display[i], 2 );
     }
     
-    format_with_thousandssep( buffer[TOT], BUFSIZ, (display[IN]+display[OUT]) / 1024.0, 2 );
+    format_byte_humanreadable( buffer[TOT], BUFSIZ, (display[IN]+display[OUT]), 2 );
     
     {
         char* ip = get_ip_address(&(global->monitor->data));
         g_snprintf(caption, sizeof(caption), 
                 _("<< %s >> (%s)\nAverage of last %d measures:\n"
-                    "Incoming: %s kByte/s\nOutgoing: %s kByte/s\nTotal: %s kByte/s"),
+                    "Incoming: %s/s\nOutgoing: %s/s\nTotal: %s/s"),
                     get_name(&(global->monitor->data)), ip ? ip : _("no IP address"),
                     HISTSIZE_CALCULATE, buffer[IN], buffer[OUT], buffer[TOT]);
         gtk_label_set_text(GTK_LABEL(global->tooltip_text), caption);
 
         if (global->monitor->options.show_values)
         {
-            g_snprintf(received, sizeof(received), "%s KiB/s", buffer[IN]);
+            g_snprintf(received, sizeof(received), "%s/s", buffer[IN]);
             gtk_label_set_text(GTK_LABEL(global->monitor->rcv_label), received);
 
-            g_snprintf(sent, sizeof(sent), _("%s KiB/s"), buffer[OUT]);
+            g_snprintf(sent, sizeof(sent), _("%s/s"), buffer[OUT]);
             gtk_label_set_text(GTK_LABEL(global->monitor->sent_label), sent);
         }
     }
@@ -311,7 +311,7 @@ static void monitor_set_orientation (XfcePanelPlugin *plugin, GtkOrientation ori
     gtk_widget_show(global->monitor->label);
 
     global->monitor->rcv_label = gtk_label_new("");
-    gtk_label_set_width_chars(GTK_LABEL(global->monitor->rcv_label), 13);
+    gtk_label_set_width_chars(GTK_LABEL(global->monitor->rcv_label), 10);
     gtk_misc_set_alignment(GTK_MISC(global->monitor->rcv_label), 1.0f, 0.5f);
     gtk_widget_show(global->monitor->rcv_label);
 
@@ -321,7 +321,7 @@ static void monitor_set_orientation (XfcePanelPlugin *plugin, GtkOrientation ori
     }
 
     global->monitor->sent_label = gtk_label_new("");
-    gtk_label_set_width_chars(GTK_LABEL(global->monitor->sent_label), 13);
+    gtk_label_set_width_chars(GTK_LABEL(global->monitor->sent_label), 10);
     gtk_widget_show(global->monitor->sent_label);
 
     if (orientation == GTK_ORIENTATION_HORIZONTAL)
@@ -332,7 +332,7 @@ static void monitor_set_orientation (XfcePanelPlugin *plugin, GtkOrientation ori
             gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(global->monitor->status[i]),
                     GTK_PROGRESS_BOTTOM_TO_TOP);
         }
-		gtk_misc_set_alignment(GTK_MISC(global->monitor->sent_label), 0.0f, 0.5f);
+        gtk_misc_set_alignment(GTK_MISC(global->monitor->sent_label), 0.0f, 0.5f);
     }
     else
     {
@@ -342,7 +342,7 @@ static void monitor_set_orientation (XfcePanelPlugin *plugin, GtkOrientation ori
             gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(global->monitor->status[i]), 
                     GTK_PROGRESS_LEFT_TO_RIGHT);
         }
-		gtk_misc_set_alignment(GTK_MISC(global->monitor->sent_label), 1.0f, 0.5f);
+        gtk_misc_set_alignment(GTK_MISC(global->monitor->sent_label), 1.0f, 0.5f);
     }
 
     gtk_box_pack_start(GTK_BOX(global->monitor->box),
@@ -528,7 +528,7 @@ static void setup_monitor(t_global_monitor *global, gboolean supress_warnings)
             && !supress_warnings)
     {
         xfce_dialog_show_error (NULL, NULL,
-	    _("%s: Error in initalizing:\n%s"),
+        _("%s: Error in initalizing:\n%s"),
             _(APP_NAME),
             _(errormessages[
                 global->monitor->data.errorcode == 0 
diff --git a/panel-plugin/utils.c b/panel-plugin/utils.c
index 8ee5e89..53c9b1f 100644
--- a/panel-plugin/utils.c
+++ b/panel-plugin/utils.c
@@ -22,6 +22,9 @@
 #include <string.h>
 #include <limits.h>
 #include <locale.h>
+#include <glib.h>
+#include <glib/gprintf.h>
+#include <glib/gi18n.h>
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -67,11 +70,14 @@ unsigned long max_array( unsigned long array[], int size )
 
 
 /* ---------------------------------------------------------------------------------------------- */
-char* format_with_thousandssep(char* string, int stringsize, double number, int digits)
+char* format_byte_humanreadable(char* string, int stringsize, double number, int digits)
 {
     char* str = string;
     char buffer[BUFSIZ], formatstring[BUFSIZ];
     char* bufptr = buffer;
+    char* unit_names[] = { N_("B"), N_("KiB"), N_("MiB"), N_("GiB") };
+    unsigned int uidx = 0;
+    double number_displayed = number;
     unsigned int i;
     int numberOfIntegerChars, count;
     struct lconv* localeinfo = localeconv();
@@ -84,8 +90,22 @@ char* format_with_thousandssep(char* string, int stringsize, double number, int
         digits = 2;
     }
     
+    /* no digits for smallest unit size */
+    if (number <= 1024.0)
+    {
+        digits = 0;
+    }
+
+    /* calculate number and appropriate unit size for display */
+    while(number_displayed >= 1024.0 && uidx < sizeof(unit_names))
+    {
+        number_displayed /= 1024.0;
+        uidx++;
+    }
+
+    /* format number first */
     snprintf(formatstring, BUFSIZ, "%%.%df", digits);
-    snprintf(buffer, BUFSIZ, formatstring, number);
+    snprintf(buffer, BUFSIZ, formatstring, number_displayed);
     
     /* get the number of integer characters */
     count = numberOfIntegerChars = ( digits > 0
@@ -115,15 +135,20 @@ char* format_with_thousandssep(char* string, int stringsize, double number, int
         count--;
     }
     
-    /* Copy the rest */
+    /* Copy the rest of the number */
     while (digits > 0 && *bufptr != 0)
     {
         *str++ = *bufptr++;
     }
     
+    /* Add space */
+    *str++ = ' ';
+
     /* terminate with 0 finally */
     *str = 0;
     
+    /* Add the unit name */
+    g_strlcat(string, _(unit_names[uidx]), stringsize);
+
     return string;
 }
-
diff --git a/panel-plugin/utils.h b/panel-plugin/utils.h
index cee0a9f..1da6a9b 100644
--- a/panel-plugin/utils.h
+++ b/panel-plugin/utils.h
@@ -20,16 +20,19 @@
 #define UTILS_H
 
 /**
- * Formats the number according the current locale with thousands separator. E.g.
- * 1234.5678 is formated in a German locale with digits=2 to 1.234,57. If the size
- * is too small, <code>NULL</code> is returned and the string contains garbage.
+ * Formats the number into a number of the appropriate byte unit with
+ * a thousands separator, respecting the current locale. It appends
+ * the byte unit to the number. E.g. 1024000 byte will be formatted in
+ * a German locale with 2 digits to 1.000,00 KiB. If the size is too
+ * small, <code>NULL</code> is returned and the string contains
+ * garbage.
  * @param   string      a character array in which the result is stored
  * @param   stringsize  the size of the character array
- * @param   number      the number that should be formated
+ * @param   number      the number that should be formatted
  * @param   digits      the number of digits after the decimal point
  * @return  the string to allow concatening buffers or <code>null</code>
  */
-char* format_with_thousandssep( char* string, int stringsize, double number, int digits );
+char* format_byte_humanreadable( char* string, int stringsize, double number, int digits );
 
 /**
  * Returns the minimum of the array. The array must contain at least one element.


More information about the Xfce4-commits mailing list