Ruby handle up arrow for shell style interface
I am implementing a simple shell style interface开发者_高级运维 in ruby as follows:
exit = false
while(not exit)
print '?> '
case gets("\n").strip
when "quit"
exit = true
when "xxx"
#handle xxx command
end
end
If I press the up arrow, I get ^[[A. Is there anyway to implement this as would be done in a shell where it fills in the previous command?
You should use readline for this. Ruby has an implementation: http://bogojoker.com/readline/
Here is the ruby api for it http://www.ruby-doc.org/stdlib/libdoc/readline/rdoc/index.html
精彩评论