简体   繁体   English

删除当前终端输入行

[英]Delete current terminal input line

I am trying to code a little console chat-program in C on linux.我正在尝试在 linux 上用 C 编写一个小的控制台聊天程序。

So far I coded it in a way that both chatting partners are only able to alternately send/recv, because these function calls are blocking by default.到目前为止,我以两种聊天伙伴只能交替发送/接收的方式对其进行编码,因为默认情况下这些函数调用是阻塞的。

Now I would like to modify that program, so that both are able to send and receive simultaneously.现在我想修改该程序,以便两者都能够同时发送和接收。

The problem that I find is, that once you typed some input to the terminal, I don't know how to output received messages, without messing up the current input line of the terminal.我发现的问题是,一旦你向终端输入了一些输入,我不知道如何输出接收到的消息,而不会弄乱终端的当前输入行。

If there was a way to delete that current input line, you could temporarily save that line, print the new message and put the input line right back.如果有办法删除当前输入行,您可以暂时保存该行,打印新消息并将输入行放回原处。

However, I was not able to find a solution for this problem on the internet.但是,我无法在互联网上找到解决此问题的方法。

Is it possible to delete the current input line, and if not, how else could I achieve what I want?是否可以删除当前输入行,如果不能,我还能如何实现我想要的?

I think you should look into ncurses as Edd said in his comment.我认为你应该像 Edd 在他的评论中所说的那样研究ncurses It would allow you to easily manage contents in your terminal window, which sounds like a good idea for your chat program.它可以让您轻松管理终端窗口中的内容,这对您的聊天程序来说听起来是个好主意。 All you'd need to do is store your messages in 2 character arrays:您需要做的就是将您的消息存储在 2 个字符数组中:

char incoming[MSG_MAX]
and
char outgoing[MSG_MAX]

Then you can output those messages wherever you want in your terminal window, since ncurses allows you to specify x,y coordinates on where to put your text.然后您可以在终端窗口中的任何位置输出这些消息,因为 ncurses 允许您在放置文本的位置指定x,y坐标。 Then a simple wrapper for one of ncurses erase() family functions would allow you to delete characters from specify x,y coordinates in your terminal window.然后,ncurses erase()系列函数之一的简单包装器将允许您从终端窗口中指定的x,y坐标中删除字符。

Edit: MSG_MAX is not an actual ncurses macro.编辑: MSG_MAX不是实际的 ncurses 宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM