【问题标题】:Refreshing several lines in console while running运行时刷新控制台中的几行
【发布时间】:2015-09-02 02:10:27
【问题描述】:

我想制作一个看起来像top 或类似的程序,它在运行时会更新几行。 Mac OS Terminal.app 中的system "clear" 实际上只是将当前内容滚动到顶部,所以如果我向上滚动,我会看到很多垃圾。
可以通过curses 以某种方式解决,但这不是矫枉过正吗?

【问题讨论】:

  • 我猜curses 不是矫枉过正,而是标准的方式。毕竟它在 2.1 之前的 ruby​​ 核心库中。

标签: ruby console


【解决方案1】:

您正在尝试制作的内容称为“备用屏幕”。
如果您认为curses 对此太过分了,那么也许您可以执行here 提供给我的解决方案。

print `tput smcup`
puts 'We are on alternate screen'
# ...do something here...

# when you finished, do this to get out of the alternate screen
print `tput rmcup`

重写几行:

print "\r\e[#{number_of_lines_to_jump_up_to}A"

删除前面的字符:

print "\b"*number_of_characters_to_delete  # \b for backspace

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-10
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2014-03-07
    • 1970-01-01
    相关资源
    最近更新 更多