-1

I want to write UTF-8 Chars onto the terminal, but despite of trying everything I found here on stack overflow it doesn't work.

I build my program with the command "cc main.c -lncursesw" and I include ncurses/curses.h, wchar.h and locale.h.
As described in here (How to make ncurses display UTF-8 chars correctly in C?) I set the locale. I cannot access any function to print wide character and doing it prints two undesired ASCII chars. The (I think) relevant code:

#define unsetcolor(color) attroff(COLOR_PAIR(color))
#define to_full_color(f, b) (b << 3) | f | 64
#define setcolor(color) attron(COLOR_PAIR(color))

void init_colorpairs()
{
    for(int f = 0; f < 8; f++)
    {
        for(int b = 0; b < 8; b++)
        {
                        //I just do color things 
            int color_pair = to_full_color(f, b);
            init_pair(color_pair, f, b);
        }
    }
}

int main()
{
    setlocale(LC_ALL, "");
    WINDOW* main_win = initscr();
    if(has_colors() == FALSE)
    {
        return -1;
    }
    start_color();
    init_colorpairs();
    

    //window size x, y
    int win_s_x, win_s_y;
    getmaxyx(main_win, win_s_y, win_s_x);
    //curs_set(0);
    //0=succsess, -1=error
    
    attron(A_BOLD);

    for(int i = 0; i < (win_s_x * win_s_y); i++)
    {
        u8 color = (i&63)|64;
        setcolor(color);
                //My try to print an UTF-8 char, i also searched for addchw or mvaddv
        addch(9617);
        
        unsetcolor(color);
    }
    refresh();
    attroff(A_BOLD);
    
    //refresh();
    getch();
    
    endwin();
    return 0;
}

1 Answers1

0

I found out what went wrong. Event though ncurses is a C library, it has only very limited unicode support. The only (at least for me) working functions with the desired symbols are printw and addch_wch (printing and compilation succsess, not really what I needed but they worked). For more functions like mvaddch_wch you should use ncursesw (the ncursesw/ncurses.h header). This is an C++ header, so the solution is to compile with a C++ compiler or write some sort of wrapper.

Further more I want to add how I used the for me needed function, in case someone is too new to ncurses and has the same problems.

extern "C" 
{
#include<ncursesw/ncurses.h>
}

/*somewhere in the main function*/
//set the character and it's attribute, the char value can be representet as an integer number
cchar_t c = { A_BOLD | COLOR_PAIR(1), 9617};
//the char is given as a reference, and the coordinates as values
mvadd_wch(1, 10, &c);