File: win_api.rb

package info (click to toggle)
ruby-tty-reader 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 772 kB
  • sloc: ruby: 1,759; sh: 4; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,303 bytes parent folder | download
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

require "fiddle"

module TTY
  class Reader
    module WinAPI
      include Fiddle

      CRT_HANDLE = Fiddle::Handle.new("msvcrt") rescue Fiddle::Handle.new("crtdll")

      # Get a character from the console without echo.
      #
      # @return [String]
      #   return the character read
      #
      # @api public
      def getch
        @@getch ||= Fiddle::Function.new(CRT_HANDLE["_getch"], [], TYPE_INT)
        @@getch.call
      end
      module_function :getch

      # Gets a character from the console with echo.
      #
      # @return [String]
      #   return the character read
      #
      # @api public
      def getche
        @@getche ||= Fiddle::Function.new(CRT_HANDLE["_getche"], [], TYPE_INT)
        @@getche.call
      end
      module_function :getche

      # Check the console for recent keystroke. If the function
      # returns a nonzero value, a keystroke is waiting in the buffer.
      #
      # @return [Integer]
      #   return a nonzero value if a key has been pressed. Otherwirse,
      #   it returns 0.
      #
      # @api public
      def kbhit
        @@kbhit ||= Fiddle::Function.new(CRT_HANDLE["_kbhit"], [], TYPE_INT)
        @@kbhit.call
      end
      module_function :kbhit
    end # WinAPI
  end # Reader
end # TTY