使用 ruby 接管控制台输出
当我从控制台运行 vim 或 top 时,它们能够接管渲染整个控制台。当我退出时,我会返回到控制台。
可以从红宝石中做到这一点吗?作为一个简单的示例,当我运行此命令时,我将如何执行以下操作
# Rakefile
task :clock do
loop do
console.render Time.now
sleep 1
end
end
,控制台将被清除,第一行将显示时间。当我退出时,我会继续控制台会话,就像我运行 rake Clock 之前一样。
更新
检查了 tictactoe 示例 中的 ruby 诅咒后,这里是时钟示例的实现。我在随机线上显示了时钟来演示刷新整个控制台。
#!/usr/bin/env ruby
require 'curses'
loop do
Curses.clear
Curses.setpos(rand * 10, 0)
Curses.addstr(Time.now.to_s);
Curses.refresh
sleep 1
end
When I run vim or top from a console they are able to take over rendering the whole console. When I quit I'm then returned to the console.
Is it possible to do this from ruby? As a simple example, how would I do the following
# Rakefile
task :clock do
loop do
console.render Time.now
sleep 1
end
end
when I run this the console would be cleared and the first line would show the time. When I quit I'd then continue the console session as it was before I ran rake clock.
Update
Having checked the tictactoe example for ruby curses here's an implementation of the clock example. I've shown the clock on random lines to demonstrate refreshing the whole console.
#!/usr/bin/env ruby
require 'curses'
loop do
Curses.clear
Curses.setpos(rand * 10, 0)
Curses.addstr(Time.now.to_s);
Curses.refresh
sleep 1
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找 Rubycurses 库,它可以让您完全控制屏幕:定位、颜色等。
它不是一个很好的文档库,但在 Stackoverflow 中搜索“[ruby]curses”将为您提供示例链接。
You're looking for the Ruby curses library which gives you full control over the screen: positioning, color, &c.
It's not a well document library, but a Stackoverflow search for "[ruby] curses" will give you links to examples.