Code

Refactor toggle_date_option into a generic enum_map based toggler
authorJonas Fonseca <jonas.fonseca@savoirfairelinux.com>
Fri, 11 Dec 2009 22:52:47 +0000 (17:52 -0500)
committerJonas Fonseca <jonas.fonseca@savoirfairelinux.com>
Fri, 11 Dec 2009 23:12:39 +0000 (18:12 -0500)
tig.c

diff --git a/tig.c b/tig.c
index 399567b07416ba63bb9a5bd4790f5fd0722c0cad..08b2993b7f8a664384c5f4812055162d85448682 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -2484,20 +2484,19 @@ redraw_display(bool clear)
 }
 
 static void
-toggle_date_option(enum date *date)
+toggle_enum_option_do(unsigned int *opt, const char *help,
+                     const struct enum_map *map, size_t size)
 {
-       static const char *help[] = {
-               "no",
-               "default",
-               "relative",
-               "short"
-       };
-
-       *date = (*date + 1) % ARRAY_SIZE(help);
+       *opt = (*opt + 1) % size;
        redraw_display(FALSE);
-       report("Displaying %s dates", help[*date]);
+       report("Displaying %s %s", enum_name(map[*opt]), help);
 }
 
+#define toggle_enum_option(opt, help, map) \
+       toggle_enum_option_do(opt, help, map, ARRAY_SIZE(map))
+
+#define toggle_date() toggle_enum_option(&opt_date, "dates", date_map)
+
 static void
 toggle_view_option(bool *option, const char *help)
 {
@@ -2521,7 +2520,7 @@ open_option_menu(void)
 
        if (prompt_menu("Toggle option", menu, &selected)) {
                if (menu[selected].data == &opt_date)
-                       toggle_date_option(menu[selected].data);
+                       toggle_date();
                else
                        toggle_view_option(menu[selected].data, menu[selected].text);
        }
@@ -3518,7 +3517,7 @@ view_driver(struct view *view, enum request request)
                break;
 
        case REQ_TOGGLE_DATE:
-               toggle_date_option(&opt_date);
+               toggle_date();
                break;
 
        case REQ_TOGGLE_AUTHOR: