[Gambas-user] A Way to determine if Terminal supports color
Brian G
brian at westwoodsvcs.com
Sat Jul 31 14:43:31 CEST 2021
"Failure is the key to success;
each mistake teaches us something" .. Morihei Ueshiba
Brian G
----- On Jul 31, 2021, at 4:41 AM, Gambas mailing list user at lists.gambas-basic.org wrote:
> On Fri, 30 Jul 2021, Brian G wrote:
>> Maybe Tobias can help?
>>
>> Is there a way to use the color.available in gb.ncurses without having the
>> screen automatically initialized.
>>
Thanks for taking the time to look at this question Tobias.
I finally found the method below to work correctly.
Using the lower level terminfo database calls. Tested this with most terminals on the linux
Seems to work reliably ..... so far.
Even worked with the gambas console which reports 8 colors!
That snippet I originally found did not work correctly, it crossed the lower level calls with curses calls.
It sure would be helpful if the terminfo data was available through gb.term.
I was looking for a way to determine capabilities without messing with the actual screen.
' Gambas class file termcap
Extern setupterm(term As String, filedes As Integer, errret As Pointer) As Integer In "libtinfo"
Extern tigetnum(cap As String) As Integer In "libtinfo"
Private NoTermInfo As Boolean = False
Public errmsg As String = ""
Public Sub _new()
Dim err As Integer = 0
Dim result As Integer = setupterm(Null, File.out.Handle, VarPtr(err))
If result Then
Select Case err
Case 1
errmsg = "terminfo: terminal is hardcopy, cannot be used for curses applications"
Case 0
errmsg = "terminfo: terminal could not be found, or not enough information for curses applications"
Case -1
errmsg = "terminfo: entry could not be found"
End Select
NoTermInfo = True
Endif
End
'' Returns the number of colors the terminal can support
Public Sub has_color() As Integer
If NoTermInfo Then Return 0
Return tigetnum("colors") ' returns -number if not found
End
>
> gb.ncurses always calls initscr() as soon as the component is loaded to
> establish a clean environment for the other classes "once and for all".
> So there is no way to do that with gb.ncurses. It was conceived as a
> user interface component (although not a lot came of it), not to inspect
> low-level terminal capabilities. (I'm not saying that this wouldn't be
> useful to have, maybe in gb.term?)
>
>> Like using setupterm() and then call has_color()
>> To get the color available info from the terminal cap, so it never initializes
>> the actual screen?
>>
>> Or is there another way to get this info?
>>
>
> Honestly, the quickest way is probably via Extern:
>
> Extern setupterm(term As String, fd As Integer, err As Pointer) As Integer In
> "libncursesw"
> Extern has_colors() As Integer In "libncursesw"
>
> But note that when I try, inspired by your snippet,
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <ncurses.h>
> #include <term.h>
>
> int main(void) {
> int err = 0;
> printf("%d ", setupterm(NULL, 1, &err));
> printf("%d ", err);
> printf("%d\n", has_colors());
> exit(0);
> }
>
> I get the output "0 1 0", meaning my terminal is detected as hardcopy,
> not capable of curses or color.
>
> But when I properly initialize ncurses using initscr(),
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <ncurses.h>
>
> int main(void) {
> initscr();
> int c = has_colors();
> endwin();
>
> printf("%d\n", c);
> exit(0);
> }
>
> I get the expected output "1". Are you sure your method is supposed to work?
>
> Best,
> Tobias
>
> --
> "There's an old saying: Don't change anything... ever!" -- Mr. Monk
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
More information about the User
mailing list