如何擦除控制台应用程序(Linux)中的打印字符? [英] How do I erase printed characters in a console application(Linux)?

查看:13
本文介绍了如何擦除控制台应用程序(Linux)中的打印字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个需要进度条的小型控制台应用程序.比如……

I am creating a small console app that needs a progress bar. Something like...

Conversion: 175/348 Seconds   |==========          |  50%

我的问题是,如何擦除已打印到控制台的字符?当我达到第 51 个百分比时,我必须从控制台中删除这一行并插入一个新行.在我目前的解决方案中,会发生这种情况......

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%

我使用的代码是...

print "Conversion: $converted_seconds/$total_time Seconds   $progress_bar  $converted_percentage%
";

我正在使用 PHP 在 Linux 中执行此操作(只有我会使用该应用程序 - 所以请原谅语言选择).因此,该解决方案应该可以在 Linux 平台上运行 - 但如果您有一个跨平台的解决方案,那就更可取了.

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.

推荐答案

我认为你不需要为语言选择道歉.PHP 是一种非常适合控制台应用程序的语言.

I don't think you need to apologize for the language choice. PHP is a great language for console applications.

试试这个:

<?php
for( $i=0;$i<10;$i++){
  print "$i 
";
  sleep(1);
}
?>

"将用新文本覆盖该行.要创建一个新行,你可以使用 ",但我猜你已经知道了.

The " " will overwrite the line with the new text. To make a new line you can just use " ", but I'm guessing you already knew that.

希望这会有所帮助!我知道这适用于 Linux,但我不知道它是否适用于 Windows 或其他操作系统.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