ncursesでUTF-8表示
UTF-8対応版なら先頭でsetlocale()しておくだけで普通に表示できる。 ただし(Fedora Coreの場合は)リンク時に「 -lncurses ではなくて -lncursesw をリンクすること」。UTF-8非対応版の場合は面倒でもmbstowcs()でワイド文字に直してから addwstr() を使う必要がある。
概要
ソース
hjklで移動、qで終了UTF-8対応版
#include <ncurses.h> // -lncurses ではなくて -lncursesw をリンクすること
#include <locale.h> // setlocale()
int main()
{
setlocale( LC_ALL, "" );
initscr();
int x = 0;
int y = 0;
for(;;){
clear();
move( y, x );
addstr( "UTF-8対応" );
refresh();
switch( getch() ){
case 'q' : goto EXIT_FOR;
case 'k': --y; break;
case 'j': ++y; break;
case 'l': ++x; break;
case 'h': --x; break;
}
}
EXIT_FOR:
endwin();
return 0;
}
UTF-8非対応版
#include <ncursesw/ncurses.h> // addwstr(), -lncursesw
#include <stdlib.h> // mbstowcs()
#include <locale.h> // setlocale()
#define MAX_STR 256
void addutf8str( const char* str )
{
wchar_t dst[ MAX_STR ];
if( mbstowcs( dst, str, MAX_STR ) ) addwstr( dst );
}
int main()
{
setlocale( LC_ALL, "" );
initscr();
int x = 0;
int y = 0;
for(;;){
clear();
move( y, x );
addutf8str( "UTF-8非対応" );
refresh();
switch( getch() ){
case 'q' : goto EXIT_FOR;
case 'k': --y; break;
case 'j': ++y; break;
case 'l': ++x; break;
case 'h': --x; break;
}
}
EXIT_FOR:
endwin();
return 0;
}