Skip to content

Commit e03891f

Browse files
authored
Implemented graph value clamping (#2000)
* Implemented graph value clamping * Update variables.yaml * Update variables.yaml
1 parent 7ec86cf commit e03891f

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed

‎doc/variables.yaml‎

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ values:
9999
- (-l)
100100
- (-x)
101101
- (-y)
102+
- (-m value)
102103
- name: apcupsd_model
103104
desc: Prints the model of the UPS.
104105
- name: apcupsd_name
@@ -331,7 +332,9 @@ values:
331332
numbers) when you use the -l switch. Takes the switch '-t' to use a
332333
temperature gradient, which makes the gradient values change depending
333334
on the amplitude of a particular graph value (try it and see). The flag
334-
'-x' inverts the x axis and '-y' inverts the y axis of the graph.
335+
'-x' inverts the x axis and '-y' inverts the y axis of the graph. The flag
336+
'-m' sets a nonzero minimum/lowerbound, ensuring that all values are at
337+
least the specified minimum (excluding zero).
335338
args:
336339
- (cpuN)
337340
- (height),(width)
@@ -342,6 +345,7 @@ values:
342345
- (-l)
343346
- (-x)
344347
- (-y)
348+
- (-m value)
345349
- name: curl
346350
desc: |-
347351
Download data from URI using Curl at the specified interval.
@@ -395,7 +399,8 @@ values:
395399
'-t' to use a temperature gradient, which makes the gradient values
396400
change depending on the amplitude of a particular graph value (try it
397401
and see). The flag '-x' inverts the x axis and '-y' inverts the y axis
398-
of the graph.
402+
of the graph. The flag '-m' sets a nonzero minimum/lowerbound, ensuring
403+
that all values are at least the specified minimum (excluding zero).
399404
args:
400405
- (device)
401406
- (height),(width)
@@ -406,6 +411,7 @@ values:
406411
- (-l)
407412
- (-x)
408413
- (-y)
414+
- (-m value)
409415
- name: diskiograph_read
410416
desc: |-
411417
Disk IO graph for reads, colours defined in hex, minus the
@@ -414,7 +420,9 @@ values:
414420
use -l switch. Takes the switch '-t' to use a temperature gradient,
415421
which makes the gradient values change depending on the amplitude of a
416422
particular graph value (try it and see). The flag '-x' inverts the x
417-
axis and '-y' inverts the y axis of the graph.
423+
axis and '-y' inverts the y axis of the graph. The flag '-m' sets a nonzero
424+
minimum/lowerbound, ensuring that all values are at least the specified
425+
minimum (excluding zero).
418426
args:
419427
- (device)
420428
- (height),(width)
@@ -425,6 +433,7 @@ values:
425433
- (-l)
426434
- (-x)
427435
- (-y)
436+
- (-m value)
428437
- name: diskiograph_write
429438
desc: |-
430439
Disk IO graph for writes, colours defined in hex, minus the
@@ -433,7 +442,9 @@ values:
433442
use -l switch. Takes the switch '-t' to use a temperature gradient,
434443
which makes the gradient values change depending on the amplitude of a
435444
particular graph value (try it and see). The flag '-x' inverts the x
436-
axis and '-y' inverts the y axis of the graph.
445+
axis and '-y' inverts the y axis of the graph. The flag '-m' sets a
446+
nonzero minimum/lowerbound, ensuring that all values are at
447+
least the specified minimum (excluding zero).
437448
args:
438449
- (device)
439450
- (height),(width)
@@ -444,6 +455,7 @@ values:
444455
- (-l)
445456
- (-x)
446457
- (-y)
458+
- (-m value)
447459
- name: distribution
448460
desc: |-
449461
The name of the distribution. It could be that some of the
@@ -471,7 +483,9 @@ values:
471483
-l switch. Takes the switch '-t' to use a temperature gradient, which makes
472484
the gradient values change depending on the amplitude of a particular
473485
graph value (try it and see). The flag '-x' inverts the x axis and '-y'
474-
inverts the y axis of the graph.
486+
inverts the y axis of the graph. The flag '-m' sets a nonzero
487+
minimum/lowerbound, ensuring that all values are at least the specified
488+
minimum (excluding zero).
475489
args:
476490
- (netdev)
477491
- (height),(width)
@@ -482,6 +496,7 @@ values:
482496
- (-l)
483497
- (-x)
484498
- (-y)
499+
- (-m)
485500
- name: draft_mails
486501
desc: |-
487502
Number of mails marked as draft in the specified mailbox or
@@ -553,7 +568,9 @@ values:
553568
-l switch to enable a logarithmic scale, which helps to see small values.
554569
The default size for graphs can be controlled via the default_graph_height
555570
and default_graph_width config settings. The flag '-x' inverts the x axis
556-
and '-y' inverts the y axis of the graph.
571+
and '-y' inverts the y axis of the graph. The flag '-m' sets a nonzero
572+
minimum/lowerbound, ensuring that all values are at least the specified
573+
minimum (excluding zero).
557574
558575
If you need to execute a command with spaces, you have a
559576
couple options:
@@ -582,6 +599,7 @@ values:
582599
- (-l)
583600
- (-x)
584601
- (-y)
602+
- (-m value)
585603
- name: execi
586604
desc: |-
587605
Same as exec, but with a specific interval in seconds. The
@@ -615,6 +633,7 @@ values:
615633
- (-l)
616634
- (-x)
617635
- (-y)
636+
- (-m value)
618637
- name: execp
619638
desc: |-
620639
Executes a shell command and displays the output in conky.
@@ -1135,6 +1154,8 @@ values:
11351154
gradient, which makes the gradient values change depending on the
11361155
amplitude of a particular graph value (try it and see). The flag
11371156
'-x' inverts the x axis and '-y' inverts the y axis of the graph.
1157+
The flag '-m' sets a nonzero minimum/lowerbound, ensuring that all
1158+
values are at least the specified minimum (excluding zero).
11381159
args:
11391160
- (height),(width)
11401161
- (gradient colour 1)
@@ -1144,6 +1165,7 @@ values:
11441165
- (-l)
11451166
- (-x)
11461167
- (-y)
1168+
- (-m value)
11471169
- name: lowercase
11481170
desc: Converts all letters into lowercase.
11491171
args:
@@ -1190,7 +1212,9 @@ values:
11901212
(try it and see). Conky puts 'conky_' in front of function_name to
11911213
prevent accidental calls to the wrong function unless you put you
11921214
place 'conky_' in front of it yourself. The flag '-x' inverts the
1193-
x axis and '-y' inverts the y axis of the graph.
1215+
x axis and '-y' inverts the y axis of the graph. The flag '-m' sets
1216+
a nonzero minimum/lowerbound, ensuring that all values are at least
1217+
the specified minimum (excluding zero).
11941218
args:
11951219
- function_name
11961220
- (height),(width)
@@ -1201,6 +1225,7 @@ values:
12011225
- (-l)
12021226
- (-x)
12031227
- (-y)
1228+
- (-m value)
12041229
- name: lua_parse
12051230
desc: |-
12061231
Executes a Lua function with given parameters as per $lua,
@@ -1261,7 +1286,9 @@ values:
12611286
numbers) when you use the -l switch. Takes the switch '-t' to use a
12621287
temperature gradient, which makes the gradient values change depending
12631288
on the amplitude of a particular graph value (try it and see). The flag
1264-
'-x' inverts the x axis and '-y' inverts the y axis of the graph.
1289+
'-x' inverts the x axis and '-y' inverts the y axis of the graph. The flag
1290+
'-m' sets a nonzero minimum/lowerbound, ensuring that all values are at
1291+
least the specified minimum (excluding zero).
12651292
args:
12661293
- (height),(width)
12671294
- (gradient colour 1)
@@ -1271,6 +1298,7 @@ values:
12711298
- (-l)
12721299
- (-x)
12731300
- (-y)
1301+
- (-m value)
12741302
- name: meminactive
12751303
desc: Amount of inactive memory. FreeBSD only.
12761304
- name: memlaundry
@@ -1299,6 +1327,8 @@ values:
12991327
gradient, which makes the gradient values change depending on the
13001328
amplitude of a particular graph value (try it and see). The flag
13011329
'-x' inverts the x axis and '-y' inverts the y axis of the graph.
1330+
The flag '-m' sets a nonzero minimum/lowerbound, ensuring that all
1331+
values are at least the specified minimum (excluding zero).
13021332
args:
13031333
- (height),(width)
13041334
- (gradient colour 1)
@@ -1308,6 +1338,7 @@ values:
13081338
- (-l)
13091339
- (-x)
13101340
- (-y)
1341+
- (-m value)
13111342
- name: mixer
13121343
desc: |-
13131344
Prints the mixer value as reported by the OS. On Linux, this
@@ -1574,6 +1605,7 @@ values:
15741605
- (-l)
15751606
- (-x)
15761607
- (-y)
1608+
- (-m)
15771609
- GPU_ID
15781610
- name: offset
15791611
desc: Move text over by N pixels. See also $voffset.
@@ -2365,7 +2397,9 @@ values:
23652397
use a temperature gradient, which makes the gradient values
23662398
change depending on the amplitude of a particular graph value
23672399
(try it and see). The flag '-x' inverts the x axis and '-y'
2368-
inverts the y axis of the graph.
2400+
inverts the y axis of the graph. The flag '-m' sets a nonzero
2401+
minimum/lowerbound, ensuring that all values are at least the
2402+
specified minimum (excluding zero).
23692403
args:
23702404
- (netdev)
23712405
- (height),(width)
@@ -2376,6 +2410,7 @@ values:
23762410
- (-l)
23772411
- (-x)
23782412
- (-y)
2413+
- (-m)
23792414
- name: uptime
23802415
desc: Uptime.
23812416
- name: uptime_short

