[英]How to update command line output?
In Linux, While stdout
to command line, I want to update specific area, such as the apt-get
output: 在Linux中,当
stdout
到命令行时,我想更新特定区域,例如apt-get
输出:
54% [Waiting for headers] [Waiting for headers] 211 kB/s 3s
the percentage , kB/s and second will update each second. 百分比 , kB / s和秒将每秒更新。
If we use printf
then we will get multiple lines output them. 如果我们使用
printf
那么我们将获得多行输出。 I have tried following printf
: 我试过跟随
printf
:
printf("\e[1;1H\e[2J");
But it cleans all the output. 但它清除了所有输出。
My question is how to update specific area and keep others stable? 我的问题是如何更新特定区域并保持其他区域稳定?
Use the carriage return. 使用回车。 It will move the cursor back to the start of the line, from which you can overwrite what was in the line before.
它会将光标移回到行的开头,您可以从该行开始覆盖行中的内容。 For example:
例如:
printf("Hello, world!\rX");
Will be visible as: 将显示为:
Xello, world!
Make sure you flush stdout
often if you want it to be visible: 如果您希望它显示,请确保经常刷新
stdout
:
fflush(stdout);
In addition (of the useful \\r
& fflush
advice above), if you want a full screen console output, consider using ncurses . 另外(上面有用的
\\r
& fflush
建议 ),如果你想要全屏控制台输出,可以考虑使用ncurses 。 If you want an editable input line, the GNU readline library is useful too! 如果你想要一个可编辑的输入行,那么GNU readline库也很有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.