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
953 current_instructions = instructions;953 current_instructions = instructions;
954 954
955 clear();955 clear();
956 print_in_middle(stdscr, 1, 0, getmaxx(stdscr),956 print_in_middle(stdscr, 1, getmaxx(stdscr),
957 menu_backtitle,957 menu_backtitle,
958 attr_main_heading);958 attr_main_heading);
959 959
scripts/kconfig/nconf.gui.c+4−27
@@ -117,32 +117,10 @@ void set_colors(void) · 116 unmodified lines
117}117}
118 118
119/* this changes the windows attributes !!! */119/* this changes the windows attributes !!! */
120void print_in_middle(WINDOW *win,120void print_in_middle(WINDOW *win, int y, int width, const char *str, int attrs)
121 int starty,121{
122 int startx,122 wattrset(win, attrs);
123 int width,123 mvwprintw(win, y, (width - strlen(str)) / 2, "%s", str);
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();
146}124}
147 125
148int get_line_no(const char *text)126int get_line_no(const char *text)
@@ -577,7 +555,6 @@ void show_scroll_win(WINDOW *main_window, · 428 unmodified lines
577 text_cols, 0);555 text_cols, 0);
578 print_in_middle(win,556 print_in_middle(win,
579 text_lines+2,557 text_lines+2,
580 0,
581 text_cols,558 text_cols,
582 "<OK>",559 "<OK>",
583 attr_dialog_menu_fore);560 attr_dialog_menu_fore);
scripts/kconfig/nconf.h+1−6
@@ -68,12 +68,7 @@ typedef enum { · 67 unmodified lines
68void set_colors(void);68void set_colors(void);
69 69
70/* this changes the windows attributes !!! */70/* this changes the windows attributes !!! */
71void print_in_middle(WINDOW *win,71void print_in_middle(WINDOW *win, int y, int width, const char *str, int attrs);
72 int starty,
73 int startx,
74 int width,
75 const char *string,
76 chtype color);
77int get_line_length(const char *line);72int get_line_length(const char *line);
78int get_line_no(const char *text);73int get_line_no(const char *text);
79const char *get_line(const char *text, int line_no);74const char *get_line(const char *text, int line_no);