]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
cfdisk: consolidate menu move code
authorKarel Zak <kzak@redhat.com>
Mon, 24 Feb 2014 13:27:49 +0000 (14:27 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 11 Mar 2014 10:35:14 +0000 (11:35 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/cfdisk.c

index b5029eef144ed91c36465a38e97c832a594f2892..73c08e17ff1400d83ae59a4476f4678587f7d007 100644 (file)
@@ -80,7 +80,8 @@ static void ui_warnx(const char *fmt, ...);
 static void ui_warn(const char *fmt, ...);
 static void ui_info(const char *fmt, ...);
 static void ui_draw_menu(struct cfdisk *cf);
-static void ui_menu_goto(struct cfdisk *cf, int where);
+static int ui_menu_move(struct cfdisk *cf, int key);
+
 static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
                       uintmax_t low, uintmax_t up);
 
@@ -380,17 +381,12 @@ static int ask_menu(struct fdisk_ask *ask, struct cfdisk *cf)
 
        /* wait for keys */
        do {
-               switch (getch()) {
-               case KEY_LEFT:
-#ifdef KEY_BTAB
-               case KEY_BTAB:
-#endif
-                       ui_menu_goto(cf, cf->menu_idx - 1);
-                       break;
-               case KEY_RIGHT:
-               case '\t':
-                       ui_menu_goto(cf, cf->menu_idx + 1);
-                       break;
+               int key = getch();
+
+               if (ui_menu_move(cf, key) == 0)
+                       continue;
+
+               switch (key) {
                case KEY_ENTER:
                case '\n':
                case '\r':
@@ -438,7 +434,6 @@ static int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask,
        return rc;
 }
 
-
 static int ui_end(struct cfdisk *cf)
 {
        if (cf && !ui_enabled)
@@ -984,6 +979,49 @@ static void ui_menu_goto(struct cfdisk *cf, int where)
        ui_draw_menuitem(cf, d, where);
 }
 
+static int ui_menu_move(struct cfdisk *cf, int key)
+{
+       assert(cf);
+       assert(cf->menu);
+
+       if (cf->menu->vertical)
+       {
+               switch (key) {
+               case KEY_DOWN:
+               case '\016':    /* ^N */
+               case 'j':       /* Vi-like alternative */
+                       ui_menu_goto(cf, cf->menu_idx + 1);
+                       return 0;
+               case KEY_UP:
+               case '\020':    /* ^P */
+               case 'k':       /* Vi-like alternative */
+                       ui_menu_goto(cf, cf->menu_idx - 1);
+                       return 0;
+               case KEY_HOME:
+                       ui_menu_goto(cf, 0);
+                       return 0;
+               case KEY_END:
+                       ui_menu_goto(cf, cf->menu->nitems);
+                       return 0;
+               }
+       } else {
+               switch (key) {
+               case KEY_RIGHT:
+               case '\t':
+                       ui_menu_goto(cf, cf->menu_idx + 1);
+                       return 0;
+               case KEY_LEFT:
+#ifdef KEY_BTAB
+               case KEY_BTAB:
+#endif
+                       ui_menu_goto(cf, cf->menu_idx - 1);
+                       return 0;
+               }
+       }
+
+       return 1;       /* key irrelevant for menu move */
+}
+
 /* returns: error: < 0, success: 0, quit: 1 */
 static int ui_menu_action(struct cfdisk *cf, int key)
 {
@@ -1292,7 +1330,6 @@ static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
        return rc;
 }
 
-
 static int ui_run(struct cfdisk *cf)
 {
        int rc;
@@ -1308,6 +1345,9 @@ static int ui_run(struct cfdisk *cf)
        do {
                int rc = 0, key = getch();
 
+               if (ui_menu_move(cf, key) == 0)
+                       continue;
+
                switch (key) {
                case KEY_DOWN:
                case '\016':    /* ^N */
@@ -1327,16 +1367,6 @@ static int ui_run(struct cfdisk *cf)
                        break;
                        ui_menu_action(cf, 0);
                        break;
-               case KEY_LEFT:
-#ifdef KEY_BTAB
-               case KEY_BTAB:
-#endif
-                       ui_menu_goto(cf, cf->menu_idx - 1);
-                       break;
-               case KEY_RIGHT:
-               case '\t':
-                       ui_menu_goto(cf, cf->menu_idx + 1);
-                       break;
                case KEY_ENTER:
                case '\n':
                case '\r':