From: Michael Tremer Date: Tue, 24 Sep 2024 14:31:31 +0000 (+0000) Subject: tui: Show more information in the status bar X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=94302377749d5dd44200ac708834f8f71ec545a9;p=fireperf.git tui: Show more information in the status bar Signed-off-by: Michael Tremer --- diff --git a/src/tui.c b/src/tui.c index 6d8e952..96863f7 100644 --- a/src/tui.c +++ b/src/tui.c @@ -53,6 +53,27 @@ struct fireperf_tui { WINDOW* status; }; +static const struct fireperf_stats* fireperf_tui_get_stats(struct fireperf_tui* tui, int i) { + return &tui->stats[(tui->s - i) % MAX_STATS]; +} + +static size_t fireperf_tui_get_peak_bps(struct fireperf_tui* tui, int max) { + const struct fireperf_stats* stats = NULL; + size_t peak = 0; + + for (unsigned int i = 0; i < max; i++) { + stats = fireperf_tui_get_stats(tui, i); + + if (stats->bytes_sent > peak) + peak = stats->bytes_sent; + + if (stats->bytes_received > peak) + peak = stats->bytes_received; + } + + return peak; +} + static int fireperf_tui_setup_graph(struct fireperf_tui* tui) { int r; @@ -90,32 +111,93 @@ static int fireperf_tui_setup_frame(struct fireperf_tui* tui) { return 0; } -static int fireperf_tui_setup_status(struct fireperf_tui* tui) { + +static int fireperf_tui_update_status(struct fireperf_tui* tui) { + const struct fireperf_stats* stats = NULL; int r; - // Create the window - tui->status = newwin(1, COLS, LINES - 1, 0); - if (!tui->status) - return 1; + int max_x = 0; + int max_y = 0; - // Create status line - move(LINES - 1, 0); + // Fetch the dimensions of the status bar + getmaxyx(tui->status, max_y, max_x); - const char* help = "Press 'q' to quit"; + // Status on the left and right + char* status_l = NULL; + char* status_r = NULL; + + // Fetch the latest stats + stats = fireperf_tui_get_stats(tui, 0); + if (!stats) + goto ERROR; + + // Print some help text on the right + r = asprintf(&status_r, "Press 'q' to quit"); + if (r < 0) + goto ERROR; + + // Print some connection information on the left + r = asprintf(&status_l, "%zu Open Connection(s), %zu Total Connection(s)", + stats->open_connections, stats->connections); + if (r < 0) + goto ERROR; + + size_t peak_bps = fireperf_tui_get_peak_bps(tui, max_x); + + // Print Peak Bandwidth + r = asprintf(&status_l, "%s, Peak Bandwidth: %s", status_l, + format_size(peak_bps, FIREPERF_FORMAT_BITS)); + if (r < 0) + goto ERROR; + + // Erase the previous content + werase(tui->status); // Write in black & white wattron(tui->status, COLOR_PAIR(4)); - // Push the help text to the right - for (int i = 0; i < COLS - strlen(help); i++) + // Fill everything with the background color + for (unsigned int i = 0; i < COLS; i++) waddch(tui->status, ' '); - // Write the help text - waddstr(tui->status, help); + // Write the text on the left + if (status_l) + mvwaddstr(tui->status, 0, 1, status_l); + + // Write the text on the right + if (status_r) + mvwaddstr(tui->status, 0, COLS - strlen(status_r) - 2, status_r); + + waddch(tui->status, ' '); // Reset the colour wattroff(tui->status, COLOR_PAIR(4)); + // Refresh the view + wrefresh(tui->status); + +ERROR: + if (status_l) + free(status_l); + if (status_r) + free(status_r); + + return 0; +} + +static int fireperf_tui_setup_status(struct fireperf_tui* tui) { + int r; + + // Create the window + tui->status = newwin(1, COLS, LINES - 1, 0); + if (!tui->status) + return 1; + + // Perform an initial drawing + r = fireperf_tui_update_status(tui); + if (r) + return r; + // Refresh wrefresh(tui->status); @@ -253,27 +335,6 @@ int fireperf_tui_action(struct fireperf_tui* tui) { return 0; } -static const struct fireperf_stats* fireperf_tui_get_stats(struct fireperf_tui* tui, int i) { - return &tui->stats[(tui->s - i) % MAX_STATS]; -} - -static size_t fireperf_tui_get_peak_bps(struct fireperf_tui* tui, int max) { - const struct fireperf_stats* stats = NULL; - size_t peak = 0; - - for (unsigned int i = 0; i < max; i++) { - stats = fireperf_tui_get_stats(tui, i); - - if (stats->bytes_sent > peak) - peak = stats->bytes_sent; - - if (stats->bytes_received > peak) - peak = stats->bytes_received; - } - - return peak; -} - /* Draws the big graph */ @@ -363,5 +424,10 @@ int fireperf_tui_update(struct fireperf_tui* tui, struct fireperf_stats stats) { tui->s %= MAX_STATS; // Draw the graph - return fireperf_tui_draw_graph(tui); + r = fireperf_tui_draw_graph(tui); + if (r) + return r; + + // Update the status + return fireperf_tui_update_status(tui); }