2021-10-11 15:24:19 +03:00
|
|
|
begin
|
|
|
|
require 'fiddle'
|
|
|
|
require 'fiddle/import'
|
|
|
|
rescue LoadError
|
|
|
|
module Reline::Terminfo
|
|
|
|
def self.curses_dl
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-05-18 15:46:16 +03:00
|
|
|
|
|
|
|
module Reline::Terminfo
|
|
|
|
extend Fiddle::Importer
|
|
|
|
|
|
|
|
class TerminfoError < StandardError; end
|
|
|
|
|
2021-07-05 09:54:12 +03:00
|
|
|
def self.curses_dl_files
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /mingw/, /mswin/
|
|
|
|
# aren't supported
|
|
|
|
[]
|
2021-07-06 17:03:09 +03:00
|
|
|
when /cygwin/
|
|
|
|
%w[cygncursesw-10.dll cygncurses-10.dll]
|
|
|
|
when /darwin/
|
|
|
|
%w[libncursesw.dylib libcursesw.dylib libncurses.dylib libcurses.dylib]
|
2021-07-05 09:54:12 +03:00
|
|
|
else
|
2022-06-13 11:17:38 +03:00
|
|
|
%w[libncursesw.so libcursesw.so libncurses.so libcurses.so]
|
2021-07-05 09:54:12 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-06 07:33:47 +03:00
|
|
|
@curses_dl = false
|
2021-05-18 15:46:16 +03:00
|
|
|
def self.curses_dl
|
2021-08-06 07:33:47 +03:00
|
|
|
return @curses_dl unless @curses_dl == false
|
2021-06-21 12:44:10 +03:00
|
|
|
if RUBY_VERSION >= '3.0.0'
|
|
|
|
# Gem module isn't defined in test-all of the Ruby repository, and
|
|
|
|
# Fiddle in Ruby 3.0.0 or later supports Fiddle::TYPE_VARIADIC.
|
|
|
|
fiddle_supports_variadic = true
|
2022-01-21 17:59:13 +03:00
|
|
|
elsif Fiddle.const_defined?(:VERSION,false) and Gem::Version.create(Fiddle::VERSION) >= Gem::Version.create('1.0.1')
|
2021-05-18 15:46:16 +03:00
|
|
|
# Fiddle::TYPE_VARIADIC is supported from Fiddle 1.0.1.
|
2021-06-21 12:44:10 +03:00
|
|
|
fiddle_supports_variadic = true
|
|
|
|
else
|
|
|
|
fiddle_supports_variadic = false
|
|
|
|
end
|
2021-08-05 20:51:10 +03:00
|
|
|
if fiddle_supports_variadic and not Fiddle.const_defined?(:TYPE_VARIADIC)
|
2021-07-28 17:26:41 +03:00
|
|
|
# If the libffi version is not 3.0.5 or higher, there isn't TYPE_VARIADIC.
|
|
|
|
fiddle_supports_variadic = false
|
|
|
|
end
|
2021-06-21 12:44:10 +03:00
|
|
|
if fiddle_supports_variadic
|
2021-07-05 09:54:12 +03:00
|
|
|
curses_dl_files.each do |curses_name|
|
2021-05-18 15:46:16 +03:00
|
|
|
result = Fiddle::Handle.new(curses_name)
|
|
|
|
rescue Fiddle::DLError
|
|
|
|
next
|
|
|
|
else
|
|
|
|
@curses_dl = result
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2021-08-06 07:33:47 +03:00
|
|
|
@curses_dl = nil if @curses_dl == false
|
2021-05-18 15:46:16 +03:00
|
|
|
@curses_dl
|
|
|
|
end
|
2021-10-11 15:24:19 +03:00
|
|
|
end if not Reline.const_defined?(:Terminfo) or not Reline::Terminfo.respond_to?(:curses_dl)
|
2021-05-18 15:46:16 +03:00
|
|
|
|
|
|
|
module Reline::Terminfo
|
|
|
|
dlload curses_dl
|
|
|
|
#extern 'int setupterm(char *term, int fildes, int *errret)'
|
|
|
|
@setupterm = Fiddle::Function.new(curses_dl['setupterm'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT)
|
|
|
|
#extern 'char *tigetstr(char *capname)'
|
|
|
|
@tigetstr = Fiddle::Function.new(curses_dl['tigetstr'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOIDP)
|
2021-07-24 08:46:54 +03:00
|
|
|
begin
|
|
|
|
#extern 'char *tiparm(const char *str, ...)'
|
|
|
|
@tiparm = Fiddle::Function.new(curses_dl['tiparm'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VARIADIC], Fiddle::TYPE_VOIDP)
|
|
|
|
rescue Fiddle::DLError
|
2021-07-23 23:43:28 +03:00
|
|
|
# OpenBSD lacks tiparm
|
2021-07-24 08:46:54 +03:00
|
|
|
#extern 'char *tparm(const char *str, ...)'
|
|
|
|
@tiparm = Fiddle::Function.new(curses_dl['tparm'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VARIADIC], Fiddle::TYPE_VOIDP)
|
2021-07-23 23:43:28 +03:00
|
|
|
end
|
2022-01-01 17:03:32 +03:00
|
|
|
begin
|
2022-01-01 18:40:24 +03:00
|
|
|
#extern 'int tigetflag(char *str)'
|
2022-01-01 17:03:32 +03:00
|
|
|
@tigetflag = Fiddle::Function.new(curses_dl['tigetflag'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT)
|
|
|
|
rescue Fiddle::DLError
|
|
|
|
# OpenBSD lacks tigetflag
|
2022-01-01 18:40:24 +03:00
|
|
|
#extern 'int tgetflag(char *str)'
|
2022-01-01 17:03:32 +03:00
|
|
|
@tigetflag = Fiddle::Function.new(curses_dl['tgetflag'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT)
|
|
|
|
end
|
2022-01-02 04:17:33 +03:00
|
|
|
begin
|
|
|
|
#extern 'int tigetnum(char *str)'
|
|
|
|
@tigetnum = Fiddle::Function.new(curses_dl['tigetnum'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT)
|
|
|
|
rescue Fiddle::DLError
|
|
|
|
# OpenBSD lacks tigetnum
|
|
|
|
#extern 'int tgetnum(char *str)'
|
|
|
|
@tigetnum = Fiddle::Function.new(curses_dl['tgetnum'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT)
|
|
|
|
end
|
2021-05-18 15:46:16 +03:00
|
|
|
|
|
|
|
def self.setupterm(term, fildes)
|
2022-01-08 06:34:54 +03:00
|
|
|
errret_int = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
|
2021-05-18 15:46:16 +03:00
|
|
|
ret = @setupterm.(term, fildes, errret_int)
|
2022-01-08 06:34:54 +03:00
|
|
|
errret = errret_int[0, Fiddle::SIZEOF_INT].unpack1('i')
|
2021-05-18 15:46:16 +03:00
|
|
|
case ret
|
|
|
|
when 0 # OK
|
|
|
|
0
|
|
|
|
when -1 # ERR
|
|
|
|
case errret
|
|
|
|
when 1
|
|
|
|
raise TerminfoError.new('The terminal is hardcopy, cannot be used for curses applications.')
|
|
|
|
when 0
|
|
|
|
raise TerminfoError.new('The terminal could not be found, or that it is a generic type, having too little information for curses applications to run.')
|
|
|
|
when -1
|
|
|
|
raise TerminfoError.new('The terminfo database could not be found.')
|
|
|
|
else # unknown
|
|
|
|
-1
|
|
|
|
end
|
|
|
|
else # unknown
|
|
|
|
-2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-24 12:04:02 +03:00
|
|
|
class StringWithTiparm < String
|
|
|
|
def tiparm(*args) # for method chain
|
2021-05-19 11:57:30 +03:00
|
|
|
Reline::Terminfo.tiparm(self, *args)
|
|
|
|
end
|
2021-06-24 12:04:02 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.tigetstr(capname)
|
2022-01-02 05:44:45 +03:00
|
|
|
raise TerminfoError, "capname is not String: #{capname.inspect}" unless capname.is_a?(String)
|
2021-08-06 07:34:25 +03:00
|
|
|
capability = @tigetstr.(capname)
|
|
|
|
case capability.to_i
|
|
|
|
when 0, -1
|
|
|
|
raise TerminfoError, "can't find capability: #{capname}"
|
|
|
|
end
|
|
|
|
StringWithTiparm.new(capability.to_s)
|
2021-05-18 15:46:16 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.tiparm(str, *args)
|
|
|
|
new_args = []
|
|
|
|
args.each do |a|
|
|
|
|
new_args << Fiddle::TYPE_INT << a
|
|
|
|
end
|
2021-05-19 11:53:17 +03:00
|
|
|
@tiparm.(str, *new_args).to_s
|
2021-05-18 15:46:16 +03:00
|
|
|
end
|
|
|
|
|
2022-01-01 17:03:32 +03:00
|
|
|
def self.tigetflag(capname)
|
2022-01-02 05:44:45 +03:00
|
|
|
raise TerminfoError, "capname is not String: #{capname.inspect}" unless capname.is_a?(String)
|
2022-01-01 17:03:32 +03:00
|
|
|
flag = @tigetflag.(capname).to_i
|
|
|
|
case flag
|
|
|
|
when -1
|
|
|
|
raise TerminfoError, "not boolean capability: #{capname}"
|
|
|
|
when 0
|
|
|
|
raise TerminfoError, "can't find capability: #{capname}"
|
|
|
|
end
|
|
|
|
flag
|
|
|
|
end
|
|
|
|
|
2022-01-02 04:17:33 +03:00
|
|
|
def self.tigetnum(capname)
|
2022-01-02 05:44:45 +03:00
|
|
|
raise TerminfoError, "capname is not String: #{capname.inspect}" unless capname.is_a?(String)
|
2022-01-02 04:17:33 +03:00
|
|
|
num = @tigetnum.(capname).to_i
|
|
|
|
case num
|
|
|
|
when -2
|
|
|
|
raise TerminfoError, "not numeric capability: #{capname}"
|
|
|
|
when -1
|
|
|
|
raise TerminfoError, "can't find capability: #{capname}"
|
|
|
|
end
|
|
|
|
num
|
|
|
|
end
|
|
|
|
|
2021-05-18 15:46:16 +03:00
|
|
|
def self.enabled?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end if Reline::Terminfo.curses_dl
|
|
|
|
|
|
|
|
module Reline::Terminfo
|
|
|
|
def self.enabled?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end unless Reline::Terminfo.curses_dl
|