MichaelR
MichaelR

Reputation: 999

Wait for key stroke while running script in ruby

Is there a way to run a ruby script and while executing commands in the script ,still respond to key stroke ?

I want to run a ruby script but be able to press "Space" and pause the script (after currently running command executes ), and then press "Space" again and resume the script .

My only idea (and im sure its a weird one) , is to open a new thread and wait for key strokes there , then when ill get a key stroke , set a stop_flag. Only now it looks like i need to be checking this flag after each command to know when to stop.

Upvotes: 1

Views: 3752

Answers (3)

racecannon
racecannon

Reputation: 11

You can use a system command.

In Windows use: system "pause>null"

This would be different for each OS. So, you could set a variable to check the OS. Then use the appropriate command. If you want to see if the OS is Windows, your code would look like this:

if RUBY_PLATFORM =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/ $operatingSystem="win" end

Upvotes: 1

Josh Voigts
Josh Voigts

Reputation: 4132

Similar idea to @Catnapper, I thought I'd share it although he beat me to the punch.

require 'io/console' # Ruby 1.9

# Wait for the spacebar key to be pressed
def wait_for_spacebar
   sleep 1 while $stdin.getch != " "
end

# Fork a process that waits for the spacebar 
# to be pressed. When pressed, send a signal 
# to the main process.
def fork_new_waiter
   Process.fork do
      wait_for_spacebar
      Process.kill("USR1", Process.ppid)
   end
end

# Wait for a signal from the forked process
Signal.trap("USR1") do
   wait_for_spacebar

   # Debug code here

   fork_new_waiter
end

# Traps SIGINT so the program terminates nicely
Signal.trap("INT") do
   exit
end

fork_new_waiter

# Run program here in place of this loop
i = 0
loop do
   print i+=1
   sleep 1
end

Upvotes: 1

Catnapper
Catnapper

Reputation: 1905

You can use a signal to turn debug output on and off at will, if you have a logger set up with appropriate code sprinkled throughout your script:

 pid = fork do

  # set up a logger
  require 'logger'
  log = Logger.new(STDOUT)
  log.level = Logger::INFO

  # toggle between INFO and DEBUG log levels on SIGUSR1
  trap(:SIGUSR1) do
    if log.level == Logger::DEBUG
      log.level = Logger::INFO
    else
      log.level = Logger::DEBUG
    end
  end

  # Main loop - increment a counter and occasionally print progress
  # as INFO level.  DEBUG level prints progress at every iteration.
  counter = 0
  loop do
    counter += 1
    exit if counter > 100
    log.debug "Counter is #{counter}"
    log.info "Counter is #{counter}" if counter % 10 == 0
    sleep 0.1
  end

end

# This makes sure that the signal sender process exits when the
# child process exits - only needed here to make the example
# terminate nicely.
trap(:SIGCLD) do
  exit(0) if Process.wait(-1, Process::WNOHANG) == pid
end

# This is an example of sending a signal to another process.
# Any process may signal another by pid.
# This example uses a forking parent-child model because this
# approach conveniently yields the child pid to the parent.
loop do
  puts "Press ENTER to send SIGUSR1 to child"
  STDIN.gets
  Process.kill :SIGUSR1, pid
end

The forking and SIGCLD trapping is to make the example fit into one file; any process may send a signal to another.

The code inside the fork block is your script. The script sets up a logger with a default log level of INFO, and a handler for the SIGUSR1 signal that toggles the logger between DEBUG and INFO levels.

The stuff outside of the fork block is just an example of sending a signal to another process. Pressing ENTER will send the signal and alter the logging level of the other process.

This works on POSIX systems, I have no idea about Windows.

Upvotes: 1

Related Questions