我的提案的兩個重要組成部分是:
1.系統(「stty的原料-echo」)#至立即得到任何類型的字符與
結合使用2.使用STDIN.getc和string-var作爲輸入緩衝區來重新打印用戶輸入。
按9(&布爾變量incoming_message)用於超簡單模擬您的服務器消息。
(因爲無論如何你都必須適應你的情況,所以更真實會使示例膨脹,因爲無論如何你必須適應你的想法。)
按x退出。
示例輸出:
$ ruby simultaneous_input_and_output.rb
Hello World!9
The incoming message is: true
Hello World!9x
You entered:
Hello World!9x
End!
#!/usr/bin/ruby
$input_buffer = "";
incoming_message = false
begin
system("stty raw -echo")
begin
str = STDIN.getc
print str.chr
$input_buffer = $input_buffer + str.chr
if str.chr == '9'
incoming_message = true
end
if incoming_message
puts "\nThe incoming message is: " + incoming_message.to_s
print $input_buffer
incoming_message = false
end
end while (str.chr != 'x')
ensure
system("stty -raw echo")
end
puts "\n\nYou entered:"
puts $input_buffer
puts "End!"
PS: 爲系統( 「stty的原始-echo」)看到(用戶周杰倫) How to get a single character without pressing enter?
你能接受使用ncurses庫的解決方案嗎? – 2010-03-03 20:44:01
當然。我對這個軟件包並不熟悉,但我很樂意看到一個例子。 – captncraig 2010-03-03 22:21:22
更深入地觀察ncurses,它絕對看起來像我需要的,我只是不知道如何完成這一切。這個庫很複雜,ruby綁定沒有很好的文檔。 – captncraig 2010-03-05 02:54:57