如何将 STDIN 保持在 C 终端的底部
How to keep STDIN at bottom the terminal in C
我正在用 c 编写一个简单的即时消息客户端。它目前运行良好,但是如果用户正在键入并在键入时收到一条消息,则该消息显示在文本之后,然后用户继续在下面的行上。它看起来像这样:
USER: I am trying to ty...FRIEND: Hello
pe a message. <--- (the end of the users message)
我的想法是:
以某种方式从 stdin 强制当前数据并将其加载到缓冲区中,然后在打印 FRIEND: 之前使用 \r 来擦除行中的内容,然后从缓冲区打印。有没有人有任何具体的例子来说明如何完成这个任务?
最后的结果应该是
FRIEND: Hello
USER: I am trying to type a message
用户开始输入消息,收到一条消息,stdin 行向下移动,然后用户完成了他们的消息。
注意:我是 运行 GNOME Terminal 3.6.2 在最新版本的 Linux Mint
通常的方法是使用 ncurses(任何形式的诅咒都可以),接受一个 window 中的输入并编写结果 另一个 。这是一个简短的例子:
#include <curses.h>
int
main(void)
{
bool done = FALSE;
WINDOW *input, *output;
char buffer[1024];
initscr();
cbreak();
echo();
input = newwin(1, COLS, LINES - 1, 0);
output = newwin(LINES - 1, COLS, 0, 0);
wmove(output, LINES - 2, 0); /* start at the bottom */
scrollok(output, TRUE);
while (!done) {
mvwprintw(input, 0, 0, "> ");
if (wgetnstr(input, buffer, COLS - 4) != OK) {
break;
}
werase(input);
waddch(output, '\n'); /* result from wgetnstr has no newline */
waddstr(output, buffer);
wrefresh(output);
done = (*buffer == 4); /* quit on control-D */
}
endwin();
return 0;
}
如果您想了解 VT100 控制代码(与 ECMA-48 不同),vt100.net 有一些终端的手册。
关于 link VT100 control codes: that is a source of misinformation, as noted in the ncurses FAQ How do I get color with VT100?
我正在用 c 编写一个简单的即时消息客户端。它目前运行良好,但是如果用户正在键入并在键入时收到一条消息,则该消息显示在文本之后,然后用户继续在下面的行上。它看起来像这样:
USER: I am trying to ty...FRIEND: Hello
pe a message. <--- (the end of the users message)
我的想法是:
以某种方式从 stdin 强制当前数据并将其加载到缓冲区中,然后在打印 FRIEND: 之前使用 \r 来擦除行中的内容,然后从缓冲区打印。有没有人有任何具体的例子来说明如何完成这个任务?
最后的结果应该是
FRIEND: Hello
USER: I am trying to type a message
用户开始输入消息,收到一条消息,stdin 行向下移动,然后用户完成了他们的消息。
注意:我是 运行 GNOME Terminal 3.6.2 在最新版本的 Linux Mint
通常的方法是使用 ncurses(任何形式的诅咒都可以),接受一个 window 中的输入并编写结果 另一个 。这是一个简短的例子:
#include <curses.h>
int
main(void)
{
bool done = FALSE;
WINDOW *input, *output;
char buffer[1024];
initscr();
cbreak();
echo();
input = newwin(1, COLS, LINES - 1, 0);
output = newwin(LINES - 1, COLS, 0, 0);
wmove(output, LINES - 2, 0); /* start at the bottom */
scrollok(output, TRUE);
while (!done) {
mvwprintw(input, 0, 0, "> ");
if (wgetnstr(input, buffer, COLS - 4) != OK) {
break;
}
werase(input);
waddch(output, '\n'); /* result from wgetnstr has no newline */
waddstr(output, buffer);
wrefresh(output);
done = (*buffer == 4); /* quit on control-D */
}
endwin();
return 0;
}
如果您想了解 VT100 控制代码(与 ECMA-48 不同),vt100.net 有一些终端的手册。
关于 link VT100 control codes: that is a source of misinformation, as noted in the ncurses FAQ How do I get color with VT100?