From 594d0c85baff7e930ec4c9568690bb53d3cd5d2a Mon Sep 17 00:00:00 2001 From: Travis Yaeger Date: Thu, 6 Jun 2019 19:01:11 -0700 Subject: [PATCH] Fix bug of "Graph reset when using conditional color" Graphs would appear to change their previous data when any of the displayed special types (e.g. $color, $hr, $offset) was added or removed from the display (e.g. from a conditional statement). This is because Graphs were just looking at memory locations for previous graph data, but those locations could change if any special types were added or removed. Now the previous graph data will get stored explicitly. --- src/specials.cc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/specials.cc b/src/specials.cc index 3e6b4efb..cc96ff38 100644 --- a/src/specials.cc +++ b/src/specials.cc @@ -39,6 +39,7 @@ #endif /* HAVE_SYS_PARAM_H */ #include #include +#include #include "colours.h" #include "common.h" #include "conky.h" @@ -46,6 +47,9 @@ struct special_t *specials = nullptr; int special_count; +int graph_count = 0; + +std::map graphs; namespace { conky::range_config_setting default_bar_width( @@ -90,6 +94,7 @@ struct gauge { }; struct graph { + int id; char flags; int width, height; unsigned int first_colour, last_colour; @@ -203,6 +208,7 @@ char *scan_graph(struct text_object *obj, const char *args, double defscale) { obj->special_data = g; /* zero width means all space that is available */ + g->id = ++graph_count; g->width = default_graph_width.get(*state); g->height = default_graph_height.get(*state); g->first_colour = 0; @@ -501,6 +507,32 @@ graph_buf_end: *p = '\0'; } +double* copy_graph(double* original_graph, int graph_width) { + double* new_graph = new double[graph_width]; + + memcpy(new_graph, original_graph, graph_width * sizeof(double)); + + return new_graph; +} + +double* retrieve_graph(int graph_id, int graph_width) { + if (graphs.find(graph_id) == graphs.end()) { + return new double[graph_width]; + } + else { + return copy_graph(graphs[graph_id], graph_width); + } +} + +void store_graph(int graph_id, struct special_t *s) { + if (s->graph == nullptr) { + graphs[graph_id] = nullptr; + } + else { + graphs[graph_id] = copy_graph(s->graph, s->graph_width); + } +} + /** * Creates a visual graph and/or appends val to the graph / plot * @@ -559,8 +591,14 @@ void new_graph(struct text_object *obj, char *buf, int buf_max_size, #ifdef BUILD_MATH if ((g->flags & SF_SHOWLOG) != 0) { s->scale = log10(s->scale + 1); } #endif + + int graph_id = ((struct graph*)obj->special_data)->id; + s->graph = retrieve_graph(graph_id, s->graph_width); + graph_append(s, val, g->flags); + store_graph(graph_id, s); + if (out_to_stdout.get(*state)) { new_graph_in_shell(s, buf, buf_max_size); } }