为什么我不能正常使用 ncurses 替代字符集?

Why isn't my use of ncurses alternative charset working properly?

我正在尝试编写一个生成填字游戏网格的程序,所以我使用 ncurses 库,因为我只需要一个简单的界面来显示网格,问题是当我使用 box() 函数时ACS_VLINE和ACS_HLINE,都不起作用;它写 'q' 和 'x' 而不是方框线。它在开始时有效,但突然停止工作;我不知道为什么。 我只是用 initscr() 和 noecho() 初始化 ncurses。 这是我绘制框的代码部分:


int crossword(char ***grid, WINDOW **win, WINDOW ****c, int x, int y)
{
  int i;
  int j;
  int ch;
  t_word_list *wrdlist;

  clear();
  (void)x;
  (void)y;
  if (!(wrdlist = get_words("data/words.list")))
    return (-1);
  box(*win, ACS_VLINE, ACS_HLINE);
  i = -1;
  while ((*c)[++i])
  {
    j = -1;
    while ((*c)[i][++j])
      mvwaddch((*c)[i][j], 0, 0, (*grid)[i][j]);
  }
  wrefresh(stdscr);
  while ((ch = getch()))
    if (ch == 27)
    {
      nodelay(stdscr, TRUE);
      ch = getch();
      nodelay(stdscr, FALSE);
      if (ch < 0)
        break ;
      }
  return (endwin());
}

输出:

 lqqqqqqqqqqqqqqqqqqqqqk
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 x 0 0 0 0 0 0 0 0 0 0 x
 mqqqqqqqqqqqqqqqqqqqqqj

编辑:我用最少的代码重现了问题:

#include <curses.h>

int     main(void)
{
  WINDOW *win;
  
  initscr();
  win = subwin(stdscr, 10, 10, 1, 1);
  box(win, ACS_VLINE, ACS_HLINE);
  wrefresh(stdscr);
  getch();
  return (0);
}

输出:

 lqqqqqqqqk
 x        x
 x        x
 x        x
 x        x
 x        x
 x        x
 x        x
 x        x
 mqqqqqqqqj

我用于编译的标志:gcc main.c -lcurses

正在将部分评论转换为答案。

你在它工作和停止工作之间做了什么改变? …你能重新创建你在新的 MCVE 中创建工作版本时使用的步骤吗(Minimal, Complete, Verifiable Example — 或 MRE 或 SO 现在使用的任何名称) 或一个 SSCCE(Short, Self-Contained, Correct Example)。 这将使您能够用最少的代码找出破坏工作代码的原因?

… I just edited my atoi function that I just use in the main for the sizeX and sizeY; I didn't touch anything else and it suddenly stopped working. I tried to undo what I did after it wasn't working and it still doesn't work.

所以你也改变了一些东西,不管你是否意识到。终端设置有可能被搞砸了——更有趣的事情已经为人所知。您是否尝试过创建一个新终端 window 并在新 window 中重试?

Oh yes! It was the terminal! It worked after a 'reset', thank you! I don't know why I didn't think about that earlier.

在 curses (ncurses) 程序证明自己可靠之前,请始终考虑被测程序的有缺陷版本搞乱终端设置的可能性。在终端正常工作时使用 stty -g 生成字符串(首次创建时,在您 运行 您的程序之前)。然后,您可以使用该字符串将终端重置为相同的已知状态(假设问题出在 stty 设置上)。有时,即使如此,也需要一个新终端 window。

good_stty=$(stty -g)

…testing program under development…

stty "$good_stty"

有时,您可能需要输入 control-J,然后输入 stty "$good_stty"(或 stty sane)和另一个 control-J 因为行尾设置被修改,没有正确恢复。

问题可能出在您的控制台的控制台编码上。此外,如果您从 putty 访问,则必须按照以下步骤操作。

验证控制台配置是否为 UTF

dpkg-reconfigure console-setup