在终端中修改文本 [英] Modifying text in the terminal
问题描述
是否可以在不清除屏幕的情况下修改打印到终端的文本?
Is it possible to modify text I printed to the terminal without clearing the screen?
例如,如果我以百分比显示进度,是否可以在不清除屏幕并再次打印的情况下修改该百分比?我正在寻找一种跨平台的方式,如果有的话.
For instance, if I'm showing progress of something in percentage, can I modify that percentage without having to clear the screen and printing again? I'm looking for a cross-platform way, if there is one.
谈论C ++.
谢谢
推荐答案
有多种方法可以执行此操作,并且根据要投入多少精力,您可以使用ascii做很多很酷的事情终端窗口中显示文本.
There are a number of ways to do this, and depending on how much effort you want to put into it, you can do a lot of cool things with ascii text in a terminal window.
-
高级: ncurses库
更容易:使用printf
或cout
Easier: ansi escape characters with printf
or cout
最简单:正如其他人所说,只需使用\r
即可在不换行的情况下返回回车.
Easiest: As others have said, simply use \r
for a carriage return without a linefeed.
使用ESC序列返回两个字符的示例:
example of using the ESC sequence to go back two characters:
#include <iostream>
#define ESC char(0x1B)
int main(){
std::cout << "This will overwrite 'rs' in the following: characters" << ESC << "[2D" << "xx" << std::endl;
return 0;
}
这篇关于在终端中修改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!