5

我正在为一个小 TUI(文本用户界面)练习开发一个 NCURSES 应用程序。不幸的是,我没有选择使用永远如此美妙和忠实的 ASCII。我的程序使用了很多 Unicode 方框图字符。

我的程序已经可以检测到终端是否具有颜色功能。我需要做类似的事情:

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.
}

这不仅仅是简单地包含ncursesw和设置语言环境的问题。我需要获取特定的终端信息,如果它不会发生,实际上会抛出一个错误。例如,当用户尝试以可爱的XTerm而不是支持 Unicode 的UXTerm.

4

3 回答 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:

于 2016-02-11T10:24:23.197 回答
1
于 2016-02-11T03:06:54.813 回答
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()
{
        setlocale(LC_CTYPE, "");
        return strcmp(nl_langinfo(CODESET), "UTF-8") ? false : true;
}

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

于 2022-03-04T10:50:47.517 回答