Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

+6 -34
+1 -1
scripts/kconfig/nconf.c
··· 953 953 current_instructions = instructions; 954 954 955 955 clear(); 956 - print_in_middle(stdscr, 1, 0, getmaxx(stdscr), 956 + print_in_middle(stdscr, 1, getmaxx(stdscr), 957 957 menu_backtitle, 958 958 attr_main_heading); 959 959
+4 -27
scripts/kconfig/nconf.gui.c
··· 117 117 } 118 118 119 119 /* 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); 146 124 } 147 125 148 126 int get_line_no(const char *text) ··· 555 577 text_cols, 0); 556 578 print_in_middle(win, 557 579 text_lines+2, 558 - 0, 559 580 text_cols, 560 581 "<OK>", 561 582 attr_dialog_menu_fore);
+1 -6
scripts/kconfig/nconf.h
··· 68 68 void set_colors(void); 69 69 70 70 /* 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); 77 72 int get_line_length(const char *line); 78 73 int get_line_no(const char *text); 79 74 const char *get_line(const char *text, int line_no);