简体   繁体   中英

strange extended characters in ncurses

I'm writing an ncurses application, and there's a strange issue with how special characters are printed to the screen. Here's an example:

#include <ncurses.h>

int main(int argc, char *argv[])
{
  initscr();
  noecho();
  keypad(stdscr, TRUE);
  cbreak();
  curs_set(0);

  addch(ACS_LARROW);
  addch(' ');
  addch(ACS_UARROW);
  addch(' ');
  addch(ACS_DARROW);
  addch(' ');
  addch(ACS_RARROW);
  addch(' ');

  refresh();

  getch();

  endwin();

  return 0;
}

So, when I run this on a tty, the characters are correctly printed as arrows (←, ↑, ↓, →), but when I try and run this on a terminal (I've tried on gnome-terminal and LXTerminal) this is the output:

< ^ v >

Is there any reason for this difference? I thought it might be font related, but I'm really out of my territory here, and my googling didn't help.

Any suggestion on how to force lxterminal (or any other terminal) to output the same characters of the tty?

ncurses decides which character to output for ACS_LARROW and friends based on your termtype. It's likely that in a tty your termtype is set to 'linux', whereas in gnome-terminal, etc it'll most likely be set to 'xterm'. Although I'm not certain, it's quite possible that the xterm termtype doesn't support these characters.

You could try running you application as so:

env TERM=linux ./a.out

Other termtypes to try are gnome, rxvt and vt102. These will output extended ASCII characters and your terminal emulator should support them. You could also try rxvt-unicode if you have it installed, that should output the correct unicode codepoints for these special symbols.

terminal emulations are sometimes coded in 7bit ASCII, and there is no corresponding Value for the arrows (with line and wedge) in this code page, so the terminal displays what comes near (that is: only the wedge). on tty you have all the capacities of you computer (UTF-8, color encoding, ...), so the terminal can draw the arrow.

The setting of TERM is largely irrelevant. What matters is

  • the locale
  • the choice of library
  • the particular terminal emulator

The usual reason for this is that the program is built-with and linked-to the ncurses library rather than ncursesw :

  • if you use the former ( ncurses ) in a locale using UTF-8, ncurses will use ASCII graphics simply because it cannot draw UTF-8. ncursesw can draw using UTF-8 characters .
  • using ncursesw , the program will use UTF-8 as needed (since 2002 , it can use either the terminfo information, or a built-in table of Unicode values). The Linux console and screen are known special cases where the terminfo cannot describe the behavior of the terminal. For others (terminals which do not support VT100 line-drawing in UTF-8 mode), you would set NCURSES_NO_UTF8_ACS .
  • the terminal database does in fact have an extended capability addressing the lack of VT100 compatibility vs UTF-8 (see U8 note in the terminal database). However, most people set TERM to xterm or linux and often equate those (see note in xterm FAQ ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM