(Windows 7 x64 running Ruby 1.9.3)
(运行Ruby 1.9.3的Windows 7 x64)
Here's the situation: I've made text game in Ruby, and I'm using the traditional gets
method to get input from the user. When something is happening in the game (i.e. stuff is being printed to the screen), whatever the user has typed for input gets lost and the user has to continue typing what he/she has typed on a new line. What he/she has originally typed before it got lost is still there, just doesn't get shown.
情况就是这样:我在Ruby中制作了文本游戏,并且我使用传统的获取方法来获取用户的输入。当游戏中发生某些事情(即正在向屏幕打印东西)时,无论用户输入什么输入都会丢失,并且用户必须继续键入他/她在新行上键入的内容。他/她最初输入之前输入的内容仍然存在,只是没有显示出来。
If the above didn't make sense try executing this code, and you'll see the problem:
如果以上没有意义,请尝试执行此代码,您将看到问题:
Thread.new do
loop do
puts "Hello!"
sleep 2
end
end
Thread.new do
loop do
gets
end
end
What I want is the line printed (in this case "Hello!"
) to be placed before the line the user is typing into.
我想要的是打印的行(在本例中为“Hello!”)放在用户输入的行之前。
I understand that to achieve this I might need to delve into the Windows API. It may even be impossible. But if there's a way, I'd really like to know.
我知道要实现这一点,我可能需要深入研究Windows API。甚至可能是不可能的。但如果有办法,我真的很想知道。
1 个解决方案
#1
3
The most useful libraries for creating interactive terminal programs are Curses and Readline. The former lets you move the cursor anywhere in the terminal, print in colors, create separate "windows", etc. The latter is essentially a robust alternative to gets
, with command history and autocompletion like in irb
.
用于创建交互式终端程序的最有用的库是Curses和Readline。前者允许您将光标移动到终端中的任何位置,打印颜色,创建单独的“窗口”等。后者本质上是获取的强大替代方法,具有命令历史和自动完成,如irb。
If you want to use Curses with Threads, you can check out my fork of Ruby, which adds this functionality (as well as refactoring the entire curses library).
如果你想使用带有线程的Curses,你可以查看我的Ruby分支,它增加了这个功能(以及重构整个curses库)。
#1
3
The most useful libraries for creating interactive terminal programs are Curses and Readline. The former lets you move the cursor anywhere in the terminal, print in colors, create separate "windows", etc. The latter is essentially a robust alternative to gets
, with command history and autocompletion like in irb
.
用于创建交互式终端程序的最有用的库是Curses和Readline。前者允许您将光标移动到终端中的任何位置,打印颜色,创建单独的“窗口”等。后者本质上是获取的强大替代方法,具有命令历史和自动完成,如irb。
If you want to use Curses with Threads, you can check out my fork of Ruby, which adds this functionality (as well as refactoring the entire curses library).
如果你想使用带有线程的Curses,你可以查看我的Ruby分支,它增加了这个功能(以及重构整个curses库)。