Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby line editing telnet server

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)?

like image 679
wcmatthysen Avatar asked Nov 13 '22 21:11

wcmatthysen


1 Answers

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
like image 155
Martin Carpenter Avatar answered Dec 28 '22 05:12

Martin Carpenter