Search code examples
cwindowsgccmingwncurses

Get unicode character on the screen ncurses


I'm facing a problem with getting a U+2588 █ character from the screen by using the winch().

Let's assume I have a window called win and it looks like this:

###████###

I want to redraw all # characters with new attributes applied on them and ignore the █ characters. Somehow I can't do that because if (winch(win) == ACS_BLOCK) check fails constantly.

For anyone looking for the full code, here it is:

void highlight_tile(const Program *program, const int row, const int col) {
  wmove(program->board->win, TILE_HEIGHT * row + 1, TILE_WIDTH * col + 1);
  for (int i = 0; i < TILE_HEIGHT; i++) {
    for (int j = 0; j < TILE_WIDTH; j++) {
      if (winch(program->board->win) != ACS_BLOCK) {
        if ((row + col) % 2 == 0) {
          wattron(program->board->win, SELECTION);
          wprintw(program->board->win, "#");
          wattroff(program->board->win, SELECTION);
        } else {
          wattron(program->board->win, SELECTION);
          wprintw(program->board->win, "~");
          wattroff(program->board->win, SELECTION);
        }
      }
    }
    wmove(program->board->win, getcury(program->board->win) + 1, getcurx(program->board->win) - TILE_WIDTH);
  }
}

I've tried changing the condition like this:

  1. if (winch(win) == L'\u2588')
  2. if (winch(win) == '█')
  3. if ((winch(win) & A_CHARTEXT) == L'\u2588')
  4. if ((winch(win) & A_CHARTEXT) == '█')

None of them worked properly.


Solution

  • winch uses only one byte of the character. Use win_wch for Unicode (wide-characters), along with getcchar