How can I capture terminal arrow keys in Ruby?
Look into the Ruby console libraries ruby-termios
and ncurses-ruby.
Creating arrow key navigation in a command line application?
Ruby Toolbox is a great resource for finding gems that you can use, the link here pointing toward the search for 'curses'.
ncurses
should work with any ANSI/Posix compliant system.
Backspace and arrow keys aren't working in IRB(Git Bash console) on windows machine
Same thing happened to me. Running irb with --noreadline solved my problem:
irb --noreadline
Arrow keys don't work in irb/ruby command line scripts?
Using Ruby's Readline Library:
require 'readline'
foo = Readline::readline
How to allow users to edit given string via $stdin in ruby
begin
system("stty raw -echo")
print (acc = "Edit me: ")
loop.each_with_object(acc) do |_,acc|
sym = $stdin.getc
case sym.ord
when 13 # carriage return
break acc
when 127 # backspace
print "\e[1D \e[1D"
acc.slice!(acc.length - 1) if acc.length > 0
else # regular symbol
print sym
acc << sym
end
end
ensure
system("stty -raw echo")
puts
puts "\e[0mEntered: |#{acc}|"
end
Here you go. More info on terminal control sequences. Also, ANSI terminal codes.
Related Topics
Does Ruby Provide a Constant_Added Hook Method
Invalid Source Reflection MACro :Has_Many :Through
How to Sign Out in a Rails App, Using Devise Gem, No Route Matches /Users/Sign_Out
How to Require Activerecord in Irb
Rails Console Is Adding Nil Instead of Values
Rails Strong Params - Using Fields from Has_Many Object
Ruby: How to Dynamically Replace Parameters in Native Pg Gem
Ruby String Prepend '\' Character
How to Replace a Pattern in Ruby Array
Fetch VS. [] When Working with Hashes
Utc Time Resets to 2000-01-01 (Ruby). How to Prevent the Time from Resetting
How to Overwrite the '{ }' Object
How to Mimic Browser X509 Client Certificate Verification Without Access to Http Layer
Heroku-18: Git Push Fails. Showing Different Versions of Ruby on Push
Ruby Was-Sdk V2:Seahorse::Client::Networkingerror Exception: Ssl_Connect
Rails: Upload a File or Store a Url
Why Can't I Create Active Records That Count for Something in Rspec