5

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.

1
  • note that the new line sequence is \r\n, not \n\r, but you don't need that in C because the C runtime will convert to the correct new line on that platform
    – phuclv
    Mar 27, 2022 at 14:58

3 Answers 3

2

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:

1

You can't. ncurses(w) uses termcap to determine what capabilities a terminal has, and that looks at the $TERM environment variable to determine what terminal is being used. There is no special value of that variable that indicates that a terminal supports Unicode; both XTerm and UXTerm set TERM=xterm. Many other terminal applications use that value of $TERM as well, including both ones that support Unicode and ones that don't. (Indeed, in many terminal emulators, it's possible to enable and disable Unicode support at runtime.)

If you want to start outputting Unicode text to the terminal, you will just have to take it on faith that the user's terminal will support that.

If all you want to do is output box drawing characters, though, you may not need Unicode at all — those characters are available as part of the VT100 graphical character set. You can output these characters in a ncurses application using the ACS_* constants (e.g, ACS_ULCORNER for ), or use a function like box() to draw a larger figure for you.

0

The nl_langinfo() function shall return a pointer to a string containing information relevant to the particular language or cultural area defined in the current locale.

#include <langinfo.h>
#include <locale.h>
#include <stdbool.h>
#include <string.h>

bool supportsUnicode()
{
        /* Set a locale for the ctype and multibyte functions.
         * This controls recognition of upper and lower case,
         * alphabetic or non-alphabetic characters, and so on.
         */
        setlocale(LC_CTYPE, "en_US.UTF-8");
        return (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) ? true : false;
}

Refer to htop source code which can draw lines with/without Unicode.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.