Skip to content

Commit

Permalink
[ruby/reline] Continue processing even if terminfo database couldn't
Browse files Browse the repository at this point in the history
be found
(ruby/reline#673)

Fix ruby/reline#447 ruby/reline#543

This problem occurs when Fiddle can be loaded, curses can be loaded, and TERM is not registered in Terminfo.
It should also occur at hardcopy terminals and when Terminfo information is low, but no such reports have been received.

Reline should not abort the process because of missing Terminfo.
Reline proceeds with `Reline::Terminfo.enabled? == false` when fiddle or curses cannot be loaded.
And does the same when Terminfo is present but TERM is not.
https://github.com/ruby/reline/blob/ebab2875f1226f877376558d8758bc0e2a1776c7/lib/reline/terminfo.rb#L156-L160

You can check the operation with `TERM=foo bundle exec bin/console`.

ruby/reline@4ce247ce2b
  • Loading branch information
ima1zumi authored and matzbot committed Apr 6, 2024
1 parent f022a70 commit c2d02a6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/reline/ansi.rb
Expand Up @@ -315,7 +315,7 @@ def self.move_cursor_down(x)
end

def self.hide_cursor
if Reline::Terminfo.enabled?
if Reline::Terminfo.enabled? && Reline::Terminfo.term_supported?
begin
@@output.write Reline::Terminfo.tigetstr('civis')
rescue Reline::Terminfo::TerminfoError
Expand All @@ -327,7 +327,7 @@ def self.hide_cursor
end

def self.show_cursor
if Reline::Terminfo.enabled?
if Reline::Terminfo.enabled? && Reline::Terminfo.term_supported?
begin
@@output.write Reline::Terminfo.tigetstr('cnorm')
rescue Reline::Terminfo::TerminfoError
Expand Down
21 changes: 7 additions & 14 deletions lib/reline/terminfo.rb
Expand Up @@ -80,23 +80,11 @@ module Reline::Terminfo
def self.setupterm(term, fildes)
errret_int = Fiddle::Pointer.malloc(Fiddle::SIZEOF_INT)
ret = @setupterm.(term, fildes, errret_int)
errret = errret_int[0, Fiddle::SIZEOF_INT].unpack1('i')
case ret
when 0 # OK
0
@term_supported = true
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
@term_supported = false
end
end

Expand Down Expand Up @@ -148,9 +136,14 @@ def self.tigetnum(capname)
num
end

# NOTE: This means Fiddle and curses are enabled.
def self.enabled?
true
end

def self.term_supported?
@term_supported
end
end if Reline::Terminfo.curses_dl

module Reline::Terminfo
Expand Down
2 changes: 1 addition & 1 deletion test/reline/test_ansi_with_terminfo.rb
Expand Up @@ -109,4 +109,4 @@ def test_more_emacs
assert_key_binding("\e ", :em_set_mark, [:emacs])
assert_key_binding("\C-x\C-x", :em_exchange_mark, [:emacs])
end
end if Reline::Terminfo.enabled?
end if Reline::Terminfo.enabled? && Reline::Terminfo.term_supported?
2 changes: 1 addition & 1 deletion test/reline/test_terminfo.rb
Expand Up @@ -58,4 +58,4 @@ def test_tigetnum_with_error
assert_raise(Reline::Terminfo::TerminfoError) { Reline::Terminfo.tigetnum('unknown') }
assert_raise(Reline::Terminfo::TerminfoError) { Reline::Terminfo.tigetnum(nil) }
end
end if Reline::Terminfo.enabled?
end if Reline::Terminfo.enabled? && Reline::Terminfo.term_supported?

0 comments on commit c2d02a6

Please sign in to comment.