]> granicus.if.org Git - nethack/commitdiff
Minor code reorg: finding longest option name
authorPasi Kallinen <paxed@alt.org>
Wed, 16 Jun 2021 11:53:05 +0000 (14:53 +0300)
committerPasi Kallinen <paxed@alt.org>
Wed, 16 Jun 2021 11:53:05 +0000 (14:53 +0300)
src/options.c

index f18433db03a4335cc108f630e4a1499fd5e71dd2..587455ddd7493d7775f92ddc37008ff56609c733 100644 (file)
@@ -261,6 +261,7 @@ static void free_one_menu_coloring(int);
 static int count_menucolors(void);
 static boolean parse_role_opts(int, boolean, const char *,
                                char *, char **);
+static unsigned int longest_option_name(int, int);
 static void doset_add_menu(winid, const char *, int, int);
 static int handle_add_list_remove(const char *, int);
 static void remove_autopickup_exception(struct autopickup_exception *);
@@ -7190,6 +7191,32 @@ get_option_value(const char *optname)
     return (char *) 0;
 }
 
+static unsigned int
+longest_option_name(int startpass, int endpass)
+{
+    /* spin through the options to find the longest name */
+    unsigned longest_name_len = 0;
+    int i, pass, optflags;
+    const char *name;
+
+    for (pass = 0; pass < 2; pass++)
+        for (i = 0; (name = allopt[i].name) != 0; i++) {
+            if (pass == 0 &&
+                (allopt[i].opttyp != BoolOpt || !allopt[i].addr))
+                continue;
+            optflags = allopt[i].setwhere;
+            if (optflags < startpass || optflags > endpass)
+                continue;
+            if ((is_wc_option(name) && !wc_supported(name))
+                || (is_wc2_option(name) && !wc2_supported(name)))
+                continue;
+
+            if (strlen(name) > longest_name_len)
+                longest_name_len = strlen(name);
+        }
+    return longest_name_len;
+}
+
 /* the 'O' command */
 int
 doset(void) /* changing options via menu by Per Liboriussen */
@@ -7202,9 +7229,8 @@ doset(void) /* changing options via menu by Per Liboriussen */
     winid tmpwin;
     anything any;
     menu_item *pick_list;
-    int indexoffset, startpass, endpass, optflags;
+    int indexoffset, startpass, endpass;
     boolean setinitial = FALSE, fromfile = FALSE;
-    unsigned longest_name_len;
 
     tmpwin = create_nhwindow(NHW_MENU);
     start_menu(tmpwin, MENU_BEHAVE_STANDARD);
@@ -7220,25 +7246,8 @@ doset(void) /* changing options via menu by Per Liboriussen */
     endpass = (wizard) ? set_wizonly : set_in_game;
 
     if (!made_fmtstr && !iflags.menu_tab_sep) {
-        /* spin through the options to find the longest name
-           and adjust the format string accordingly */
-        longest_name_len = 0;
-        for (pass = 0; pass < 2; pass++)
-            for (i = 0; (name = allopt[i].name) != 0; i++) {
-                if (pass == 0 &&
-                    (allopt[i].opttyp != BoolOpt || !allopt[i].addr))
-                    continue;
-                optflags = allopt[i].setwhere;
-                if (optflags < startpass || optflags > endpass)
-                    continue;
-                if ((is_wc_option(name) && !wc_supported(name))
-                    || (is_wc2_option(name) && !wc2_supported(name)))
-                    continue;
-
-                if (strlen(name) > longest_name_len)
-                    longest_name_len = strlen(name);
-            }
-        Sprintf(fmtstr_doset, "%%s%%-%us [%%s]", longest_name_len);
+        Sprintf(fmtstr_doset, "%%s%%-%us [%%s]",
+                longest_option_name(startpass, endpass));
         made_fmtstr = TRUE;
     }