同时在ruby中保留用户输入

同时在ruby中保留用户输入

本文介绍了输出到控制台,同时在ruby中保留用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ruby脚本,同步和异步接收和显示来自服务器的消息,并允许用户在控制台上输入。当接收到消息时,它当前正在写入用户正在键入的中间。输入本身不是乱码,但它看起来可怕。理想情况下,它将保存用户当前输入,输出消息,然后在下一行恢复输入。我在c通过拦截每一个关键笔划,但我记得的是,这是一个主要的麻烦。我对ruby很新,所以我不知道是否有一个很好的方法来做到这一点,或如何做。



示例:用户正在输入> abcde ,并且出现消息 hello ,用户类型 fgh after。控制台现在会显示:

 > abcdehellofgh 

,用户可以在最后继续输入。我想显示:

  hello 
> abcdefgh

p>

解决方案

我的建议的两个重要部分是:

1. system raw -echo)#立即获得任何键入的字符与

的组合2.使用STDIN.getc和一个字符串var作为输入缓冲区重新打印用户输入。



按9(&布尔变量incoming_message)用于服务器消息的超简单模拟。

(更逼真会使示例膨胀, ,因为你必须适应你的场景的想法。)



按x退出。



示例输出:

  $ ruby​​ simultaneous_input_and_output.rb 
Hello World!9
传入的消息是:true
Hello World!9x

输入:
Hello World!9x
结束!







 #!/ usr / bin / ruby​​ 

$ input_buffer =;
incoming_message = false

begin
系统(stty raw -echo)

begin
str = STDIN.getc
print str.chr

$ input_buffer = $ input_buffer + str.chr
如果str.chr =='9'
incoming_message = true
end
如果incoming_message
输入\\\
传入的消息是:+ incoming_message.to_s
print $ input_buffer
incoming_message = false
end
end while(str.chr !='x')
确保
系统(stty -raw echo)
end

输入\\\
\\\
You entered:
puts $ input_buffer
输入End!






PS:
stty raw -echo)see(User Jay)


I have a ruby script that is simultaneously and asynchronously receiving and displaying messages from a server, and allowing user input on the console. When a message is received, it is currently being written in the middle of what the user is typing. The input itself isn't garbled, but it looks horrible. Ideally, it would save the users current input, output the message, and then restore the input on the next line. I've done this in c by intercepting every key stroke, but all I remember is that it was a major hassle. I'm fairly new to ruby, so I'm not sure if there is a good way to do this, or how to do it.

Example: User is typing >abcde, and message hello comes in, and user types fgh after. The console would now show:

>abcdehellofgh

and user can continue typing at the end. I would like it to show:

hello
>abcdefgh
解决方案

The two important parts of my proposal are:
1. system("stty raw -echo") # to instantly get any typed character in combination with
2. using STDIN.getc with a string-var serving as an input-buffer to reprint the user input.

Pressing 9 (& boolean variable incoming_message) is used for ultra-simple simulation for your server-messages.
(Being more realistic would bloat the example without adding much, since you have to adapt the idea for your scenario anyway.)

Press x for exit.

Sample output:

$ 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!"


P.S.:for system("stty raw -echo") see (User Jay)How to get a single character without pressing enter?

这篇关于输出到控制台,同时在ruby中保留用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:23