]> git.ipfire.org Git - fireperf.git/commitdiff
tui: Add a grid to the graph
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Sep 2024 14:52:49 +0000 (14:52 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Sep 2024 14:52:49 +0000 (14:52 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/tui.c

index 0e6433b9f1d5f73fb6bd7ac738e1104da6046abb..956de21701a6746217f50893b75c63b5dc98aa4c 100644 (file)
--- a/src/tui.c
+++ b/src/tui.c
@@ -29,6 +29,7 @@
 #include "ctx.h"
 #include "stats.h"
 #include "tui.h"
+#include "util.h"
 
 #define MAX_STATS 1024
 
@@ -137,6 +138,7 @@ static int fireperf_tui_setup(struct fireperf_tui* tui) {
        init_pair(2, COLOR_GREEN,  COLOR_BLACK);
        init_pair(3, COLOR_BLUE,   COLOR_BLACK);
        init_pair(4, COLOR_BLACK,  COLOR_WHITE);
+       init_pair(5, COLOR_YELLOW, COLOR_BLACK);
 
        // Enable raw mode
        raw();
@@ -277,6 +279,7 @@ static size_t fireperf_tui_get_peak_bps(struct fireperf_tui* tui, int max) {
 */
 static int fireperf_tui_draw_graph(struct fireperf_tui* tui) {
        const struct fireperf_stats* stats = NULL;
+       char* scale = NULL;
 
        int max_x = 0;
        int max_y = 0;
@@ -290,7 +293,30 @@ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) {
        size_t peak_bps = fireperf_tui_get_peak_bps(tui, max_x);
 
        // Figure out how many bps a step represents
-       double step = peak_bps / (max_y + 1);
+       double step = peak_bps / (max_y + 0);
+
+       // Make the brush yellow
+       wattron(tui->graph, COLOR_PAIR(5));
+
+       // Draw the grid
+       for (double line = 0; line <= 1; line += 0.25) {
+               scale = format_size(line * peak_bps, FIREPERF_FORMAT_BITS);
+               if (!scale)
+                       continue;
+
+               size_t l = strlen(scale);
+
+               // Draw a horizontal line
+               for (unsigned int x = 0; x < max_x - l - 1; x++) {
+                       mvwaddch(tui->graph, max_y - (max_y * line), x, ACS_HLINE);
+               }
+
+               // Write the scale
+               mvwaddnstr(tui->graph, max_y - (max_y * line), max_x - l, scale, l);
+               free(scale);
+       }
+
+       wattroff(tui->graph, COLOR_PAIR(5));
 
        // Make the brush red
        wattron(tui->graph, COLOR_PAIR(1));