Skip to content

Commit

Permalink
Merge pull request #418 from aycabta/implement-tigetnum
Browse files Browse the repository at this point in the history
Implement Reline::Terminfo.tigetnum
  • Loading branch information
aycabta authored Jan 2, 2022
2 parents af4d77b + 695212d commit ac4662e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/reline/terminfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ module Reline::Terminfo
#extern 'int tgetflag(char *str)'
@tigetflag = Fiddle::Function.new(curses_dl['tgetflag'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT)
end
# TODO: add int tigetnum(char *capname)
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

def self.setupterm(term, fildes)
errret_int = String.new("\x00" * 8, encoding: 'ASCII-8BIT')
Expand Down Expand Up @@ -141,6 +148,17 @@ def self.tigetflag(capname)
flag
end

def self.tigetnum(capname)
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

def self.enabled?
true
end
Expand Down
11 changes: 11 additions & 0 deletions test/reline/test_terminfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ def test_tigetflag_with_error
assert_raise(Reline::Terminfo::TerminfoError) { Reline::Terminfo.tigetflag('cuu') }
assert_raise(Reline::Terminfo::TerminfoError) { Reline::Terminfo.tigetflag('unknown') }
end

def test_tigetnum
assert_instance_of Integer, Reline::Terminfo.tigetnum('colors')
rescue Reline::Terminfo::TerminfoError => e
omit e.message
end

def test_tigetnum_with_error
assert_raise(Reline::Terminfo::TerminfoError) { Reline::Terminfo.tigetnum('cuu') }
assert_raise(Reline::Terminfo::TerminfoError) { Reline::Terminfo.tigetnum('unknown') }
end
end if Reline::Terminfo.enabled?

0 comments on commit ac4662e

Please sign in to comment.