Commit 8106bbe1 authored by Dridi Boukelmoune's avatar Dridi Boukelmoune

Use the varnishstat bindings table for key presses

This has the unfortunate effect of splitting a single switch statement
into two, however the second one being based on an enum should yield a
warning if in the future a new binding is added but not handled.

For example a binding involving the <h> key?
parent 8099ffa7
...@@ -9,6 +9,7 @@ bin_PROGRAMS = varnishstat ...@@ -9,6 +9,7 @@ bin_PROGRAMS = varnishstat
varnishstat_SOURCES = \ varnishstat_SOURCES = \
varnishstat.h \ varnishstat.h \
varnishstat.c \ varnishstat.c \
varnishstat_bindings.h \
varnishstat_curses.c \ varnishstat_curses.c \
varnishstat_options.h varnishstat_options.h
......
...@@ -62,7 +62,11 @@ ...@@ -62,7 +62,11 @@
#define VALUE_MAX 999999999999 #define VALUE_MAX 999999999999
#define CTRL(c) ((c) & 037) enum kb_e {
#define BINDING(name, desc) KB_ ## name,
#define BINDING_SIG
#include "varnishstat_bindings.h"
};
struct ma { struct ma {
unsigned n, nmax; unsigned n, nmax;
...@@ -869,56 +873,62 @@ draw_screen(void) ...@@ -869,56 +873,62 @@ draw_screen(void)
static void static void
handle_keypress(int ch) handle_keypress(int ch)
{ {
enum kb_e kb;
switch (ch) { switch (ch) {
case KEY_UP: #define BINDING_KEY(chr, name, or) \
case 'k': case chr:
#define BINDING(name, desc) \
kb = KB_ ## name; \
break;
#define BINDING_SIG
#include "varnishstat_bindings.h"
default:
return;
}
switch (kb) {
case KB_UP:
if (current == 0) if (current == 0)
return; return;
current--; current--;
break; break;
case KEY_DOWN: case KB_DOWN:
case 'j':
if (current == n_ptarray - 1) if (current == n_ptarray - 1)
return; return;
current++; current++;
break; break;
case KEY_PPAGE: case KB_PAGEUP:
case CTRL('b'):
case 'b':
current -= l_points; current -= l_points;
page_start -= l_points; page_start -= l_points;
break; break;
case KEY_NPAGE: case KB_PAGEDOWN:
case CTRL('f'):
case ' ':
current += l_points; current += l_points;
if (page_start + l_points < n_ptarray - 1) if (page_start + l_points < n_ptarray - 1)
page_start += l_points; page_start += l_points;
break; break;
case KEY_HOME: case KB_TOP:
case 'g':
current = 0; current = 0;
break; break;
case KEY_END: case KB_BOTTOM:
case 'G':
current = n_ptarray - 1; current = n_ptarray - 1;
break; break;
case 'd': case KB_UNSEEN:
hide_unseen = 1 - hide_unseen; hide_unseen = 1 - hide_unseen;
rebuild = 1; rebuild = 1;
break; break;
case 'e': case KB_SCALE:
scale = 1 - scale; scale = 1 - scale;
rebuild = 1; rebuild = 1;
break; break;
case '+': case KB_ACCEL:
interval += 0.1; interval += 0.1;
(void)snprintf(notification_message, NOTIF_MAXLEN, (void)snprintf(notification_message, NOTIF_MAXLEN,
"Refresh interval set to %.1f seconds.", interval); "Refresh interval set to %.1f seconds.", interval);
notification_eol = VTIM_mono() + 1.25; notification_eol = VTIM_mono() + 1.25;
break; break;
case '-': case KB_DECEL:
interval -= 0.1; interval -= 0.1;
if (interval < 0.1) if (interval < 0.1)
interval = 0.1; interval = 0.1;
...@@ -927,28 +937,28 @@ handle_keypress(int ch) ...@@ -927,28 +937,28 @@ handle_keypress(int ch)
notification_eol = VTIM_mono() + 1.25; notification_eol = VTIM_mono() + 1.25;
break; break;
case 'v': case KB_VERBOSE:
verbosity = VSC_ChangeLevel(verbosity, 1); verbosity = VSC_ChangeLevel(verbosity, 1);
rebuild = 1; rebuild = 1;
break; break;
case 'V': case KB_QUIET:
verbosity = VSC_ChangeLevel(verbosity, -1); verbosity = VSC_ChangeLevel(verbosity, -1);
rebuild = 1; rebuild = 1;
break; break;
case 'q': case KB_QUIT:
keep_running = 0; keep_running = 0;
return; return;
case CTRL('c'): case KB_SIG_INT:
AZ(raise(SIGINT)); AZ(raise(SIGINT));
return; return;
case CTRL('t'): case KB_SAMPLE:
sample = 1; sample = 1;
return; return;
case CTRL('z'): case KB_SIG_TSTP:
AZ(raise(SIGTSTP)); AZ(raise(SIGTSTP));
return; return;
default: default:
return; WRONG("unhandled key binding");
} }
update_position(); update_position();
......
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