I am implementing a little telnet server in Ruby. The problem I'm currently facing is that I want to add readline support so that I can have tab-completion and command-line history support. I have looked at the Readline library, but it seems as though it will only work via stdin. Is there any way of doing this in Ruby (I noticed the solution for Python)?
You can do this by plumbing a pipe into readline. Here's an example using the while
loop from the ri readline documentation that just sends command 1
, command2
, command 3
to readline.
require 'readline'
rd, wr = IO.pipe
(1..3).each do |i|
wr.puts "command #{i}"
end
wr.close
Readline.input = rd
while buf = Readline.readline('', true)
p Readline::HISTORY.to_a
print("-> ", buf, "\n")
end
Output:
["command 1"]
-> command 1
["command 1", "command 2"]
-> command 2
["command 1", "command 2", "command 3"]
-> command 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With