From 695212d5d239b35f6a46ca1970e9fcfcfcc7aab9 Mon Sep 17 00:00:00 2001 From: aycabta Date: Sun, 2 Jan 2022 10:17:33 +0900 Subject: [PATCH] Implement Reline::Terminfo.tigetnum --- lib/reline/terminfo.rb | 20 +++++++++++++++++++- test/reline/test_terminfo.rb | 11 +++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/reline/terminfo.rb b/lib/reline/terminfo.rb index 8b3105ceea..95ff082172 100644 --- a/lib/reline/terminfo.rb +++ b/lib/reline/terminfo.rb @@ -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') @@ -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 diff --git a/test/reline/test_terminfo.rb b/test/reline/test_terminfo.rb index 164ee7123f..8aa1f56c71 100644 --- a/test/reline/test_terminfo.rb +++ b/test/reline/test_terminfo.rb @@ -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?