英文:
Lock a prompt to the bottom of the screen
问题
我正在用Go编写一个聊天客户端,作为我正在编写的一个库的概念验证。它需要接收用户输入并在同一个终端窗口中打印传入的消息。考虑以下情况。
user@debian ~ $ ./client
no session> 192.168.1.100
> Hello, there!
Hi!
> So, did you get that feature working yet?
Nope. I thought you were going to ask on StackOverflow.
>> Yeah, I'm aski
如果我在输入时收到另一条消息,那么应该产生以下结果。
user@debian ~ $ ./client
no session> 192.168.1.100
> Hello, there!
Hi!
> So, did you get that feature working yet?
Nope. I thought you were going to ask on StackOverflow.
Or did you want me to?
>> Yeah, I'm aski
在Go中有没有实现这个的方法?
谢谢
英文:
all. I'm writing a chat client in Go as a proof-of-concept of a library I'm in the process of writing. It needs to take user input and print incoming messages in the same terminal window. Consider the following.
user@debian ~ $ ./client
no session> 192.168.1.100
> Hello, there!
Hi!
> So, did you get that feature working yet?
Nope. I thought you were going to ask on StackOverflow.
>> Yeah, I'm aski
If another message arrives while I'm typing, then it should produce the following.
user@debian ~ $ ./client
no session> 192.168.1.100
> Hello, there!
Hi!
> So, did you get that feature working yet?
Nope. I thought you were going to ask on StackOverflow.
Or did you want me to?
>> Yeah, I'm aski
Is there a way to do this in Go?
Thanks
答案1
得分: 4
如果你曾经使用过像那样的控制台应用程序,那么它可能使用了类似ncurses的库来实现。你不会想自己编写这样的程序,因为它非常复杂。
对于Go语言,我推荐使用termbox-go。它很容易上手,并且有一个良好结构的API。
英文:
If you've ever used a console application that behaves like that, it probably used a library like ncurses to do that. You wouldn't want to program that yourself because it's quite complex.
For Go I'd recommend termbox-go. It's easy to pick up and has a well structured api.
答案2
得分: 0
这可能不是正确的方法(我不是一个控制台开发者),但是一段时间以前,我在我的安卓手机上用Go语言做了一个终端动画。通过清除屏幕、绘制、清除等操作来实现。
我不记得我用什么方法来清除屏幕,但是举个例子,我刚刚尝试了这个,fmt.Print("\033[2J")
,它似乎可以工作。但是要注意,清除屏幕的转义序列的支持可能因平台和终端模拟器而异。
但是要记住,你可以维护一个客户端应该显示的缓冲区,当接收到消息时,清除屏幕,然后修改和重新打印缓冲区。
英文:
This might not be the proper way to do it (not a console developer) but a while back I did a terminal animation in Go that I ran on my Android phone. Accomplished this by clearing the screen, drawing, clearing, etc.
I dont remember what I used to clear the screen but for example I just tried this, fmt.Print("\033[2J")
and it seems to work. Word of caution here. The support for escape sequences to clear the screen may differ for platform and even the terminal emulator.
But with that in mind, you could maintain a buffer of what the client should look like, when a message is received, clear the screen, and then alter and reprint the buffer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论