Sha256: 84490d5374c8f365e99f796830ecdab49db91eeb276704aa2b74d52be5a26286

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'timeout'

module RSpactor
  class Interactor
    def initialize(dir, options = {})
      @root = dir
      @options = options
      ticker
    end
    
    def wait_for_enter_key(msg, seconds_to_wait, clear = false)
      begin
        Timeout::timeout(seconds_to_wait) do
          system("clear;") if clear
          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, @options[:clear])
              @main_thread.exit
              exit
            end
            Runner.new(@root).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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
guillaumegentil-rspactor-0.4.1 lib/rspactor/interactor.rb
guillaumegentil-rspactor-0.4 lib/rspactor/interactor.rb