Editing a telnet server in Ruby

I am implementing a small telnet server in Ruby. The problem that I am currently facing is that I want to add readline support in order to be able to support tabulation and command line history support. I looked at the Readline library , but it looks like it will only work through stdin. Is there a way to do this in Ruby (I noticed a solution for Python )?

+5
source share
1 answer

You can do this by placing the pipe in the readline. Here's an example, using a cycle whileof documentation ri readline, which simply sends command 1, command2, command 3to 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

Conclusion:

["command 1"]
-> command 1
["command 1", "command 2"]
-> command 2
["command 1", "command 2", "command 3"]
-> command 3
+1
source

All Articles