diff --git a/panel-plugin/netload.c b/panel-plugin/netload.c index 95811df..d223a50 100644 --- a/panel-plugin/netload.c +++ b/panel-plugin/netload.c @@ -1,6 +1,7 @@ /* * Copyright 2003-2007 Bernhard Walle * Copyright 2010 Florian Rivoal + * Copyright 2014 OmegaPhil * ------------------------------------------------------------------------------------------------- * * This program is free software; you can redistribute it and/or modify it @@ -238,11 +239,11 @@ 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_byte_humanreadable( buffer[i], BUFSIZ, display[i], 2 ); - format_byte_humanreadable( buffer_panel[i], BUFSIZ, display[i], 0 ); + format_byte_humanreadable( buffer[i], BUFSIZ, display[i], 2, 2 ); + format_byte_humanreadable( buffer_panel[i], BUFSIZ, display[i], 0, 2 ); } - format_byte_humanreadable( buffer[TOT], BUFSIZ, (display[IN]+display[OUT]), 2 ); + format_byte_humanreadable( buffer[TOT], BUFSIZ, (display[IN]+display[OUT]), 2, 2 ); { char* ip = get_ip_address(&(global->monitor->data)); diff --git a/panel-plugin/utils.c b/panel-plugin/utils.c index 30647af..b39905d 100644 --- a/panel-plugin/utils.c +++ b/panel-plugin/utils.c @@ -1,5 +1,6 @@ /* * Copyright 2003, 2005 Bernhard Walle + * Copyright 2014 OmegaPhil * ------------------------------------------------------------------------------------------------- * * This program is free software; you can redistribute it and/or modify it @@ -70,7 +71,7 @@ unsigned long max_array( unsigned long array[], int size ) /* ---------------------------------------------------------------------------------------------- */ -char* format_byte_humanreadable(char* string, int stringsize, double number, int digits) +char* format_byte_humanreadable(char* string, int stringsize, double number, int digitssmall, int digitslarge) { char* str = string; char buffer[BUFSIZ], formatstring[BUFSIZ]; @@ -79,22 +80,32 @@ char* format_byte_humanreadable(char* string, int stringsize, double number, int unsigned int uidx = 1; double number_displayed = number / 1024.0; unsigned int i; - int numberOfIntegerChars, count; + int numberOfIntegerChars, count, digits; struct lconv* localeinfo = localeconv(); int grouping = (int)localeinfo->grouping[0] == 0 ? INT_MAX : (int)localeinfo->grouping[0]; - - + + /* determining digits length to use based on whether the number is + * 'large' (>= 1MiB) or 'small' - this allows to keep sufficient + * detail when reporting on large amounts of data + * no digits for smallest unit size */ + if (number <= 1024.0) + { + digits = 0; + } + else if (number < (1024.0 * 1024.0)) + { + digits = digitssmall; + } + else + { + digits = digitslarge; + } + /* sensible value for digits */ if (digits < 0 || digits >= 10) { 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)) diff --git a/panel-plugin/utils.h b/panel-plugin/utils.h index 1da6a9b..0735c18 100644 --- a/panel-plugin/utils.h +++ b/panel-plugin/utils.h @@ -1,5 +1,6 @@ /* XFce 4 - Netload Plugin * Copyright (c) 2003,2005 Bernhard Walle + * Copyright (c) 2014 OmegaPhil * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,10 +30,15 @@ * @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 formatted - * @param digits the number of digits after the decimal point + * @param digitssmall the number of digits after the decimal point for + * a 'small' number (currently less than 1 MiB) + * @param digitslarge the number of digits after the decimal point for + * a 'large' number (currently 1 MiB or more). This + * is intended to report greater detail for larger + * numbers * @return the string to allow concatening buffers or null */ -char* format_byte_humanreadable( char* string, int stringsize, double number, int digits ); +char* format_byte_humanreadable( char* string, int stringsize, double number, int digitssmall, int digitslarge ); /** * Returns the minimum of the array. The array must contain at least one element.