具有ncurses的C99 UTF8字符 [英] C99 UTF8 characters with ncurses
问题描述
我正在玩 ncurses
,遇到了问题.我想用Unicode块字符(U + 2588,█)绘制一个简单的框,但是我无法使其正确显示:
如您所见,我想要的字符显示为〜H
.
我遵循了
我的PC语言环境是 en_US.UTF-8
,我正在使用无吸式终端,它当然是
这是一个非常简单的程序,我不确定这里出了什么问题.有什么建议吗?
手册页提供了 mvaddch
函数使用 chtype
,与 wchar_t
不同.
mvaddch
对应的curses函数是 mvadd_wch
,它使用第三种类型( cchar_t
).I am playing around with ncurses
and have encountered a problem. I want to draw a simple box with Unicode block characters (U+2588, █), but I cannot get it to display properly:
As you can see, the character I want is displayed as ~H
.
I followed the intructions from 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 achtype
, which is not the same aswchar_t
. - The curses function which corresponds to
mvaddch
ismvadd_wch
, which uses a third type (cchar_t
). - You could either convert that wide character to a
cchar_t
usingsetcchar
, or - You could store the value in an array of
wchar_t
and pass that to mvaddwstr.
这篇关于具有ncurses的C99 UTF8字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!