You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NCURSES中光标间歇性可见问题求助

NCURSES中光标间歇性可见问题求助

各位好,我正在自学C17标准的C语言,最近在折腾ncurses库做小实验。

我写了一段代码,功能是先清屏,然后在随机的X、Y坐标位置绘制随机数量的彩色句点(为了方便排查,我把代码里的颜色相关逻辑去掉了)。

我用curs_set(0)来隐藏光标,大部分时候(大概90-95%的情况)都能正常隐藏,但偶尔光标还是会莫名其妙地显示出来,这个问题挺间歇性的,不好稳定复现。

我分别从Windows 11和Mac Mini 2通过SSH连接到Debian 11服务器运行这段代码,两边都出现了同样的问题。

有没有大佬遇到过类似情况,或者能给点排查思路?

以下是能复现问题的源码:

#include <ncurses.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
        initscr();
        curs_set(0);
        timeout(0);
        nodelay(stdscr, TRUE);
        start_color();
        bkgd(COLOR_PAIR(0));
        for (int i = 0; i < (rand() % (COLS * 2)) + COLS / 2; i++) {
                char ch;
                int x, y;
                do {
                        y = rand() % LINES;
                        x = rand() % COLS;
                        ch = mvinch(y, x) & A_CHARTEXT;
                } while (ch != ' ');
                curs_set(0);
                mvprintw(y, x, ".");
                curs_set(0);
        }
        curs_set(0);
        refresh();
        curs_set(0);
        struct timespec req = {2, 0};
        int result = nanosleep(&req, NULL);
        timeout(1);
        nodelay(stdscr, FALSE);
        clear();
        endwin();
        flushinp();
        return 0;
}

备注:内容来源于stack exchange,提问作者Timothy B.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 11:09:52