scripts/kconfig/nconf: fix editing long strings

The original dialog_inputbox doesn't work with longer than prompt_width
strings, here fixed it in this way:
1) add variable cursor_form_win to record cursor of form_win,
   keep its value always between [0, prompt_width-1];
   reuse the original cursor_position as cursor of the string result,
   use (cursor_position-cursor_form_win) as begin offset to show part of
   the string in form_win;

Signed-off-by: Cheng Renquan <crquan@gmail.com>
Cc: Arnaud Lacombe <lacombar@gmail.com>
Cc: Nir Tzachar <nir.tzachar@gmail.com>
This commit is contained in:
Cheng Renquan 2011-09-01 10:52:21 -07:00 committed by Michal Marek
parent 5ea9f64ffc
commit e631a57a19
1 changed files with 23 additions and 6 deletions

View File

@ -367,6 +367,7 @@ int dialog_inputbox(WINDOW *main_window,
int i, x, y; int i, x, y;
int res = -1; int res = -1;
int cursor_position = strlen(init); int cursor_position = strlen(init);
int cursor_form_win;
char *result = *resultp; char *result = *resultp;
if (strlen(init)+1 > *result_len) { if (strlen(init)+1 > *result_len) {
@ -410,7 +411,9 @@ int dialog_inputbox(WINDOW *main_window,
fill_window(prompt_win, prompt); fill_window(prompt_win, prompt);
mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); mvwprintw(form_win, 0, 0, "%*s", prompt_width, " ");
mvwprintw(form_win, 0, 0, "%s", result); cursor_form_win = min(cursor_position, prompt_width-1);
mvwprintw(form_win, 0, 0, "%s",
result + cursor_position-cursor_form_win);
/* create panels */ /* create panels */
panel = new_panel(win); panel = new_panel(win);
@ -436,6 +439,8 @@ int dialog_inputbox(WINDOW *main_window,
&result[cursor_position], &result[cursor_position],
len-cursor_position+1); len-cursor_position+1);
cursor_position--; cursor_position--;
cursor_form_win--;
len--;
} }
break; break;
case KEY_DC: case KEY_DC:
@ -443,18 +448,22 @@ int dialog_inputbox(WINDOW *main_window,
memmove(&result[cursor_position], memmove(&result[cursor_position],
&result[cursor_position+1], &result[cursor_position+1],
len-cursor_position+1); len-cursor_position+1);
len--;
} }
break; break;
case KEY_UP: case KEY_UP:
case KEY_RIGHT: case KEY_RIGHT:
if (cursor_position < len && if (cursor_position < len) {
cursor_position < min(*result_len, prompt_width))
cursor_position++; cursor_position++;
cursor_form_win++;
}
break; break;
case KEY_DOWN: case KEY_DOWN:
case KEY_LEFT: case KEY_LEFT:
if (cursor_position > 0) if (cursor_position > 0) {
cursor_position--; cursor_position--;
cursor_form_win--;
}
break; break;
default: default:
if ((isgraph(res) || isspace(res))) { if ((isgraph(res) || isspace(res))) {
@ -470,16 +479,24 @@ int dialog_inputbox(WINDOW *main_window,
len-cursor_position+1); len-cursor_position+1);
result[cursor_position] = res; result[cursor_position] = res;
cursor_position++; cursor_position++;
cursor_form_win++;
len++;
} else { } else {
mvprintw(0, 0, "unknown key: %d\n", res); mvprintw(0, 0, "unknown key: %d\n", res);
} }
break; break;
} }
if (cursor_form_win < 0)
cursor_form_win = 0;
else if (cursor_form_win > prompt_width-1)
cursor_form_win = prompt_width-1;
wmove(form_win, 0, 0); wmove(form_win, 0, 0);
wclrtoeol(form_win); wclrtoeol(form_win);
mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); mvwprintw(form_win, 0, 0, "%*s", prompt_width, " ");
mvwprintw(form_win, 0, 0, "%s", result); mvwprintw(form_win, 0, 0, "%s",
wmove(form_win, 0, cursor_position); result + cursor_position-cursor_form_win);
wmove(form_win, 0, cursor_form_win);
touchwin(win); touchwin(win);
refresh_all_windows(main_window); refresh_all_windows(main_window);