Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NCURSES border printing as q, x, when using UTF-8 and PuTTY

Tags:

c++

putty

ncurses

I am trying to learn ncurses to add some functionality to my programs however I cannot seem to get my terminal settings to the point where the default ncurses window border shows up as expected.

Here is the output I am getting for a window with a box border:

  lqqqqqqqqk
  x        x
  x        x
  x        x
  mqqqqqqqqj

However I should be getting this:

  ┌────────┐
  │        │
  │        │
  │        │
  └────────┘

The only thing I am able to find that fixes this issue is setting my PuTTY Remove Character Set to be Latin-1 instead of UTF-8, however this messes up all of my other applications including VIM.

There were some related SO questions that I found (1 and 2) however neither of their solutions help me. The only interesting thing I pulled out of the second one is that if I run printf '\342\224\224\342\224\200\342\224\220' in my command line it prints out └─┐ (which is correct...).

Here is the simple program I am using to test this:

 #include <iostream>
 #include <ncurses.h>
 #include <string>
 #include <cstring>

 int main() {
     WINDOW *my_win;
     int startx, starty, width, height;
     int ch;

     initscr();
     cbreak();
     keypad(stdscr, TRUE);
     refresh();

     int height = 5;
     int width = 10;
     my_win = newwin(height, width, 1, 2);
     box(my_win, 0, 0);
     wrefresh(my_win);

     getch();

     endwin();
     return 0;
 }

Any ideas what I could be doing wrong? Thanks!

like image 400
wakey Avatar asked Dec 30 '16 01:12

wakey


1 Answers

You're not initializing the locale. Without that, ncurses will assume that it can use the terminal description.

Further reading:

  • ncurses manual page, Initialization:

The library uses the locale which the calling program has initialized. That is normally done with setlocale:

setlocale(LC_ALL, "");

If the locale is not initialized, the library assumes that characters are printable as in ISO-8859-1, to work with certain legacy programs. You should initialize the locale and not rely on specific details of the library when the locale has not been setup.

  • ncurses manual page, NCURSES_NO_UTF8_ACS:

During initialization, the ncurses library checks for special cases where VT100 line-drawing (and the corresponding alternate character set capabilities) described in the terminfo are known to be missing. Specifically, when running in a UTF-8 locale, the Linux console emulator and the GNU screen program ignore these. Ncurses checks the TERM environment variable for these. For other special cases, you should set this environment variable.

like image 116
Thomas Dickey Avatar answered Nov 04 '22 19:11

Thomas Dickey