Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I guarantee a terminal has Unicode/wide character support with NCURSES?

I am developing an NCURSES application for a little TUI (text user interface) exercise. Unfortunately, I do not have the option of using the ever-so-wonderful-and-faithful ASCII. My program uses a LOT of Unicode box drawing characters.

My program can already detect if the terminal is color-capable. I need to do something like:

if(!supportsUnicode()) //I prefer camel-case, it's just the way I am.
{
    fprintf(stderr, "This program requires a Unicode-capable terminal.\n\r");
    exit(1);
}
else
{
    //Yay, we have Unicode! some random UI-related code goes here.
}

This isn't just a matter of simply including ncursesw and just setting the locale. I need to get specific terminal info and actually throw an error if it's not gonna happen. I need to, for example, throw an error when the user tries to run the program in the lovely XTerm rather than the Unicode-capable UXTerm.

like image 605
Mason Watmough Avatar asked Oct 18 '22 15:10

Mason Watmough


1 Answers

As noted, you cannot detect the terminal's capabilities reliably. For that matter, you cannot detect the terminal's support for color either. In either case, your application can only detect what you have configured, which is not the same thing.

Some people have had partial success detecting Unicode support by writing a UTF-encoded character and using the cursor-position report to see where the cursor is (see for example Detect how much of Unicode my terminal supports, even through screen).

Compiling/linking with ncursesw relies upon having your locale configured properly, with some workarounds for terminals (such as PuTTY) which do not support VT100 line-graphics when in UTF-8 mode.

Further reading:

  • Line Graphics curs_add_wch(3x)
  • NCURSES_NO_UTF8_ACS ncurses(3x)
like image 182
Thomas Dickey Avatar answered Oct 29 '22 01:10

Thomas Dickey