4

I have a Ruby script that outputs progress messages on the same line, using the carriage return character, like this:

print "\r#{file_name} processed."

As an example, the output changes from 'file001.html' processed. to 'file002.html.' processed and so on until the script completes.

I'd like to replace the last progress message with Done., but I can't just write print "\rDone." because that piece of code outputs something like this:

Done.99.html processed.

I guess I have to empty the line after the last progress message and then print Done.. How do I do that?

4

1 に答える 1

7

clr_eolを使用した後、terminfo変数(機能名el)に対応するバイトシーケンスを送信する必要があります\r。あなたがそれを得ることができるいくつかの方法があります。

最も単純なのは、定数値があると仮定することです。私がチェックした端末では\e[K、それはですが、私はいくつかしかチェックしていません。それらの両方で、次の作品:

clear = "\e[K"
print "foo 123"
print "\r#{clear}bar\n"

次を使用して値を取得することもできます。

clear = `tput el` 

または、 terminfo gemを使用することもできます:

require 'terminfo'
clear = TermInfo.control_string 'el'
于 2012-12-02T20:07:43.583 に答える