Skip to content

Commit

Permalink
Implemented graph value clamping (#2000)
Browse files Browse the repository at this point in the history
* Implemented graph value clamping

* Update variables.yaml

* Update variables.yaml
  • Loading branch information
donutAnees authored Jul 30, 2024
1 parent 7ec86cf commit e03891f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
53 changes: 44 additions & 9 deletions doc/variables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m value)
- name: apcupsd_model
desc: Prints the model of the UPS.
- name: apcupsd_name
Expand Down Expand Up @@ -331,7 +332,9 @@ values:
numbers) when you use the -l switch. Takes the switch '-t' to use a
temperature gradient, which makes the gradient values change depending
on the amplitude of a particular graph value (try it and see). The flag
'-x' inverts the x axis and '-y' inverts the y axis of the graph.
'-x' inverts the x axis and '-y' inverts the y axis of the graph. The flag
'-m' sets a nonzero minimum/lowerbound, ensuring that all values are at
least the specified minimum (excluding zero).
args:
- (cpuN)
- (height),(width)
Expand All @@ -342,6 +345,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m value)
- name: curl
desc: |-
Download data from URI using Curl at the specified interval.
Expand Down Expand Up @@ -395,7 +399,8 @@ values:
'-t' to use a temperature gradient, which makes the gradient values
change depending on the amplitude of a particular graph value (try it
and see). The flag '-x' inverts the x axis and '-y' inverts the y axis
of the graph.
of the graph. The flag '-m' sets a nonzero minimum/lowerbound, ensuring
that all values are at least the specified minimum (excluding zero).
args:
- (device)
- (height),(width)
Expand All @@ -406,6 +411,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m value)
- name: diskiograph_read
desc: |-
Disk IO graph for reads, colours defined in hex, minus the
Expand All @@ -414,7 +420,9 @@ values:
use -l switch. Takes the switch '-t' to use a temperature gradient,
which makes the gradient values change depending on the amplitude of a
particular graph value (try it and see). The flag '-x' inverts the x
axis and '-y' inverts the y axis of the graph.
axis and '-y' inverts the y axis of the graph. The flag '-m' sets a nonzero
minimum/lowerbound, ensuring that all values are at least the specified
minimum (excluding zero).
args:
- (device)
- (height),(width)
Expand All @@ -425,6 +433,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m value)
- name: diskiograph_write
desc: |-
Disk IO graph for writes, colours defined in hex, minus the
Expand All @@ -433,7 +442,9 @@ values:
use -l switch. Takes the switch '-t' to use a temperature gradient,
which makes the gradient values change depending on the amplitude of a
particular graph value (try it and see). The flag '-x' inverts the x
axis and '-y' inverts the y axis of the graph.
axis and '-y' inverts the y axis of the graph. The flag '-m' sets a
nonzero minimum/lowerbound, ensuring that all values are at
least the specified minimum (excluding zero).
args:
- (device)
- (height),(width)
Expand All @@ -444,6 +455,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m value)
- name: distribution
desc: |-
The name of the distribution. It could be that some of the
Expand Down Expand Up @@ -471,7 +483,9 @@ values:
-l switch. Takes the switch '-t' to use a temperature gradient, which makes
the gradient values change depending on the amplitude of a particular
graph value (try it and see). The flag '-x' inverts the x axis and '-y'
inverts the y axis of the graph.
inverts the y axis of the graph. The flag '-m' sets a nonzero
minimum/lowerbound, ensuring that all values are at least the specified
minimum (excluding zero).
args:
- (netdev)
- (height),(width)
Expand All @@ -482,6 +496,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m)
- name: draft_mails
desc: |-
Number of mails marked as draft in the specified mailbox or
Expand Down Expand Up @@ -553,7 +568,9 @@ values:
-l switch to enable a logarithmic scale, which helps to see small values.
The default size for graphs can be controlled via the default_graph_height
and default_graph_width config settings. The flag '-x' inverts the x axis
and '-y' inverts the y axis of the graph.
and '-y' inverts the y axis of the graph. The flag '-m' sets a nonzero
minimum/lowerbound, ensuring that all values are at least the specified
minimum (excluding zero).
If you need to execute a command with spaces, you have a
couple options:
Expand Down Expand Up @@ -582,6 +599,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m value)
- name: execi
desc: |-
Same as exec, but with a specific interval in seconds. The
Expand Down Expand Up @@ -615,6 +633,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m value)
- name: execp
desc: |-
Executes a shell command and displays the output in conky.
Expand Down Expand Up @@ -1135,6 +1154,8 @@ values:
gradient, which makes the gradient values change depending on the
amplitude of a particular graph value (try it and see). The flag
'-x' inverts the x axis and '-y' inverts the y axis of the graph.
The flag '-m' sets a nonzero minimum/lowerbound, ensuring that all
values are at least the specified minimum (excluding zero).
args:
- (height),(width)
- (gradient colour 1)
Expand All @@ -1144,6 +1165,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m value)
- name: lowercase
desc: Converts all letters into lowercase.
args:
Expand Down Expand Up @@ -1190,7 +1212,9 @@ values:
(try it and see). Conky puts 'conky_' in front of function_name to
prevent accidental calls to the wrong function unless you put you
place 'conky_' in front of it yourself. The flag '-x' inverts the
x axis and '-y' inverts the y axis of the graph.
x axis and '-y' inverts the y axis of the graph. The flag '-m' sets
a nonzero minimum/lowerbound, ensuring that all values are at least
the specified minimum (excluding zero).
args:
- function_name
- (height),(width)
Expand All @@ -1201,6 +1225,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m value)
- name: lua_parse
desc: |-
Executes a Lua function with given parameters as per $lua,
Expand Down Expand Up @@ -1261,7 +1286,9 @@ values:
numbers) when you use the -l switch. Takes the switch '-t' to use a
temperature gradient, which makes the gradient values change depending
on the amplitude of a particular graph value (try it and see). The flag
'-x' inverts the x axis and '-y' inverts the y axis of the graph.
'-x' inverts the x axis and '-y' inverts the y axis of the graph. The flag
'-m' sets a nonzero minimum/lowerbound, ensuring that all values are at
least the specified minimum (excluding zero).
args:
- (height),(width)
- (gradient colour 1)
Expand All @@ -1271,6 +1298,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m value)
- name: meminactive
desc: Amount of inactive memory. FreeBSD only.
- name: memlaundry
Expand Down Expand Up @@ -1299,6 +1327,8 @@ values:
gradient, which makes the gradient values change depending on the
amplitude of a particular graph value (try it and see). The flag
'-x' inverts the x axis and '-y' inverts the y axis of the graph.
The flag '-m' sets a nonzero minimum/lowerbound, ensuring that all
values are at least the specified minimum (excluding zero).
args:
- (height),(width)
- (gradient colour 1)
Expand All @@ -1308,6 +1338,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m value)
- name: mixer
desc: |-
Prints the mixer value as reported by the OS. On Linux, this
Expand Down Expand Up @@ -1574,6 +1605,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m)
- GPU_ID
- name: offset
desc: Move text over by N pixels. See also $voffset.
Expand Down Expand Up @@ -2365,7 +2397,9 @@ values:
use a temperature gradient, which makes the gradient values
change depending on the amplitude of a particular graph value
(try it and see). The flag '-x' inverts the x axis and '-y'
inverts the y axis of the graph.
inverts the y axis of the graph. The flag '-m' sets a nonzero
minimum/lowerbound, ensuring that all values are at least the
specified minimum (excluding zero).
args:
- (netdev)
- (height),(width)
Expand All @@ -2376,6 +2410,7 @@ values:
- (-l)
- (-x)
- (-y)
- (-m)
- name: uptime
desc: Uptime.
- name: uptime_short
Expand Down
5 changes: 5 additions & 0 deletions src/conky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,11 @@ static inline void set_foreground_color(Colour c) {
static inline void draw_graph_bars(special_node *current, std::unique_ptr<Colour[]>& tmpcolour,
conky::vec2i& text_offset, int i, int &j, int w,
int colour_idx, int cur_x, int by, int h) {
double graphheight = current->graph[j] * (h - 1) / current->scale;
/* Check if graphheight is less than the minheight threshold, if so we must change it to the threshold */
if(graphheight > 0 && current->minheight - graphheight > 0) {
current->graph[j] = current->minheight * current->scale / (h - 1);
}
if (current->colours_set) {
if (current->tempgrad != 0) {
set_foreground_color(tmpcolour[static_cast<int>(
Expand Down
34 changes: 33 additions & 1 deletion src/specials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ struct graph {
char tempgrad;
char speedgraph; /* If the current graph is a speed graph */
char invertflag; /* If the axis needs to be inverted */
int minheight; /* Clamp values below this threshold to this threshold */
};

struct stippled_hr {
Expand Down Expand Up @@ -246,12 +247,13 @@ std::pair<char *, size_t> scan_command(const char *s) {
}

/**
* parses for [height,width] [color1 color2] [scale] [-t] [-l]
* parses for [height,width] [color1 color2] [scale] [-t] [-l] [-m value]
*
* -l will set the showlog flag, enabling logarithmic graph scales
* -t will set the tempgrad member to true, enabling temperature gradient colors
* -x will set the invertx flag to true, inverting the x axis
* -y will set the invertx flag to true, inverting the y axis
* -m will set the minheight to value, this will clamp values below the threshold to the threshold
*
* @param[out] obj struct in which to save width, height and other options
* @param[in] args argument string to parse
Expand All @@ -277,6 +279,7 @@ bool scan_graph(struct text_object *obj, const char *argstr, double defscale, ch
g->scale = defscale;
g->tempgrad = FALSE;
g->invertflag = FALSE;
g->minheight = 0;
if (speedGraph) {
g->speedgraph = TRUE;
}
Expand Down Expand Up @@ -307,6 +310,34 @@ bool scan_graph(struct text_object *obj, const char *argstr, double defscale, ch
g->invertflag |= SF_INVERTY;
}

/* set MINHEIGHT to specified value if '-m' specified.
* It doesn't matter where the argument is exactly.
* Accepted values are from [0-5] */
const char *position = strstr(argstr, " " MINHEIGHT);
if ((position != nullptr) ||
strncmp(argstr, MINHEIGHT, strlen(MINHEIGHT)) == 0) {
int minheight = 0;
position += strlen(MINHEIGHT) + 1;
int size = strlen(argstr);
// Avoid whitespaces
while(*position == ' ' && position < argstr + size) {
position++;
}
// Get the numeric value start and end position
const char* numStart = position;
while (isdigit(*position)) {
position++;
}
// Convert the numeric value to an integer
std::string numStr(numStart, position);
if (!numStr.empty()) {
minheight = atoi(numStr.c_str());
}
// If specified value is greater than the max threshold
minheight = minheight > 5 ? 5 : minheight;
g->minheight = minheight;
}

/* all the following functions try to interpret the beginning of a
* a string with different format strings. If successful, they return from
* the function */
Expand Down Expand Up @@ -652,6 +683,7 @@ void new_graph(struct text_object *obj, char *buf, int buf_max_size,
s->show_scale = 1;
}
s->tempgrad = g->tempgrad;
s->minheight = g->minheight;
#ifdef BUILD_MATH
if ((g->flags & SF_SHOWLOG) != 0) {
s->scale_log = 1;
Expand Down
2 changes: 2 additions & 0 deletions src/specials.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#define TEMPGRAD "-t"
#define INVERTX "-x"
#define INVERTY "-y"
#define MINHEIGHT "-m"

enum class text_node_t : uint32_t {
NONSPECIAL = 0,
Expand Down Expand Up @@ -85,6 +86,7 @@ struct special_node {
char speedgraph;
char invertx;
char inverty;
int minheight;
struct special_node *next;
};

Expand Down

0 comments on commit e03891f

Please sign in to comment.