具有ncurses的C99 UTF8字符 [英] C99 UTF8 characters with ncurses

查看:72
本文介绍了具有ncurses的C99 UTF8字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩 ncurses ,遇到了问题.我想用Unicode块字符(U + 2588,█)绘制一个简单的框,但是我无法使其正确显示:

如您所见,我想要的字符显示为〜H .

我遵循了

我的PC语言环境是 en_US.UTF-8 ,我正在使用无吸式终端,它当然是

这是一个非常简单的程序,我不确定这里出了什么问题.有什么建议吗?

解决方案

手册页提供了 mvaddch 函数使用 chtype ,与 wchar_t 不同.

  • mvaddch 对应的curses函数是 mvadd_wch ,它使用第三种类型( cchar_t ).
  • 您可以使用a similar question to a tee. Minimal working example:

    #include <locale.h>
    #include <ncurses.h>
    
    int main() {
        setlocale(LC_ALL, ""); // must be caled before initscr
        WINDOW *win = initscr();
    
        int w, h;
        getmaxyx(win, h, w);
        
        // should fill the left half of the 
        // terminal window with filled block characters
        int i, j;
        for (i = 0; i < h; i++) {
            for (j = 0; j < w/2; j++) {
                mvaddch(x, y, L'\u2588');
            }
        }
    
        refresh(); // show changes
        getch();   // wait for user input
        endwin();  // kill window
        
        return 1;
    }
    

    Compiled with:

    gcc main.c -o main -std=c99 -lncurses
    

    My PC locale is en_US.UTF-8 and I'm using the suckless terminal which is of course perfectly capable of dislaying utf8:

    It's a very simple program and I'm not sure what is going wrong here. Any suggestions?

    解决方案

    The manual page gives a short overview of data-types which are used for function parameters.

    In the example, L'\u2588' is a wide character, which would be stored in a wchar_t type.

    • The mvaddch function uses a chtype, which is not the same as wchar_t.
    • The curses function which corresponds to mvaddch is mvadd_wch, which uses a third type (cchar_t).
    • You could either convert that wide character to a cchar_t using setcchar, or
    • You could store the value in an array of wchar_t and pass that to mvaddwstr.

    这篇关于具有ncurses的C99 UTF8字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