‎src/conky.cc‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,11 @@ static inline void set_foreground_color(Colour c) {
998998
static inline void draw_graph_bars(special_node *current, std::unique_ptr<Colour[]>& tmpcolour,
999999
conky::vec2i& text_offset, int i, int &j, int w,
10001000
int colour_idx, int cur_x, int by, int h) {
1001+
double graphheight = current->graph[j] * (h - 1) / current->scale;
1002+
/* Check if graphheight is less than the minheight threshold, if so we must change it to the threshold */
1003+
if(graphheight > 0 && current->minheight - graphheight > 0) {
1004+
current->graph[j] = current->minheight * current->scale / (h - 1);
1005+
}
10011006
if (current->colours_set) {
10021007
if (current->tempgrad != 0) {
10031008
set_foreground_color(tmpcolour[static_cast<int>(

‎src/specials.cc‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ struct graph {
113113
char tempgrad;
114114
char speedgraph; /* If the current graph is a speed graph */
115115
char invertflag; /* If the axis needs to be inverted */
116+
int minheight; /* Clamp values below this threshold to this threshold */
116117
};
117118

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

248249
/**
249-
* parses for [height,width] [color1 color2] [scale] [-t] [-l]
250+
* parses for [height,width] [color1 color2] [scale] [-t] [-l] [-m value]
250251
*
251252
* -l will set the showlog flag, enabling logarithmic graph scales
252253
* -t will set the tempgrad member to true, enabling temperature gradient colors
253254
* -x will set the invertx flag to true, inverting the x axis
254255
* -y will set the invertx flag to true, inverting the y axis
256+
* -m will set the minheight to value, this will clamp values below the threshold to the threshold
255257
*
256258
* @param[out] obj struct in which to save width, height and other options
257259
* @param[in] args argument string to parse
@@ -277,6 +279,7 @@ bool scan_graph(struct text_object *obj, const char *argstr, double defscale, ch
277279
g->scale = defscale;
278280
g->tempgrad = FALSE;
279281
g->invertflag = FALSE;
282+
g->minheight = 0;
280283
if (speedGraph) {
281284
g->speedgraph = TRUE;
282285
}
@@ -307,6 +310,34 @@ bool scan_graph(struct text_object *obj, const char *argstr, double defscale, ch
307310
g->invertflag |= SF_INVERTY;
308311
}
309312

313+
/* set MINHEIGHT to specified value if '-m' specified.
314+
* It doesn't matter where the argument is exactly.
315+
* Accepted values are from [0-5] */
316+
const char *position = strstr(argstr, " " MINHEIGHT);
317+
if ((position != nullptr) ||
318+
strncmp(argstr, MINHEIGHT, strlen(MINHEIGHT)) == 0) {
319+
int minheight = 0;
320+
position += strlen(MINHEIGHT) + 1;
321+
int size = strlen(argstr);
322+
// Avoid whitespaces
323+
while(*position == ' ' && position < argstr + size) {
324+
position++;
325+
}
326+
// Get the numeric value start and end position
327+
const char* numStart = position;
328+
while (isdigit(*position)) {
329+
position++;
330+
}
331+
// Convert the numeric value to an integer
332+
std::string numStr(numStart, position);
333+
if (!numStr.empty()) {
334+
minheight = atoi(numStr.c_str());
335+
}
336+
// If specified value is greater than the max threshold
337+
minheight = minheight > 5 ? 5 : minheight;
338+
g->minheight = minheight;
339+
}
340+
310341
/* all the following functions try to interpret the beginning of a
311342
* a string with different format strings. If successful, they return from
312343
* the function */
@@ -652,6 +683,7 @@ void new_graph(struct text_object *obj, char *buf, int buf_max_size,
652683
s->show_scale = 1;
653684
}
654685
s->tempgrad = g->tempgrad;
686+
s->minheight = g->minheight;
655687
#ifdef BUILD_MATH
656688
if ((g->flags & SF_SHOWLOG) != 0) {
657689
s->scale_log = 1;

‎src/specials.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#define TEMPGRAD "-t"
4242
#define INVERTX "-x"
4343
#define INVERTY "-y"
44+
#define MINHEIGHT "-m"
4445

4546
enum class text_node_t : uint32_t {
4647
NONSPECIAL = 0,
@@ -85,6 +86,7 @@ struct special_node {
8586
char speedgraph;
8687
char invertx;
8788
char inverty;
89+
int minheight;
8890
struct special_node *next;
8991
};
9092

0 commit comments

Comments
 (0)