gpt4 book ai didi

c - Ncurses 在光标后显示不需要的文本

转载 作者:太空宇宙 更新时间:2023-11-04 08:41:25 26 4
gpt4 key购买 nike

我昨天问了一个问题,你们帮了大忙。之后又出现了另一个问题,这次是关于 ncurses 的。每当我四处移动箭头 (<) 时,光标后就会弹出不需要的文本。 Image of ncurses output

现在的箭头在 p 右边的第一列。似乎我按下的任何键都会出现在光标之后。如果有不同,我会在 Cygwin 中运行该程序。

代码:

    #include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <ncurses/ncurses.h>

#define MAXWORDS 100
#define WORDLEN 11
#define DEBUG 0 // set to 0 to disable debug output

#define ARROW '<'
#define BLANK ' '

//PROTOTYPES
int readWords(char *wl[MAXWORDS], char* filename); // reads words from the file into wl and trims the whitespace off of the end
void trimws(char* s); //modifies s to trim white space off the right side
void printWords(char* words[20][5], int wordCount, int rows, int columns);
void draw_character(int x, int y, char use);
void read_dir(int* time, int* b_down, int* b_up, int* b_left, int * b_right, int* dir_down, int* dir_up, int* dir_left, int* dir_right);
void printSentence(char* sentence[MAXWORDS], int position);

struct arrowlocation{
int row;
int column;
}arrowloc;

int main(int argc, char* argv[]) {
char* wordlist[MAXWORDS];
char* sentence[MAXWORDS];
char space[]=" \0";
int wordCount;
int i=0,j=0,k=0;
int columns=5, rows;
int row=0,column=16; //initial position for '<'
int prevrow, prevcolumn;
int time, b_down, b_up, b_left, b_right, dir_down, dir_up, dir_left, dir_right;
int position=0;
wordCount = readWords(wordlist, argv[1]);

struct arrowlocation arrowloc;
arrowloc.row=0;
arrowloc.column=0;

//DEBUGGING
if (DEBUG) {
printf("Read %d words from %s \n",wordCount, argv[1]);
for (i = 0; i< wordCount; i++) {
printf("%s,", wordlist[i]);
}
printf("\n\n");
}

//Calculates number of rows needed
if ((((double)wordCount)/columns)>((double)(wordCount/columns))){
rows=(wordCount/5)+1;
}
else{
rows=(wordCount/5);
}

char* words[rows][columns];

//Converts the list of words to a 2D array table
for(i=0; i<rows; i++){
for(j=0; j<columns; j++){
if (k==wordCount){
break;
}
words[i][j]=wordlist[k];
k++;
}
}

//Starts ncurses screen
initscr();
refresh();


printWords(words, wordCount, rows, columns);
refresh();

draw_character(row, column, ARROW);

int c=0;
do{
refresh();
c = wgetch(stdscr);

refresh();
switch(c){
case 66:
//move the arrow down one
if((arrowloc.row)<20){
draw_character(row, column, BLANK);
row=row+1;
arrowloc.row=arrowloc.row+1;
draw_character(row, column, ARROW);

//mvprintw(27, 0, "arrow loc; row:%d column:%d",arrowloc.row, arrowloc.column);
refresh();
}
break;
case 65:
//move the arrow up one
if((arrowloc.row)>0){
draw_character(row, column, BLANK);
row=row-1;
arrowloc.row=arrowloc.row-1;
draw_character(row, column, ARROW);

//mvprintw(27, 0, "arrow loc; row:%d column:%d",arrowloc.row, arrowloc.column);
refresh();
}
break;
case 68:
//move the arrow left one
if((arrowloc.column)>0){
draw_character(row, column, BLANK);
column=column-15;
draw_character(row, column, ARROW);

arrowloc.column--;
//mvprintw(27, 0, "arrow loc; row:%d column:%d",arrowloc.row, arrowloc.column);
}
break;
case 67:
//move the arrow right one
if((arrowloc.column)<5){
draw_character(row, column, BLANK);
column=column+15;
draw_character(row, column, ARROW);

arrowloc.column++;
//mvprintw(27, 0, "arrow loc; row:%d column:%d",arrowloc.row, arrowloc.column);
}
break;
case 113:
//Left button adds word to sentence without space at end
//if (b_left==1){
sentence[position]=words[arrowloc.row][arrowloc.column];
position++;
printSentence(sentence, position);
//}
break;
case 119:
//Up button adds word to sentence with a space at the end
//if (b_up==1){
sentence[position]=words[arrowloc.row][arrowloc.column];
position++;
sentence[position]=&space[0];
position++;
printSentence(sentence, position);
//}
break;
case 101:
//Down button acts as a backspace
//else if (b_down==1){
position--;
printSentence(sentence, position);
//}
break;
default:
//mvprintw(24, 0, "Charcter pressed is = %3d , c, c);
refresh();
break;
}



} while(1);


endwin();

}

