我刚刚完成了一个CLI程序的代码,用ncurses把它转换成一个TUI程序。
它以类似于闪存卡的方式测试用户的问题和答案的集合。
一切都比较顺利,除了我更换了许多的 printf()
呼叫 popupinfo(int colour,char * title, char * body)
函数来弹出一个窗口。这个函数使用这些函数。
int textwidth (char * text);//returns the width of a given string (which may include newlines) in chars when displayed without wrapping (for purposes of determining optimum window width)
int textheight (char * text, int width);//returns the height of a given string (which may include newlines) in lines when displayed wrapped to the given width (for purposes of determining optimum window width)
计算窗口的大小,然后再使用... wprintw()
来打印到该窗口。
我遇到的问题是,当最后一行以外的其他行的长度正好等于窗口宽度(或窗口宽度的倍数)时,窗口中会省略一行或多行文本。
例如,当最后一行的长度正好等于窗口宽度(或窗口宽度的倍数)时,窗口中会省略一行或多行文字。
Answer:
Foobarbaz.
将正确打印,但在。
Answer:
Foo.
"Foo. "没有被打印出来。
我相信这是因为 wprintw()
函数在打印后将光标移动到新的行。(window_width)
chars,但又遇到了刚刚打印出来的行末的换行符。
有谁知道有什么办法(除了自己写一个完整的函数来处理输出)来阻止这种情况发生?
有用的细节。
我把:
printf("\nSorry, the correct answer is:\n\n\t%s\n\n",currententry->answer);
替换成:
sprintf(passingstring,"The correct answer is:\n\n%s",currententry->answer);
popupinfo(3,"Sorry!",passingstring);
popupinfo被定义为:
void popupinfo(int colour,char * title,char * message)//pops up a window with the given colour, title and text
{
WINDOW * wbpopup = NULL, * wpopup = NULL;
PANEL * ppopup = NULL;
int width, height;
width=textwidth(message);
getmaxyx(stdscr,nlines,ncols);
if (width>ncols-16)width=ncols-16;
height=textheight(message,width)+4;
width+=8;
if (!(wbpopup = newwin(height,width,(nlines-height)/2,(ncols-width)/2))) outofmemory();
ppopup = new_panel(wbpopup);
wattrset(wbpopup,COLOR_PAIR(colour));
werase(wbpopup);
wbkgd(wbpopup,COLOR_PAIR(colour));
box(wbpopup,0,0);
windowtitle(wbpopup,title);
wpopup = innerwindow(wbpopup);
wprintw(wpopup,message);
update_panels();
doupdate();
wgetch(wpopup);
delwin(wpopup);
del_panel(ppopup);
delwin(wbpopup);
update_panels();
doupdate();
}
同样有用的是:
int textwidth (char * text)//returns the width of a given string (which may include newlines) in chars when displayed without wrapping (for purposes of determining optimum window width)
{
int i=0,j=0,k=0;
while (text[i]!='\0')
{
if (text[i]=='\n')
{
k=j>k?j:k;
j=0;
}
else j++;
i++;
}
k=j>k?j:k;
return k;
}
和
int textheight (char * text, int width)//returns the height of a given string (which may include newlines) in lines when displayed wrapped to the given width (for purposes of determining optimum window width)
{
int i=0,j=0,k=1;
while (text[i]!='\0')
{
if (text[i]=='\n')
{
k++;
j=0;
}
else j++;
if (j>width)
{
k++;
j=1;
}
i++;
}
return k;
}
其他功能。
WINDOW * innerwindow(WINDOW * outerwindow);//creates an area within another window for purposes of displaying text with a margin
void windowtitle(WINDOW * window, char * title);//writes the given string to the given window (top centre)
更多内容请参见CLI和ncurses版本的完整源码,可以在以下网站找到。http:/github.commegamasha。
你说的完全正确。
我相信这是因为wprintw()函数在打印(window_width)字符后将光标移动到了新的行,但是却遇到了刚刚打印的行末的换行符。
关于你的问题
有谁知道有什么办法(除了自己写一个完整的函数来处理输出)来阻止这种情况发生?
- 没有这样的方法,因为你所观察到的是在ncurses中行包装的工作方式,你可以做的是让弹出窗口宽一个字符,从而避免由于达到窗口宽度而导致的包装,例如通过改变行 width+=8;
到 width+=8+1;
在 popupinfo
.