Sha256: 016dc0f5d90415fd8d5a05262bcda5570c1fbf81f38260057dbbf7a9718af632

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 KB

Contents

require 'timeout'

class Interactor
  
  def initialize
    ticker
  end
  
  def wait_for_enter_key(msg, seconds_to_wait)
    begin
      Timeout::timeout(seconds_to_wait) do
        ticker(:start => true, :msg => msg)
        $stdin.gets   
        return true 
      end
    rescue Timeout::Error
      false
    ensure
      ticker(:stop => true)
    end
  end
  
  def start_termination_handler
    @main_thread = Thread.current
    Thread.new do
      loop do
        sleep 0.5
        if $stdin.gets
          if wait_for_enter_key("** Running all specs.. Hit <enter> again to exit RSpactor", 3)
            @main_thread.exit 
            exit
          end
          Runner.run_all_specs
        end
      end
    end
  end
  

  private

  def ticker(opts = {})
    if opts[:stop]
      $stdout.puts "\n"           
      @pointer_running = false
    elsif opts[:start]
      @pointer_running = true
      write(opts[:msg]) if opts[:msg]
    else
      Thread.new do
        loop do
          write('.') if @pointer_running == true
          sleep 1.0
        end
      end      
    end
  end
    
  def write(msg)
    $stdout.print(msg)
    $stdout.flush
  end
end

Version data entries

9 entries across 9 versions & 4 rubygems

Version Path
guillaumegentil-rspactor-0.2.1 lib/interactor.rb
guillaumegentil-rspactor-0.2.2 lib/interactor.rb
guillaumegentil-rspactor-0.2.3 lib/interactor.rb
guillaumegentil-rspactor-0.2.4 lib/interactor.rb
mislav-rspactor-0.3.0 lib/interactor.rb
pelle-rspactor-0.2.2 lib/interactor.rb
pelle-rspactor-0.2.3 lib/interactor.rb
pelle-rspactor-0.2.4 lib/interactor.rb
rspactor-0.2.0 lib/interactor.rb