void printSentence(char* sentence[MAXWORDS], int position){
int i=0, x=0;
//clrtoeol();
for(i=0; i<position; i++){
mvprintw(22, x, "%s", sentence[i]);
//mvprintw(23, x, "%d", i);
x+=strlen(sentence[i]);
}
refresh();

}

void printWords(char* words[20][5], int wordCount, int rows, int columns){
int k=0, i=0, j=0;
clear();
for(i=0; i<rows; i++){
for(j=0; j<columns; j++){
if (k<wordCount){
printw("%15s", words[i][j]);
k++;
}
}
printw("\n");
}
refresh();
}

void draw_character(int x, int y, char use){
mvaddch(x,y,use);
refresh();
}

void read_dir(int* time, int* b_down, int* b_up, int* b_left, int * b_right, int* dir_down, int* dir_up, int* dir_left, int* dir_right) {
int dummy;
scanf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d,", time, b_down, b_up, b_left, b_right, dummy, dummy, dir_down, dir_up, dir_left, dir_right, dummy, dummy);
}

void trimws(char* s) {
int len = strlen(s) ;
int x;
if (len ==0) return;
x = len-1;
while (isspace(s[x]) && (x>=0)) {
s[x] = '\0';
x--;
}
}

int readWords(char* wl[MAXWORDS], char* filename) {
int numread =0;
char line[WORDLEN];
char *p;
FILE* fp = fopen(filename,"r");
while (!feof(fp)) {
p =fgets(line, WORDLEN, fp);
if (!feof(fp) && p !=NULL) {
trimws(line);
wl[numread] = (char *) malloc(strlen(line)+1);
strcpy(wl[numread], line);
numread++;
}
}
fclose(fp);
return numread;
}

如果您想自己编译并尝试,您将需要此文本文件 http://pastebin.com/xk0MmkEm您可能必须将案例结构中的数字替换为您的机器才能工作的数字。

运行它只是./program.exe wordslist.txt

最佳答案

<ESC>[B 以来,您的终端定义似乎是正确的 CygWin 的光标向下转义序列,并且在运行代码时,它确实实际移动了光标。

然而,这些转义序列也出现的事实意味着它们来自某个地方,结果证明它们来自哪里是等式的输入端,而不是输出端。

当您按下向下箭头时,转义序列首先回显到屏幕,然后对输入进行操作,在输出 < 之前将光标移动到正确的位置。性格。

因此您需要在 initscr() 之后立即关闭 echo 调用:

initscr();
noecho(); // This line added.
refresh();

这将防止输入被回显。

现在您可能会在某些时候依赖回声完成(例如,对于常规字符),但我发现通常最好完全断开输入与输出的连接并在程序本身中进行映射。这样,您可以根据需要使映射变得简单或复杂。

换句话说,如果有人输入 e , 确保 echo 关闭并输出 e你自己而不是依靠 echo 为你做这件事。

关于c - Ncurses 在光标后显示不需要的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23378930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com