nocbreak();
echo();
mvwprintw(game->prompt, 1, 0, "Move from? ");
wgetnstr(game->prompt, input, 4);
wgetch(game->prompt);
/* find first not empty character and set cursor on it */
wmove(game->prompt, 1, getmaxx(game->prompt) - 1);
while ((winch(game->prompt) & A_CHARTEXT) == ' ')
wmove(game->prompt, 1, getcurx(game->prompt) - 1);
wmove(game->prompt, 1, getcurx(game->prompt) + 2);
wprintw(game->prompt, "To? ");
wgetnstr(game->prompt, input, 4);
This is the only solution I have found. I wonder if there is easier way to discard the newline so I's possible to easily call multiple wgetnstr or any other IO function on the same line. I tried reading with getch but it didn't help.
In other words:
I want to read input on the same line like this Move from? <input1> To? <input2> without using hacks like the one above to mitigate the newline from <input1>. Just call the IO function after each other (I tried different modes like nonl(), nl() but it seems they don't affect the user input).
Here is how the default behavior looks for reference:
code
getstr(input)
printw("Hello")
behavior
my input here
Hello
expected behavior
my input hereHello
edit: here is my current solution as suggested by @n.m.couldbeanAI
int
wread(WINDOW *win, char *input, int maxlen)
{
int ch;
char *beg = input;
doupdate();
curs_set(1);
noecho();
cbreak();
keypad(win, TRUE);
while ((ch = wgetch(win)) != '\n') {
switch (ch) {
case KEY_BACKSPACE:
if (input - beg <= 0) continue;
wmove(win, getcury(win), getcurx(win) - 1);
wdelch(win);
input--;
break;
default:
if (input - beg >= maxlen) continue;
waddch(win, ch);
*(input++) = ch;
break;
}
}
curs_set(0);
return input - beg;
}