File: keyboard.rb

package info (click to toggle)
ruby-em-synchrony 1.0.5-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 572 kB
  • sloc: ruby: 3,458; sh: 37; sql: 7; makefile: 2
file content (33 lines) | stat: -rw-r--r-- 731 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module EventMachine
  module Synchrony
    class Keyboard
      attr_reader :current_fiber, :separator
      
      def gets
        @current_fiber = Fiber.current        
        EM.open_keyboard(EventMachine::Synchrony::KeyboardHandler, self)
        
        Fiber.yield
      end
    end
    
    class KeyboardHandler < EM::Connection      
      include EM::Protocols::LineText2
      
      def initialize(keyboard)
        @keyboard = keyboard
      end
          
      def receive_line(line)
        # Simulate gets by adding a trailing line feed
        @input = "#{line}#{$/}"
        
        close_connection
      end
      
      def unbind
        @keyboard.current_fiber.resume @input
      end
    end
  end
end