概述
#include static FILE *output_stream = (FILE *)0;
char *menu[] = {
“a - add new record”,
“d - delete record”,
“q - quit”,
NULL,
};
int getchoice(char *greet, char *choices[], FILE *in, FILE *out);
int char_to_terminal(int char_to_write);
int main()
{
...
}
int getchoice(char *greet, char *choices[], FILE *in, FILE *out)
{
int chosen = 0;
int selected;
int screenrow, screencol = 10;
char **option;
char *cursor, *clear;
output_stream = out;
setupterm(NULL,fileno(out), (int *)0);
cursor = tigetstr(“cup”);
clear = tigetstr(“clear”);
screenrow = 4;
tputs(clear, 1, (int *) char_to_terminal);
tputs(tparm(cursor, screenrow, screencol), 1, char_to_terminal);
fprintf(out, “Choice: %s, greet);
screenrow += 2;
option = choices;
while(*option) {
tputs(tparm(cursor, screenrow, screencol), 1, char_to_terminal);
fprintf(out,”%s”, *option);
screenrow++;
option++;
}
fprintf(out, “n”);
do {
fflush(out);
selected = fgetc(in);
option = choices;
while(*option) {
if(selected == *option[0]) {
chosen = 1;
break;
}
option++;
}
if(!chosen) {
tputs(tparm(cursor, screenrow, screencol), 1, char_to_terminal);
fprintf(out,”Incorrect choice, select againn”);
}
} while(!chosen);
tputs(clear, 1, char_to_terminal);
return selected;
}
int char_to_terminal(int char_to_write)
{
if (output_stream) putc(char_to_write, output_stream);
return 0;
}
工作原理
重写的getchoice函数实现了与我们前面的例子中相同的菜单,但是输出函数进行了修改从而来使用terminfo功能。如果我们希望在屏幕被清除之前看到You have chosen:信息停留一会,可以使用下面的选择,在main函数中添加一个sleep调用:
do {
choice = getchoice(“Please select an action”, menu, input, output);
printf(“nYou have chosen: %cn”, choice);
sleep(1);
} while (choice != ‘q’);
这个程序中的最后一个函数,char_to_terminal,包含了一个我们在第3章提到的putc函数调用。
要结束这一章,我们将会看一个如何检测击键的例子。
最后
以上就是友好高山为你收集整理的linux+terminfo工具,Linux终端(三)的全部内容,希望文章能够帮你解决linux+terminfo工具,Linux终端(三)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复