diff options
author | Baptiste Jonglez <baptiste--git@jonglez.org> | 2012-05-14 12:38:19 +0200 |
---|---|---|
committer | Lukas Fleischer <calcurse@cryptocrack.de> | 2012-05-14 19:31:12 +0200 |
commit | f5dd276edbeb65d787fcfd30a0d07bbc2660813c (patch) | |
tree | 1fed95a92495e3bd7b3d42ad616b9cd3aa810fbf | |
parent | 6da787a5cc94d1b20d0730c18905ff3b93201874 (diff) | |
download | calcurse-f5dd276edbeb65d787fcfd30a0d07bbc2660813c.tar.gz calcurse-f5dd276edbeb65d787fcfd30a0d07bbc2660813c.zip |
src/utils.c: Add a status_ask_choice() function
This function allows the user to choose between various alternatives,
each one being associated to a given key.
This will allow a great deal of factorisation, which will make it
easier to handle special events (like resizing, user escape...) in an
uniform manner.
The cool part of the approach taken here is that it allows full i18n
(i.e. the key bound to an alternative can be different depending on
the language), at the expense of a somewhat less readable code on the
caller side.
Signed-off-by: Baptiste Jonglez <baptiste--git@jonglez.org>
Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
-rw-r--r-- | src/calcurse.h | 1 | ||||
-rw-r--r-- | src/utils.c | 42 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/calcurse.h b/src/calcurse.h index f2e6991..4b54549 100644 --- a/src/calcurse.h +++ b/src/calcurse.h @@ -896,6 +896,7 @@ void fatalbox (const char *); void warnbox (const char *); void status_mesg (const char *, const char *); void status_mesg_yesno (const char *); +int status_ask_choice (const char *, const char[], int); void erase_window_part (WINDOW *, int, int, int, int); WINDOW *popup (int, int, int, int, const char *, const char *, int); void print_in_middle (WINDOW *, int, int, int, const char *); diff --git a/src/utils.c b/src/utils.c index 8ddbe59..92c9d2b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -191,6 +191,48 @@ status_mesg_yesno (const char *msg) status_mesg (msg, "[y/n] "); } +/* + * Prompts the user to make a choice between named alternatives. + * + * The available choices are described by a string of the form + * "[ynp]". The first and last char are ignored (they are only here to + * make the translators' life easier), and every other char indicates + * a key the user is allowed to press. + * + * Returns the index of the key pressed by the user (starting from 1), + * or -1 if the user doesn't want to answer (e.g. by escaping). + */ +int +status_ask_choice(const char *message, const char choice[], int nb_choice) +{ + int i, ch; + char tmp[BUFSIZ]; + /* "[4/2/f/t/w/.../Z] " */ + char avail_choice[2 * nb_choice + 3]; + + avail_choice[0] = '['; + avail_choice[1] = '\0'; + + for (i = 1; i <= nb_choice; i++) + { + sprintf (tmp, (i == nb_choice) ? "%c] " : "%c/", choice[i]); + strcat (avail_choice, tmp); + } + + status_mesg (message, avail_choice); + + for (;;) + { + ch = wgetch (win[STA].p); + for (i = 1; i <= nb_choice; i++) + if (ch == choice[i]) + return i; + if (ch == ESCAPE) + return (-1); + /* TODO: handle resize events. */ + } +} + /* Erase part of a window. */ void erase_window_part (WINDOW *win, int first_col, int first_row, int last_col, |