简体   繁体   English

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

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

I'm trying to write a program that generates a crossword grid, so I'm using the ncurses library because I just need a simple interface to display the grid, the problem is when I use box() function with ACS_VLINE and ACS_HLINE, it doesn't work;我正在尝试编写一个生成填字游戏网格的程序,所以我使用 ncurses 库,因为我只需要一个简单的界面来显示网格,问题是当我使用 box() function 和 ACS_VLINE 和 ACS_HLINE 时,它不工作; it writes 'q' and 'x' instead of the box lines.它写 'q' 和 'x' 而不是方框线。 It worked at the beginning but suddenly it stopped working;它一开始工作,但突然停止工作; I don't know why.我不知道为什么。 I'm simply initializing ncurses with initscr() and noecho().我只是用 initscr() 和 noecho() 初始化 ncurses。 Here's the part of the code where I draw the box:这是我绘制框的代码部分:


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());
}

Output: Output:

 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

EDIT: I recreated the problem with minimal code:编辑:我用最少的代码重新创建了问题:

#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);
}

Output: Output:

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

The flags I use for compilation: gcc main.c -lcurses我用于编译的标志: gcc main.c -lcurses

Converting parts of comments into an answer.将部分评论转换为答案。

What did you change between when it worked and when it stopped working?在它工作和停止工作之间你有什么变化? … Can you recreate the steps you would have used while creating the working version in a new MCVE ( Minimal, Complete, Verifiable Example — or MRE or whatever name SO now uses) or an SSCCE ( Short, Self-Contained, Correct Example ). ...您能否重新创建在新 MCVE(最小、完整、可验证示例- 或 MRE 或 SO 现在使用的任何名称)或 SSCCE(简短、自包含、正确示例)中创建工作版本时使用的步骤。 This would allow you to find out what breaks the working code with the bare minimum of code?这将允许您用最少的代码找出是什么破坏了工作代码?

… I just edited my atoi function that I just use in the main for the sizeX and sizeY ; …我刚刚编辑了我的atoi function ,我只是主要用于sizeXsizeY 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.我试图撤消我在它不起作用后所做的事情,但它仍然不起作用。

So you changed something else as well, whether or not you realized it.所以你也改变了其他东西,不管你是否意识到。 It's possible that the terminal settings are screwed up — funnier things have been known.终端设置可能被搞砸了——更有趣的事情已经知道了。 Have you tried creating a new terminal window and trying again in the new window?您是否尝试过创建新的终端 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.我不知道为什么我没有早点想到这一点。

Until curses (ncurses) programs have proved themselves reliable, always consider the possibility that a flawed version of the program under test messed up the terminal settings.在 curses (ncurses) 程序证明自己是可靠的之前,请始终考虑被测程序的有缺陷版本破坏终端设置的可能性。 Use stty -g to generate a string when the terminal is working properly (when first created, before you run your program).当终端正常工作时(首次创建时,在运行程序之前),使用stty -g生成字符串。 You can then use that string to reset the terminal to the same known state (assuming it is stty settings that are the problem).然后,您可以使用该字符串将终端重置为相同的已知 state(假设问题是stty设置)。 Sometimes, a new terminal window is necessary even so.有时,即使这样,也需要一个新的终端 window。

good_stty=$(stty -g)

…testing program under development…

stty "$good_stty"

Sometimes, you may need to type control-J and then stty "$good_stty" (or stty sane ) and another control-J because the line ending settings have been modified and not restored correctly.有时,您可能需要键入control-J ,然后键入stty "$good_stty" (或stty sane )和另一个control-J ,因为行尾设置已被修改且未正确恢复。

The problem may be the console encoding for your console.问题可能是您的控制台的控制台编码。 Also if you access from putty you must follow the following steps.此外,如果您从 putty 访问,则必须按照以下步骤操作。

即使在 UTF-8 模式下也启用 VT100 线条绘制

Verif console configuration is UTF验证控制台配置为 UTF

dpkg-reconfigure console-setup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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