Skip to content

Commit 0871874

Browse files
committed
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>
1 parent 0a94768 commit 0871874

3 files changed

Lines changed: 6 additions & 34 deletions

File tree

scripts/kconfig/nconf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ static void show_menu(const char *prompt, const char *instructions,
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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -117,32 +117,10 @@ void set_colors(void)
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,
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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,7 @@ typedef enum {
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);

0 commit comments

Comments
 (0)