如何使用ncurses在不同的终端上以相同的方式呈现Unicode?

时间:2018-04-17 19:04:22

标签: c++ unicode ncurses

我被告知使用ncursesthis帖子中由用户MGetz在我的程序中呈现unicode级别。所以,我已经更新了我的代码以使用ncurses,但它在不同的终端中仍然呈现不同的方式。我使用g++ file.cpp -o file -std=c++11 -lncursesw编译了我的代码。说到我的代码,就在这里。

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

using namespace std;

void PrintLevel();

string lvl[9][11] {
        {"\u250f", "\u2501", "\u2501", "\u2501", "\u2501", "\u2501", "\u2501", "\u2501", "\u2501", "\u2501", "\u2513"},
        {"\u2503", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u2503"},
        {"\u2503", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u2503"},
        {"\u2503", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u2503"},
        {"\u2503", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u2503"},
        {"\u2503", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u2503"},
        {"\u2503", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u2503"},
        {"\u2503", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u0020", "\u2503"},
        {"\u2517", "\u2501", "\u2501", "\u2501", "\u2501", "\u2501", "\u2501", "\u2501", "\u2501", "\u2501", "\u251B"}
    };

int main() {
    setlocale(LC_ALL, "");
    initscr();
    PrintLevel();
    getch();
    endwin();
}

void PrintLevel() {
    for(int i = 0; i<9; i++) {
        for(int j = 0; j<11; j++) {
            printw(lvl[i][j].c_str());
            if(j == 10) {
                printw("\n");
            }
        }
    }

    refresh();
}

哦,仅供参考,我正在使用Codenvy,因为我只能访问我的学校Chromebook。尝试在另一个类似Codenvy的服务Codeanywhere上,Unicode甚至无法正确呈现,我明白了:

codeanywhere output

1 个答案:

答案 0 :(得分:0)

副手:

  • 由于屏幕显示替换字符(那个奇怪的问号),人们会认为终端设置为UTF-8,但是
  • 您正在呼叫setlocale使用区域设置,但不要提及它们的内容(LC_ALL等,如 locale所示命令)和
  • 没有提及您是否正在使用 ncursesw (预期)或 ncurses 进行编译/链接。

如果您同时使用错误的区域设置并使用 ncurses 进行关联,则可能会显示该图片。