Bug 8280: Format units automatically per Byte/KiB/MiB/GiB
parent
6256c6ce1a
commit
e7f63558e4
|
@ -236,26 +236,26 @@ static gboolean update_monitors(t_global_monitor *global)
|
||||||
if (global->monitor->options.show_bars)
|
if (global->monitor->options.show_bars)
|
||||||
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(global->monitor->status[i]), temp);
|
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));
|
char* ip = get_ip_address(&(global->monitor->data));
|
||||||
g_snprintf(caption, sizeof(caption),
|
g_snprintf(caption, sizeof(caption),
|
||||||
_("<< %s >> (%s)\nAverage of last %d measures:\n"
|
_("<< %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"),
|
get_name(&(global->monitor->data)), ip ? ip : _("no IP address"),
|
||||||
HISTSIZE_CALCULATE, buffer[IN], buffer[OUT], buffer[TOT]);
|
HISTSIZE_CALCULATE, buffer[IN], buffer[OUT], buffer[TOT]);
|
||||||
gtk_label_set_text(GTK_LABEL(global->tooltip_text), caption);
|
gtk_label_set_text(GTK_LABEL(global->tooltip_text), caption);
|
||||||
|
|
||||||
if (global->monitor->options.show_values)
|
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);
|
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);
|
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);
|
gtk_widget_show(global->monitor->label);
|
||||||
|
|
||||||
global->monitor->rcv_label = gtk_label_new("");
|
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_misc_set_alignment(GTK_MISC(global->monitor->rcv_label), 1.0f, 0.5f);
|
||||||
gtk_widget_show(global->monitor->rcv_label);
|
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("");
|
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);
|
gtk_widget_show(global->monitor->sent_label);
|
||||||
|
|
||||||
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
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_bar_set_orientation(GTK_PROGRESS_BAR(global->monitor->status[i]),
|
||||||
GTK_PROGRESS_BOTTOM_TO_TOP);
|
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
|
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_bar_set_orientation(GTK_PROGRESS_BAR(global->monitor->status[i]),
|
||||||
GTK_PROGRESS_LEFT_TO_RIGHT);
|
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),
|
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)
|
&& !supress_warnings)
|
||||||
{
|
{
|
||||||
xfce_dialog_show_error (NULL, NULL,
|
xfce_dialog_show_error (NULL, NULL,
|
||||||
_("%s: Error in initalizing:\n%s"),
|
_("%s: Error in initalizing:\n%s"),
|
||||||
_(APP_NAME),
|
_(APP_NAME),
|
||||||
_(errormessages[
|
_(errormessages[
|
||||||
global->monitor->data.errorcode == 0
|
global->monitor->data.errorcode == 0
|
||||||
|
|
|
@ -22,6 +22,9 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
|
#include <glib.h>
|
||||||
|
#include <glib/gprintf.h>
|
||||||
|
#include <glib/gi18n.h>
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
||||||
#include <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* str = string;
|
||||||
char buffer[BUFSIZ], formatstring[BUFSIZ];
|
char buffer[BUFSIZ], formatstring[BUFSIZ];
|
||||||
char* bufptr = buffer;
|
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;
|
unsigned int i;
|
||||||
int numberOfIntegerChars, count;
|
int numberOfIntegerChars, count;
|
||||||
struct lconv* localeinfo = localeconv();
|
struct lconv* localeinfo = localeconv();
|
||||||
|
@ -84,8 +90,22 @@ char* format_with_thousandssep(char* string, int stringsize, double number, int
|
||||||
digits = 2;
|
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(formatstring, BUFSIZ, "%%.%df", digits);
|
||||||
snprintf(buffer, BUFSIZ, formatstring, number);
|
snprintf(buffer, BUFSIZ, formatstring, number_displayed);
|
||||||
|
|
||||||
/* get the number of integer characters */
|
/* get the number of integer characters */
|
||||||
count = numberOfIntegerChars = ( digits > 0
|
count = numberOfIntegerChars = ( digits > 0
|
||||||
|
@ -115,15 +135,20 @@ char* format_with_thousandssep(char* string, int stringsize, double number, int
|
||||||
count--;
|
count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Copy the rest */
|
/* Copy the rest of the number */
|
||||||
while (digits > 0 && *bufptr != 0)
|
while (digits > 0 && *bufptr != 0)
|
||||||
{
|
{
|
||||||
*str++ = *bufptr++;
|
*str++ = *bufptr++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Add space */
|
||||||
|
*str++ = ' ';
|
||||||
|
|
||||||
/* terminate with 0 finally */
|
/* terminate with 0 finally */
|
||||||
*str = 0;
|
*str = 0;
|
||||||
|
|
||||||
|
/* Add the unit name */
|
||||||
|
g_strlcat(string, _(unit_names[uidx]), stringsize);
|
||||||
|
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,16 +20,19 @@
|
||||||
#define UTILS_H
|
#define UTILS_H
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats the number according the current locale with thousands separator. E.g.
|
* Formats the number into a number of the appropriate byte unit with
|
||||||
* 1234.5678 is formated in a German locale with digits=2 to 1.234,57. If the size
|
* a thousands separator, respecting the current locale. It appends
|
||||||
* is too small, <code>NULL</code> is returned and the string contains garbage.
|
* 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 string a character array in which the result is stored
|
||||||
* @param stringsize the size of the character array
|
* @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
|
* @param digits the number of digits after the decimal point
|
||||||
* @return the string to allow concatening buffers or <code>null</code>
|
* @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.
|
* Returns the minimum of the array. The array must contain at least one element.
|
||||||
|
|
Loading…
Reference in New Issue