home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume3
/
libc_term
/
pick_one.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-11-30
|
3KB
|
133 lines
#include <curses.h>
#include <ctype.h>
#include <strings.h>
#include "c_term.h"
int pick_one(ChoiceList, ChoiceCount)
char **ChoiceList;
int ChoiceCount;
/*
---------------------------------------------------------------------------
Last revision -
6 January 1985 - GWS
Change to use curses
16 November 1984 - GWS
Ignore XON, XOFF
12 April 1984 - GWS
NAME
pick_one - "crash-proof" routine for picking one of a list of
list of strings
SYNOPSIS
int pick_one(ChoiceList, ChoiceCount)
char **ChoiceList;
int ChoiceCount;
DESCRIPTION
This routine prompts and nudges the user through selection of a
string from a table of strings - useful for choosing an item
from a menu for instance. The options are displayed one at a
time. The current item is selected by pressing return. The
space bar advances, the up-arrow backs up. The return
value of the function is the index of the chosen string.
SEE ALSO
DIAGNOSTICS
none
BUGS
none known
AUTHOR
George W. Sherouse
11 April 1984
---------------------------------------------------------------------------
*/
{
int c;
int val;
int biggest;
int temp;
int loop;
int where_y,
where_x;
char Format[80];
int strlen();
/*
In the silly case where there is only one choice, just print it
and return the index 0.
*/
if (ChoiceCount == 1)
{
printw("%s", ChoiceList[0]);
return(0);
}
/*
Find the longest string in the bunch
*/
biggest = strlen(ChoiceList[0]);
for (loop = 1; loop < ChoiceCount; loop++)
if ((temp = strlen(ChoiceList[loop])) > biggest)
biggest = temp;
getyx(stdscr, where_y, where_x);
standout();
for (loop = 0; loop < biggest; loop++)
printw(" ");
sprintf(Format, "%%-%ds", biggest);
val = 0;
while (1)
{
mvprintw(where_y, where_x, Format, ChoiceList[val]);
refresh();
switch (c = (getch() & 0177))
{
#ifdef ABORT_CHAR
case ABORT_CHAR:
clear();
standend();
mvprintw(0, 0, "Program aborted at your request...");
move(LINES - 1, 0);
refresh();
endwin();
exit(0);
break;
#endif ABORT_CHAR
case '\015':
standend();
mvprintw(where_y, where_x, Format, ChoiceList[val]);
refresh();
return(val);
case ' ':
if (++val == ChoiceCount)
val = 0;
break;
case '^':
if (--val < 0)
val = ChoiceCount - 1;
break;
case '\021':
case '\023':
break;
default:
fprintf(stderr, "%c", '\007');
}
}
}