enabling colors for bmon

This commit enables colors in bmon. It adds configuration options to
let the user decide, which colors should be used. Therefor the graph_rx and graph_tx
layouts are introduced and applied respectively.

* Add graph_rx, graph_tx and layout_cfg fields.
* Setting default colors if colorized output is enabled.
* draw_table accept additional layout parameter and apply the given layout for
  the table graph.
* Apply layouts for rx, tx graph and header and statusbar.
* Add include/layout.h, which provides functions to parse, color and attribute
  strings and set the layout.
* Adding a default layout-config example in examples/bmon.conf.

[Edit: Based on original work by eri!, #PR23]
This commit is contained in:
blastmaster
2016-07-16 01:53:42 +02:00
committed by Thomas Graf
parent 1c25eacc95
commit 828b5b5368
5 changed files with 253 additions and 18 deletions

View File

@ -30,6 +30,7 @@
#include <bmon/element.h>
#include <bmon/element_cfg.h>
#include <bmon/history.h>
#include <bmon/layout.h>
#include <bmon/utils.h>
cfg_t *cfg;
@ -69,6 +70,16 @@ static cfg_opt_t unit_opts[] = {
CFG_END()
};
static cfg_opt_t color_opts[] = {
CFG_STR_LIST("color_pair", "", CFGF_NONE),
CFG_END()
};
static cfg_opt_t layout_opts[] = {
CFG_SEC("color", color_opts, CFGF_MULTI | CFGF_TITLE),
CFG_END()
};
static cfg_opt_t global_opts[] = {
CFG_FLOAT("read_interval", 1.0f, CFGF_NONE),
CFG_FLOAT("rate_interval", 1.0f, CFGF_NONE),
@ -87,6 +98,7 @@ static cfg_opt_t global_opts[] = {
CFG_SEC("attr", attr_opts, CFGF_MULTI | CFGF_TITLE),
CFG_SEC("history", history_opts, CFGF_MULTI | CFGF_TITLE),
CFG_SEC("element", element_opts, CFGF_MULTI | CFGF_TITLE),
CFG_SEC("layout", layout_opts, CFGF_MULTI | CFGF_TITLE),
CFG_END()
};
@ -103,22 +115,26 @@ static char * configfile = NULL;
#if defined HAVE_USE_DEFAULT_COLORS
struct layout cfg_layout[] =
{
{-1, -1, 0}, /* dummy, not used */
{-1, -1, 0}, /* default */
{-1, -1, A_REVERSE}, /* statusbar */
{-1, -1, 0}, /* header */
{-1, -1, 0}, /* list */
{-1, -1, A_REVERSE}, /* selected */
{-1, -1, 0}, /* dummy, not used */
{-1, -1, 0}, /* default */
{-1, -1, A_REVERSE}, /* statusbar */
{-1, -1, 0}, /* header */
{-1, -1, 0}, /* list */
{-1, -1, A_REVERSE}, /* selected */
{-1, -1, 0}, /* RX graph */
{-1, -1, 0}, /* TX graph */
};
#else
struct layout cfg_layout[] =
{
{0, 0, 0}, /* dummy, not used */
{COLOR_BLACK, COLOR_WHITE, 0}, /* default */
{COLOR_BLACK, COLOR_WHITE, A_REVERSE}, /* statusbar */
{COLOR_BLACK, COLOR_WHITE, 0}, /* header */
{COLOR_BLACK, COLOR_WHITE, 0}, /* list */
{COLOR_BLACK, COLOR_WHITE, A_REVERSE}, /* selected */
{0, 0, 0}, /* dummy, not used */
{COLOR_WHITE, COLOR_BLACK, 0}, /* default */
{COLOR_BLUE, COLOR_GREEN, A_REVERSE}, /* statusbar */
{COLOR_GREEN, COLOR_BLACK, 0}, /* header */
{COLOR_WHITE, COLOR_BLACK, 0}, /* list */
{COLOR_YELLOW, COLOR_BLACK, A_REVERSE}, /* selected */
{COLOR_GREEN, COLOR_BLACK, 0}, /* RX graph */
{COLOR_RED, COLOR_BLACK, 0}, /* TX graph */
};
#endif
#endif
@ -423,6 +439,41 @@ static void configfile_read_attrs(void)
}
}
static void configfile_read_layout_cfg(void)
{
int i, nlayouts;
cfg_t *lout;
nlayouts = cfg_size(cfg, "layout");
for (i = 0; i < nlayouts; i++)
{
int c, ncolors;
const char *name;
if (!(lout = cfg_getnsec(cfg, "layout", i)))
BUG();
if (!(name = cfg_title(lout)))
BUG();
ncolors = cfg_size(lout, "color");
if (ncolors > LAYOUT_MAX) {
fprintf(stderr, "Warning excceeded maximum number of layouts\n");
ncolors = LAYOUT_MAX;
}
for (c = 0; c < ncolors; c++) {
cfg_t *color_pair;
if (!(color_pair = cfg_getnsec(lout, "color", c)))
BUG();
if (!(name = cfg_title(color_pair)))
BUG();
add_layout(name, color_pair);
}
}
}
static void conf_read(const char *path, int must)
{
int err;
@ -450,6 +501,7 @@ static void conf_read(const char *path, int must)
configfile_read_history();
configfile_read_attrs();
configfile_read_element_cfg();
configfile_read_layout_cfg();
}
static const char default_config[] = \
@ -508,6 +560,29 @@ static const char default_config[] = \
"history day {" \
" interval = 86400.0" \
" size = 60" \
"}"
"layout colors {" \
" color default {" \
" color_pair = { \"white\", \"black\" }" \
" }" \
" color statusbar{" \
" color_pair = { \"blue\", \"white\", \"reverse\" }" \
" }" \
" color header {" \
" color_pair = { \"yellow\", \"black\" }" \
" }" \
" color list {" \
" color_pair = { \"white\", \"black\" }" \
" }" \
" color selected {" \
" color_pair = { \"yellow\", \"black\", \"reverse\" }" \
" }" \
" color rx_graph {" \
" color_pair = { \"green\", \"black\" }" \
" }" \
" color tx_graph {" \
" color_pair = { \"red\", \"black\" }" \
" }" \
"}";
static void conf_read_default(void)
@ -524,6 +599,7 @@ static void conf_read_default(void)
configfile_read_history();
configfile_read_attrs();
configfile_read_element_cfg();
configfile_read_layout_cfg();
}
void configfile_read(void)

