英文:
Changing value of printed line
问题
在我的基于命令行界面的Go应用程序中,我需要更新一些已经打印出来的值。我已经找到了一个Stack Overflow的问题,了解到使用\r
可以将"输出指针"指向前一行的第一个字符,从而覆盖它。但是多个\r
并不起作用...那么我该如何更新已经打印出来的第二行呢?
示例:
#Id #Name #pwr #dB
0 Name unkn -34
我希望在不打印新行的情况下更新这些值:
#Id #Name #pwr #dB
0 NewName 45 -34
英文:
in my go-application which is cli-based I Need to update some values which are already printed. I already found a stackoverflow-question where I got to know that with \r
the "Output-pointer" points to the first char of the former line, therefore overriding it. But multiple \r
s arent working...
So how do I update e.g. the second line I printed.
Example:
#Id #Name #pwr #dB
0 Name unkn -34
And I want to update those values without printing new lines
#Id #Name #pwr #dB
0 NewName 45 -34
答案1
得分: 2
除了更改当前行之外,正如你注意到的那样,\r
无法起作用。解决方案是将终端设置为允许更多控制的不同模式。然而,手动完成这个过程很麻烦。幸运的是,有一些库可以简化这个过程。其中一个非常著名的库是ncurses。
对于Go语言,GoCUI看起来很不错。它应该能够提供你所需的所有功能,并具有良好的界面。
英文:
To do anything more than changing the current line, as you noticed, \r
won't work. The solution is to set your terminal in a different mode which allow more control. However, this is painful to do by hand. Luckily, there are libraries out there that makes it easy. A very famous one is ncurses.
For Go, GoCUI looks great. It should give you all the power you need with a nice interface.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论