History

kconfig: nconf: refactor in print_in_middle()

This helper is the same as the sample code in the NCURSES HOWTO [1], but it is over-engineering to be used for nconf. I do not see any good reason to use the 'float' type just for the division by 2. All the call-sites pass a non-NULL pointer to the first argument, so 'if (win == NULL) win = stdscr;' is dead code. 'if (startx != 0) x = startx;' is dead code because 'x' will be overridden some lines below, by 'x = startx + (int)temp;'. All the call-sites pass a non-zero value to the second argument, so 'if (starty != 0)' is always true. getyx(win, y, x) is also dead-code because both 'y' and 'x' are overridden. All the call-sites pass 0 to the third parameter, so 'startx' can be removed. All the call-sites pass a non-zero value to the fourth parameter, so 'if (width == 0) width = 80;' is dead code. The window will be refreshed later, so there is no need to call refresh() in this function. Change the type of the last parameter from 'chtype' to 'int' to be aligned with the prototype, 'int wattrset(WINDOW *win, int attrs);' I also slightly cleaned up the indentation style. [1]: https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/color.html

Masahiro Yamada committed 5 years ago

Commit 08718741 parent 0a947683 files · +6 −34Browse files

Showing the changes to scripts (3 of 3 files). Show all

Unified · Split

3 files
  1. scripts/kconfig/nconf.c +1 −1
  2. scripts/kconfig/nconf.gui.c +4 −27
  3. scripts/kconfig/nconf.h +1 −6
scripts/kconfig/nconf.c+1−1
@@ -953,7 +953,7 @@ static void show_menu(const char *prompt, const char *instructions, · 952 unmodified lines
953953 current_instructions = instructions;
954954
955955 clear();
956− print_in_middle(stdscr, 1, 0, getmaxx(stdscr),
956+ print_in_middle(stdscr, 1, getmaxx(stdscr),
957957 menu_backtitle,
958958 attr_main_heading);
959959
scripts/kconfig/nconf.gui.c+4−27
@@ -117,32 +117,10 @@ void set_colors(void) · 116 unmodified lines
117117 }
118118
119119 /* this changes the windows attributes !!! */
120−void print_in_middle(WINDOW *win,
121− int starty,
122− int startx,
123− int width,
124− const char *string,
125− chtype color)
126−{ int length, x, y;
127− float temp;
128−
129−
130− if (win == NULL)
131− win = stdscr;
132− getyx(win, y, x);
133− if (startx != 0)
134− x = startx;
135− if (starty != 0)
136− y = starty;
137− if (width == 0)
138− width = 80;
139−
140− length = strlen(string);
141− temp = (width - length) / 2;
142− x = startx + (int)temp;
143− wattrset(win, color);
144− mvwprintw(win, y, x, "%s", string);
145− refresh();
120+void print_in_middle(WINDOW *win, int y, int width, const char *str, int attrs)
121+{
122+ wattrset(win, attrs);
123+ mvwprintw(win, y, (width - strlen(str)) / 2, "%s", str);
146124 }
147125
148126 int get_line_no(const char *text)
@@ -577,7 +555,6 @@ void show_scroll_win(WINDOW *main_window, · 428 unmodified lines
577555 text_cols, 0);
578556 print_in_middle(win,
579557 text_lines+2,
580− 0,
581558 text_cols,
582559 "<OK>",
583560 attr_dialog_menu_fore);
scripts/kconfig/nconf.h+1−6
@@ -68,12 +68,7 @@ typedef enum { · 67 unmodified lines
6868 void set_colors(void);
6969
7070 /* this changes the windows attributes !!! */
71−void print_in_middle(WINDOW *win,
72− int starty,
73− int startx,
74− int width,
75− const char *string,
76− chtype color);
71+void print_in_middle(WINDOW *win, int y, int width, const char *str, int attrs);
7772 int get_line_length(const char *line);
7873 int get_line_no(const char *text);
7974 const char *get_line(const char *text, int line_no);