如何在控制台应用程序(Linux)中删除打印字符? [英] How do I erase printed characters in a console application(Linux)?
问题描述
我正在创建一个需要进度条的小控制台应用程序。像... ...
转换:175/348 Seconds | ========== |我的问题是,如何删除已经打印到控制台上的字符?如何删除已打印到控制台上的字符?当我达到第51百分比,我必须从控制台中删除这一行,并插入一个新行。在我目前的解决方案中,这是发生了什么... 转换:175/348 Seconds | ======= === | 50%
转换:179/348秒| ========== | 52%
转换:183/348秒| ========== | 54%
转换:187/348秒| =========== | 56%
我使用的代码是...
print转换:$ converted_seconds / $ total_time秒$ progress_bar $ converted_percentage%\\\
;
我在Linux中使用PHP进行此操作(只有我会使用应用程序 - 选择)。所以,解决方案应该在Linux平台上工作 - 但是如果你有一个跨平台的解决方案,这将是更可取的。
解决方案我不认为你需要为语言选择道歉。
<$ c $ c><?php
for($ i = 0; $ i <10; $ i ++){
print$ i \r;
sleep(1);
}
?>
\r将使用新文本覆盖行。要做一个新行,你可以使用\\\
,但我猜你已经知道。
希望这有助于!我知道这在Linux中工作,但我不知道它是否在Windows或其他操作系统中工作。
I am creating a small console app that needs a progress bar. Something like...
Conversion: 175/348 Seconds |========== | 50%
My question is, how do you erase characters already printed to the console? When I reach the 51st percentage, I have to erase this line from the console and insert a new line. In my current solution, this is what happens...
Conversion: 175/348 Seconds |========== | 50%
Conversion: 179/348 Seconds |========== | 52%
Conversion: 183/348 Seconds |========== | 54%
Conversion: 187/348 Seconds |=========== | 56%
Code I use is...
print "Conversion: $converted_seconds/$total_time Seconds $progress_bar $converted_percentage%\n";
I am doing this in Linux using PHP(only I will use the app - so please excuse the language choice). So, the solution should work on the Linux platform - but if you have a solution that's cross platform, that would be preferable.
I don't think you need to apologize for the language choice. PHP is a great language for console applications.
Try this out:
<?php
for( $i=0;$i<10;$i++){
print "$i \r";
sleep(1);
}
?>
The "\r" will over-right the line with the new text. To make a new line you can just use "\n", but I'm guessing you already knew that.
Hope this helps! I know this works in Linux, but I don't know if it works in Windows or other operating systems.
这篇关于如何在控制台应用程序(Linux)中删除打印字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!