commit 8140cbcf601ff82bf61e71089a1095c7debbdcbc Author: Harald Judt Date: Tue Dec 27 16:49:50 2011 +0100 Rewrite utility function format_with_thousandssep into format_byte_humanreadable. After adding the "show values" option, the plugin needs a lot of space in the panel. This patch modifies the utility function so that it returns the number as a string with an appropriate unit size, e.g. 1.03 MiB. This makes it fit into less space, and additionally the space needed can be predicted. The length of the gtk widget label can therefore be smaller; 10 chars seemed to be a good value, a bit less might still be possible but is probably unsafe with bigger fonts. diff --git a/panel-plugin/netload.c b/panel-plugin/netload.c index ec8b152..0afd7d8 100644 --- a/panel-plugin/netload.c +++ b/panel-plugin/netload.c @@ -231,26 +231,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 kByte/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 kByte/s"), buffer[OUT]); + g_snprintf(sent, sizeof(sent), _("%s/s"), buffer[OUT]); gtk_label_set_text(GTK_LABEL(global->monitor->sent_label), sent); } } @@ -306,7 +306,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); @@ -316,7 +316,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_misc_set_alignment(GTK_MISC(global->monitor->sent_label), 0.0f, 0.5f); gtk_widget_show(global->monitor->sent_label); diff --git a/panel-plugin/utils.c b/panel-plugin/utils.c index 8ee5e89..63c1960 100644 --- a/panel-plugin/utils.c +++ b/panel-plugin/utils.c @@ -67,11 +67,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[] = { gettext("Byte"), gettext("KiB"), gettext("MiB"), gettext("GiB"), gettext("TiB"), gettext("PiB"), gettext("EiB") }; + unsigned int uidx = 0; + double number_displayed = number; unsigned int i; int numberOfIntegerChars, count; struct lconv* localeinfo = localeconv(); @@ -84,8 +87,26 @@ 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) + { + number_displayed /= 1024.0; + uidx++; + } + if (uidx >= sizeof(unit_names)) + { + uidx = sizeof(unit_names) - 1; + } + + /* 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 +136,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 */ + strcat(string, unit_names[uidx]); + 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, NULL 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, NULL 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 null */ -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.