Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 17 additions & 42 deletions lib/ffi/io/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,26 @@
require_relative 'console/version'
require_relative 'console/common'

case RbConfig::CONFIG['host_os']
when /darwin|openbsd|freebsd|netbsd|linux/i
# If Linux or BSD, try to load the native version

begin

# Attempt to load the native Linux and BSD console logic
require_relative 'console/native_console'

rescue Exception => ex

warn "failed to load native console support: #{ex}" if $VERBOSE

else

# Native ready.
ready = true

end

libs = []
# If Linux or BSD, try to load the native version
case RbConfig::CONFIG['host_os'].downcase
when /darwin|openbsd|freebsd|netbsd/
libs << 'bsd' << 'stty'
when /linux/
libs << 'linux' << 'stty'
when /mswin|win32|ming/i
# If Windows, stty is not possible, always use the stub version

ready = false

else
libs << 'stty'
end

if ready.nil?
# Native is not ready, try to use stty

begin

require_relative 'console/stty_console'
ready = true

rescue Exception => ex2

warn "failed to load stty console support: #{ex2}" if $VERBOSE
ready = false

end
return if libs.any? do |lib|
require_relative "console/#{lib}_console"
rescue Exception => ex
warn "failed to load #{lib} console support: #{ex}" if $VERBOSE
else
true
end

unless ready
# If still not ready, just use stubbed version

require_relative 'console/stub_console'
end
# If still not ready, just use stubbed version
require_relative "console/stub_console"
2 changes: 2 additions & 0 deletions lib/ffi/io/console/bsd_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
raise LoadError.new("native console on MacOS only supported on #{tested_platforms.join(', ')}")
end

require_relative 'native_console'

module IO::LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
Expand Down
2 changes: 1 addition & 1 deletion lib/ffi/io/console/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def cursor
syswrite "\e[6n"

return nil if getbyte != 0x1b
return nil if getbyte != ?[.ord
return nil if getbyte != "[".ord

num = 0
result = []
Expand Down
2 changes: 2 additions & 0 deletions lib/ffi/io/console/linux_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
warn "native console only tested on #{tested_platforms.join(', ')}"
end

require_relative 'native_console'

module IO::LibC
extend FFI::Library
ffi_lib FFI::Library::LIBC
Expand Down
10 changes: 0 additions & 10 deletions lib/ffi/io/console/native_console.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
# Load appropriate native bits for BSD or Linux
case RbConfig::CONFIG['host_os'].downcase
when /darwin|openbsd|freebsd|netbsd/
require_relative 'bsd_console'
when /linux/
require_relative 'linux_console'
else
raise LoadError.new("no native io/console support")
end

# Common logic that uses native calls for console
class IO
def ttymode
Expand Down
Loading