Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect key press (non-blocking) w/o getc/gets in Ruby

I have a simple task that needs to wait for something to change on the filesystem (it's essentially a compiler for prototypes). So I've a simple infinite loop with a 5 second sleep after the check for changed files.

loop do
  # if files changed
  #   process files
  #   and puts result
  sleep 5
end

Instead of the Ctrl+C salute, I'd rather be able to test and see if a key has been pressed, without blocking the loop. Essentially I just need a way to tell if there are incoming key presses, then a way to grab them until a Q is met, then exit out of the program.

What I want is:

def wait_for_Q
  key_is_pressed && get_ch == 'Q'
end

loop do
  # if files changed
  #   process files
  #   and puts result
  wait_for_Q or sleep 5
end

Or, is this something Ruby just doesn't do (well)?

like image 931
Robert K Avatar asked Jun 03 '09 19:06

Robert K


2 Answers

Here's one way to do it, using IO#read_nonblock:

def quit?
  begin
    # See if a 'Q' has been typed yet
    while c = STDIN.read_nonblock(1)
      puts "I found a #{c}"
      return true if c == 'Q'
    end
    # No 'Q' found
    false
  rescue Errno::EINTR
    puts "Well, your device seems a little slow..."
    false
  rescue Errno::EAGAIN
    # nothing was ready to be read
    puts "Nothing to be read..."
    false
  rescue EOFError
    # quit on the end of the input stream
    # (user hit CTRL-D)
    puts "Who hit CTRL-D, really?"
    true
  end
end

loop do
  puts "I'm a loop!"
  puts "Checking to see if I should quit..."
  break if quit?
  puts "Nope, let's take a nap"
  sleep 5
  puts "Onto the next iteration!"
end

puts "Oh, I quit."

Bear in mind that even though this uses non-blocking IO, it's still buffered IO. That means that your users will have to hit Q then <Enter>. If you want to do unbuffered IO, I'd suggest checking out ruby's curses library.

like image 158
rampion Avatar answered Sep 30 '22 14:09

rampion


A combination of the other answers gets the desired behavior. Tested in ruby 1.9.3 on OSX and Linux.

loop do
  puts 'foo'
  system("stty raw -echo")
  char = STDIN.read_nonblock(1) rescue nil
  system("stty -raw echo")
  break if /q/i =~ char
  sleep(2)
end
like image 43
Jim Haungs Avatar answered Sep 30 '22 14:09

Jim Haungs