diff --git a/src/conky.cc b/src/conky.cc index f099221c..0376da1c 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -584,10 +584,10 @@ void human_readable(long long num, char *buf, int size) { } if (short_units.get(*state)) { width = 5; - format = "%.*f%.1s"; + format = "%.*f %.1s"; } else { width = 7; - format = "%.*f%-.3s"; + format = "%.*f %-.3s"; } if (llabs(num) < 1000LL) { @@ -1220,6 +1220,38 @@ static void draw_string(const char *s) { memcpy(tmpstring1, s, tbs); } +#if defined(BUILD_MATH) && defined(BUILD_X11) +/// Format \a size as a real followed by closest SI unit, with \a prec number +/// of digits after the decimal point. +static std::string formatSizeWithUnits(double size, int prec = 1) { + int div = 0; + double rem = 0; + + while (size >= 1024.0 && + static_cast(div) < (sizeof suffixes / sizeof *suffixes)) { + rem = fmod(size, 1024.0); + div++; + size /= 1024.0; + } + + double size_d = size + rem / 1024.0; + + std::ostringstream result; + result.setf(std::ios::fixed, std::ios::floatfield); + result.precision(prec); + result << size_d; + result << " "; + + if (short_units.get(*state)) { + result << suffixes[div][0]; + } else { + result << suffixes[div]; + } + + return result.str(); +} +#endif /* BUILD_MATH && BUILD_X11 */ + int draw_each_line_inner(char *s, int special_index, int last_special_applied) { #ifndef BUILD_X11 static int cur_x, cur_y; /* current x and y for drawing */ @@ -1489,13 +1521,12 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) { if (show_graph_scale.get(*state) && (current->show_scale == 1)) { int tmp_x = cur_x; int tmp_y = cur_y; - char *tmp_str; cur_x += font_ascent() / 2; cur_y += font_h / 2; - if (asprintf(&tmp_str, "%.1f", current->scale)) { - draw_string(tmp_str); - free(tmp_str); - } + std::string tmp_str = formatSizeWithUnits( + current->scale_log != 0 ? std::pow(10.0, current->scale) + : current->scale); + draw_string(tmp_str.c_str()); cur_x = tmp_x; cur_y = tmp_y; } diff --git a/src/net_stat.cc b/src/net_stat.cc index 86a43c3c..975d7529 100644 --- a/src/net_stat.cc +++ b/src/net_stat.cc @@ -343,7 +343,7 @@ void parse_net_stat_graph_arg(struct text_object *obj, const char *arg, } /** - * returns the download speed in kiB/s for the interface referenced by obj + * returns the download speed in B/s for the interface referenced by obj * * @param[in] obj struct containting a member data, which is a struct * containing a void * to a net_stat struct @@ -351,13 +351,13 @@ void parse_net_stat_graph_arg(struct text_object *obj, const char *arg, double downspeedgraphval(struct text_object *obj) { auto *ns = static_cast(obj->data.opaque); - return (ns != nullptr ? (ns->recv_speed / 1024.0) : 0); + return (ns != nullptr ? ns->recv_speed : 0); } double upspeedgraphval(struct text_object *obj) { auto *ns = static_cast(obj->data.opaque); - return (ns != nullptr ? (ns->trans_speed / 1024.0) : 0); + return (ns != nullptr ? ns->trans_speed : 0); } #endif /* BUILD_X11 */ diff --git a/src/specials.cc b/src/specials.cc index 161367cb..8437d59e 100644 --- a/src/specials.cc +++ b/src/specials.cc @@ -590,7 +590,10 @@ void new_graph(struct text_object *obj, char *buf, int buf_max_size, } s->tempgrad = g->tempgrad; #ifdef BUILD_MATH - if ((g->flags & SF_SHOWLOG) != 0) { s->scale = log10(s->scale + 1); } + if ((g->flags & SF_SHOWLOG) != 0) { + s->scale_log = 1; + s->scale = log10(s->scale + 1); + } #endif int graph_id = ((struct graph *)obj->special_data)->id; diff --git a/src/specials.h b/src/specials.h index cea808eb..92733920 100644 --- a/src/specials.h +++ b/src/specials.h @@ -66,7 +66,8 @@ struct special_t { short show_scale; int graph_width; int graph_allocated; - int scaled; /* auto adjust maximum */ + int scaled; /* auto adjust maximum */ + int scale_log; unsigned long first_colour; // for graph gradient unsigned long last_colour; short font_added;