【发布时间】:2013-03-13 23:54:25
【问题描述】:
我正在尝试仅使用 readline 和 ANSI 转义码编写控制台聊天客户端。
我的目标是让终端处理聊天记录的回滚和滚动,同时始终在消息后提供 readline 提示以供新输入。
我已经用我的两个线程尝试了以下操作。我的控制台输入线程:
printf("\x1B[s"); // Save cursor position
message = readline("Prompt > ");
我的消息接收线程确实如此:
message = receive_message(); // Blocks for next message
printf("\x1B[u"); // Restore cursor to before the prompt
printf("\x1B[J"); // Erase readline prompt and input (down to bottom of screen)
printf("%s\n", message); // Print the message (where readline was)
printf("\x1B[s"); // Save new cursor position
rl_forced_update_display(); // Restore readline
只要 readline 输入不换行,上述方法就有效。当它包裹恢复保存的光标位置时并没有按预期工作,它似乎只恢复了水平位置,而不是垂直位置。
即使输入行换行,我如何调整上述代码工作?
【问题讨论】:
-
请参阅this question 以获得对所需行为的更好描述。但是,当用户输入包含多行时,该问题并未涵盖或解决我的具体问题。
标签: c readline ansi-escape