Skip to content

Commit

Permalink
Implemented Invert Y Axis Flag Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
donutAnees committed Jul 23, 2024
1 parent a47285f commit b021007
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
16 changes: 9 additions & 7 deletions src/conky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1009,15 +1009,17 @@ static inline void draw_graph_bars(special_node *current, std::unique_ptr<Colour
set_foreground_color(tmpcolour[colour_idx++]);
}
}
/* Handle the case where y axis is to be inverted */
int offsety1 = current->inverty ? by : by + h;
int offsety2 = current->inverty ? by + current->graph[j] * (h - 1) / current->scale
: round_to_positive_int(static_cast<double>(by) + h -
current->graph[j] * (h - 1) /
current->scale);
/* this is mugfugly, but it works */
if (display_output()) {
display_output()->draw_line(
text_offset.x() + cur_x + i + 1, text_offset.y() + by + h,
text_offset.x() + cur_x + i + 1,
text_offset.y() +
round_to_positive_int(static_cast<double>(by) + h -
current->graph[j] * (h - 1) /
current->scale));
text_offset.x() + cur_x + i + 1, text_offset.y() + offsety1,
text_offset.x() + cur_x + i + 1, text_offset.y() + offsety2);
}
++j;
}
Expand Down Expand Up @@ -1321,7 +1323,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) {
}
colour_idx = 0;
if(current->invertx){
for (i = 0; i <= w- 2; i++) {
for (i = 0; i <= w - 2; i++) {
draw_graph_bars(current, tmpcolour, text_offset,
i, j, w, colour_idx, cur_x, by, h);
}
Expand Down
19 changes: 13 additions & 6 deletions src/specials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ conky::simple_config_setting<std::string> console_graph_ticks(

/* special flag for inverting axis */
#define SF_INVERTX (1 << 0)
#define SF_INVERTY (1 << 1)

/*
* Special data typedefs
Expand Down Expand Up @@ -250,6 +251,7 @@ std::pair<char *, size_t> scan_command(const char *s) {
* -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
*
* @param[out] obj struct in which to save width, height and other options
* @param[in] args argument string to parse
Expand Down Expand Up @@ -298,6 +300,12 @@ bool scan_graph(struct text_object *obj, const char *argstr, double defscale, ch
strncmp(argstr, INVERTX, strlen(INVERTX)) == 0) {
g->invertflag |= SF_INVERTX;
}
/* set inverty to true if '-y' specified.
* It doesn't matter where the argument is exactly. */
if ((strstr(argstr, " " INVERTY) != nullptr) ||
strncmp(argstr, INVERTY, strlen(INVERTY)) == 0) {
g->invertflag |= SF_INVERTY;
}

/* all the following functions try to interpret the beginning of a
* a string with different format strings. If successful, they return from
Expand All @@ -322,9 +330,7 @@ bool scan_graph(struct text_object *obj, const char *argstr, double defscale, ch
* therfore we ensure last_colour_name is not TEMPGRAD or LOGGRAPH */
if (sscanf(argstr, "%d,%d %s %s", &g->height, &g->width, first_colour_name,
last_colour_name) == 4 &&
strcmp(last_colour_name, TEMPGRAD) != 0 &&
strcmp(last_colour_name, LOGGRAPH) != 0 &&
strcmp(last_colour_name, INVERTX) != 0) {
strchr(last_colour_name,'-') == NULL) {
apply_graph_colours(g, first_colour_name, last_colour_name);
return true;
}
Expand Down Expand Up @@ -365,9 +371,7 @@ bool scan_graph(struct text_object *obj, const char *argstr, double defscale, ch
* This could match as [scale] [-l | -t],
* therfore we ensure last_colour_name is not TEMPGRAD or LOGGRAPH */
if (sscanf(argstr, "%s %s", first_colour_name, last_colour_name) == 2 &&
strcmp(last_colour_name, TEMPGRAD) != 0 &&
strcmp(last_colour_name, LOGGRAPH) != 0 &&
strcmp(last_colour_name, INVERTX) != 0) {
strchr(last_colour_name,'-') == NULL) {
apply_graph_colours(g, first_colour_name, last_colour_name);
return true;
}
Expand Down Expand Up @@ -647,6 +651,9 @@ void new_graph(struct text_object *obj, char *buf, int buf_max_size,
if ((g->invertflag & SF_INVERTX) != 0){
s->invertx = 1;
}
if ((g->invertflag & SF_INVERTY) != 0){
s->inverty = 1;
}
if (g->speedgraph) {
s->speedgraph = TRUE;
}
Expand Down
2 changes: 2 additions & 0 deletions src/specials.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#define LOGGRAPH "-l"
#define TEMPGRAD "-t"
#define INVERTX "-x"
#define INVERTY "-y"

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

Expand Down

0 comments on commit b021007

Please sign in to comment.