Commit d099dacd authored by Laurent Defert's avatar Laurent Defert

Show legends

parent ea0d1f5e
#include <curses.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
......@@ -11,13 +12,18 @@
#define VALUES_PREALLOC 1024
FILE* input;
char **titles=NULL;
int titles_count = 0;
double **values = NULL;
int lines_count = 0;
int values_count = 0;
int scroll_graph = 1;
WINDOW* w = NULL;
int window_x = 0;
int show_help = 0;
unsigned int legends = 0;
#define LEGEND_HELP 1 << 0
#define LEGEND_TITLES 1 << 1
#define LEGEND_MAX 2
const char* help[] = {"h - Move graph to the left",
"l - Move graph to the right",
......@@ -38,7 +44,11 @@ void get_min_max(double* min, double* max)
getmaxyx(w, term_height, term_width);
(void)term_height;
*min = 0.0;
*max = 0.0;
*max = 1.0;
if(values == NULL || values_count == 0)
return;
/* Compute min and max values to be displayed */
for (i = 0; i < lines_count; ++i)
for (j = 0; j < term_width; ++j)
......@@ -102,16 +112,20 @@ void refresh_screen()
mvaddch(i, graph_left, '|');
/* Compute min and max values to be displayed */
for (i = 0; i < lines_count; ++i)
for (j = 0; j < graph_width; ++j)
if(values_count)
{
if(window_x + j < 0)
continue;
if (values[i][window_x + j] > max_value)
max_value = values[i][window_x + j];
if (values[i][window_x + j] < min_value)
min_value = values[i][window_x + j];
for (i = 0; i < lines_count; ++i)
{
for (j = 0; j < graph_width; ++j)
{
if(window_x + j < 0)
continue;
if (values[i][window_x + j] > max_value)
max_value = values[i][window_x + j];
if (values[i][window_x + j] < min_value)
min_value = values[i][window_x + j];
}
}
}
/* Draw X axis */
......@@ -159,7 +173,7 @@ void refresh_screen()
}
/* Draw curves */
if(show_help)
if(legends & LEGEND_HELP)
display_help();
for (i = 0; i < lines_count; ++i)
......@@ -179,9 +193,11 @@ void refresh_screen()
int h = (int) ((values[i][window_x + j]/(max_value - min_value)) * graph_height);
mvaddch(x_axis_top-h, x, (chtype)('a' + i));
}
if((legends & LEGEND_TITLES) && titles != NULL && titles_count > i)
mvprintw(graph_height - lines_count + i, graph_left + 2, "%s", titles[i]);
}
if(show_help)
if(legends & LEGEND_HELP)
display_help();
refresh();
......@@ -222,6 +238,7 @@ int read_input()
FD_ZERO(&rfds);
FD_SET(fileno(input), &rfds);
/* Wait for input on keyboard or from stdin */
r = select(fileno(input)+1, &rfds, NULL, NULL, &tv);
if(r == -1 && errno == EINTR)
......@@ -242,22 +259,42 @@ int read_input()
part = strtok_r(buf, " \t", &ptr);
do
{
if(i == lines_count)
if(isalpha(part[0]) && lines_count == 0)
{
int len;
titles_count++;
titles = (char**)realloc(titles, (unsigned int)(i+1) * sizeof(char*));
len = (int)strlen(part);
if(len > 0 && part[len-1] == '\n')
len -= 1;
if(len > 0 && part[len-1] == '\r')
len -= 1;
titles[i] = strndup(part, (size_t)len);
}
/* When all titles has ben retrieved */
if(lines_count == 0)
{
lines_count++;
values = (double**)realloc(values, (unsigned int)(i+1) * sizeof(double*));
values[i] = NULL;
}
char *endptr;
double value = strtod(part, &endptr);
if(!isalpha(part[0]))
{
char *endptr;
double value = strtod(part, &endptr);
if (*endptr == '\0' || *endptr == '\n')
values[i] = insert_value(values[i], value);
if (*endptr == '\0' || *endptr == '\n')
values[i] = insert_value(values[i], value);
}
i++;
} while ((part = strtok_r(NULL, " \t", &ptr)) != NULL);
values_count++;
if(lines_count == 0)
lines_count = i;
else
values_count++;
return 1;
}
......@@ -315,8 +352,11 @@ int read_kbd()
finish(0);
break;
case '?':
show_help = ~show_help;
break;
legends ^= LEGEND_HELP;
return 1;
case 't':
legends ^= LEGEND_TITLES;
return 1;
}
return 0;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment