To do completely unbuffered input you can use something like termios
. However you will have to interpret arrow key sequences manually.
If you can live with a middle layer for history completion I suggest using GNU readline, like mentioned previously, or the RawLine library by H3RALD:
http://www.h3rald.com/rawline/
http://www.h3rald.com/articles/real-world-rawline-usage/
Example of unbuffered input with termios
:
require 'rubygems'
require 'termios'
def with_unbuffered_input
old_attrs = Termios.tcgetattr(STDOUT)
new_attrs = old_attrs.dup
new_attrs.lflag &= ~Termios::ECHO
new_attrs.lflag &= ~Termios::ICANON
Termios::tcsetattr(STDOUT, Termios::TCSANOW, new_attrs)
yield
ensure
Termios::tcsetattr(STDOUT, Termios::TCSANOW, old_attrs)
end
with_unbuffered_input do
10.times {
c = STDIN.getc
puts "Got #{c}"
}
end