Skip to content

Commit fe24d02

Browse files
committed
Clean up platform dispatch in the FFI version
1 parent f66cafa commit fe24d02

5 files changed

Lines changed: 22 additions & 53 deletions

File tree

‎lib/ffi/io/console.rb‎

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,51 +24,26 @@
2424
require_relative 'console/version'
2525
require_relative 'console/common'
2626

27-
case RbConfig::CONFIG['host_os']
28-
when /darwin|openbsd|freebsd|netbsd|linux/i
29-
# If Linux or BSD, try to load the native version
30-
31-
begin
32-
33-
# Attempt to load the native Linux and BSD console logic
34-
require_relative 'console/native_console'
35-
36-
rescue Exception => ex
37-
38-
warn "failed to load native console support: #{ex}" if $VERBOSE
39-
40-
else
41-
42-
# Native ready.
43-
ready = true
44-
45-
end
46-
27+
libs = []
28+
# If Linux or BSD, try to load the native version
29+
case RbConfig::CONFIG['host_os'].downcase
30+
when /darwin|openbsd|freebsd|netbsd/
31+
libs << 'bsd' << 'stty'
32+
when /linux/
33+
libs << 'linux' << 'stty'
4734
when /mswin|win32|ming/i
4835
# If Windows, stty is not possible, always use the stub version
49-
50-
ready = false
51-
36+
else
37+
libs << 'stty'
5238
end
5339

54-
if ready.nil?
55-
# Native is not ready, try to use stty
56-
57-
begin
58-
59-
require_relative 'console/stty_console'
60-
ready = true
61-
62-
rescue Exception => ex2
63-
64-
warn "failed to load stty console support: #{ex2}" if $VERBOSE
65-
ready = false
66-
67-
end
40+
return if libs.any? do |lib|
41+
require_relative "console/#{lib}_console"
42+
rescue Exception => ex
43+
warn "failed to load #{lib} console support: #{ex}" if $VERBOSE
44+
else
45+
true
6846
end
6947

70-
unless ready
71-
# If still not ready, just use stubbed version
72-
73-
require_relative 'console/stub_console'
74-
end
48+
# If still not ready, just use stubbed version
49+
require_relative "console/stub_console"

‎lib/ffi/io/console/bsd_console.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
raise LoadError.new("native console on MacOS only supported on #{tested_platforms.join(', ')}")
77
end
88

9+
require_relative 'native_console'
10+
911
module IO::LibC
1012
extend FFI::Library
1113
ffi_lib FFI::Library::LIBC

‎lib/ffi/io/console/common.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def cursor
6767
syswrite "\e[6n"
6868

6969
return nil if getbyte != 0x1b
70-
return nil if getbyte != ?[.ord
70+
return nil if getbyte != "[".ord
7171

7272
num = 0
7373
result = []

‎lib/ffi/io/console/linux_console.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
warn "native console only tested on #{tested_platforms.join(', ')}"
77
end
88

9+
require_relative 'native_console'
10+
911
module IO::LibC
1012
extend FFI::Library
1113
ffi_lib FFI::Library::LIBC

‎lib/ffi/io/console/native_console.rb‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
# Load appropriate native bits for BSD or Linux
2-
case RbConfig::CONFIG['host_os'].downcase
3-
when /darwin|openbsd|freebsd|netbsd/
4-
require_relative 'bsd_console'
5-
when /linux/
6-
require_relative 'linux_console'
7-
else
8-
raise LoadError.new("no native io/console support")
9-
end
10-
111
# Common logic that uses native calls for console
122
class IO
133
def ttymode

0 commit comments

Comments
 (0)