如何用Ruby覆盖shell中的打印行? [英] How to overwrite a printed line in the shell with Ruby?
本文介绍了如何用Ruby覆盖shell中的打印行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何用 Ruby 在 Unix shell 中覆盖之前打印的行?
How would I overwrite the previously printed line in a Unix shell with Ruby?
假设我想每秒在 shell 上输出当前时间,但不是每个时间字符串都向下堆叠,我想覆盖之前显示的时间.
Say I'd like to output the current time on a shell every second, but instead of stacking down each time string, I'd like to overwrite the previously displayed time.
推荐答案
您可以在行尾使用 \r
转义序列(下一行将覆盖此行).按照你的例子:
You can use the \r
escape sequence at the end of the line (the next line will overwrite this line). Following your example:
require 'time'
loop do
time = Time.now.to_s + "\r"
print time
$stdout.flush
sleep 1
end
这篇关于如何用Ruby覆盖shell中的打印行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文