View File

@ -420,6 +420,7 @@ static void draw_header(void)
move(row, COLS - strlen(PACKAGE_STRING) - 1);
put_line("%s", PACKAGE_STRING);
move(row, 0);
apply_layout(LAYOUT_LIST);
}
static int lines_required_for_statusbar(void)
@ -632,6 +633,7 @@ static void draw_element(struct element_group *g, struct element *e,
static void draw_group(struct element_group *g, void *arg)
{
apply_layout(LAYOUT_HEADER);
int *line = arg;
if (line_visible(*line)) {
@ -684,7 +686,7 @@ static void draw_graph_centered(struct graph *g, int row, int ncol,
static void draw_table(struct graph *g, struct graph_table *tbl,
struct attr *a, struct history *h,
const char *hdr, int ncol)
const char *hdr, int ncol, int layout)
{
int i, save_row;
char buf[32];
@ -709,11 +711,14 @@ static void draw_table(struct graph *g, struct graph_table *tbl,
//move(row, ncol + g->g_cfg.gc_width - 3);
//put_line("[err %.2f%%]", rtiming.rt_variance.v_error);
memset(buf, 0, strlen(buf));
for (i = (g->g_cfg.gc_height - 1); i >= 0; i--) {
move(++row, ncol);
put_line("%'8.2f %s",
tbl->gt_scale[i],
tbl->gt_table + (i * graph_row_size(&g->g_cfg)));
sprintf(buf, "%'8.2f ", tbl->gt_scale[i]);
addstr(buf);
apply_layout(layout);
put_line("%s", tbl->gt_table + (i * graph_row_size(&g->g_cfg)));
apply_layout(LAYOUT_LIST);
}
move(++row, ncol);
@ -747,14 +752,14 @@ static void draw_history_graph(struct attr *a, struct history *h)
graph_refill(g, h);
save_row = row;
draw_table(g, &g->g_rx, a, h, "RX", ncol);
draw_table(g, &g->g_rx, a, h, "RX", ncol, LAYOUT_RX_GRAPH);
if (graph_display == GRAPH_DISPLAY_SIDE_BY_SIDE) {
ncol = cols / 2;
row = save_row;
}
draw_table(g, &g->g_tx, a, h, "TX", ncol);
draw_table(g, &g->g_tx, a, h, "TX", ncol, LAYOUT_TX_GRAPH);
graph_free(g);
}