2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
245
|
19 |
3381
|
20 ## -*- texinfo -*- |
3372
|
21 ## @deftypefn {Function File} {} menu (@var{title}, @var{opt1}, @dots{}) |
|
22 ## Print a title string followed by a series of options. Each option will |
|
23 ## be printed along with a number. The return value is the number of the |
|
24 ## option selected by the user. This function is useful for interactive |
|
25 ## programs. There is no limit to the number of options that may be passed |
|
26 ## in, but it may be confusing to present more than will fit easily on one |
|
27 ## screen. |
|
28 ## @end deftypefn |
|
29 |
2311
|
30 ## See also: disp, printf, input |
4
|
31 |
2314
|
32 ## Author: jwe |
|
33 |
2311
|
34 function num = menu (t, ...) |
4
|
35 |
211
|
36 if (nargin < 2) |
904
|
37 usage ("menu (title, opt1, ...)"); |
4
|
38 endif |
|
39 |
2303
|
40 ## Force pending output to appear before the menu. |
4
|
41 |
|
42 fflush (stdout); |
|
43 |
2303
|
44 ## Don't send the menu through the pager since doing that can cause |
|
45 ## major confusion. |
4
|
46 |
|
47 save_page_screen_output = page_screen_output; |
917
|
48 |
|
49 unwind_protect |
|
50 |
2360
|
51 page_screen_output = 0; |
4
|
52 |
917
|
53 if (! isempty (t)) |
|
54 disp (t); |
|
55 printf ("\n"); |
|
56 endif |
4
|
57 |
917
|
58 nopt = nargin - 1; |
4
|
59 |
917
|
60 while (1) |
|
61 va_start (); |
|
62 for i = 1:nopt |
|
63 printf (" [%2d] ", i); |
|
64 disp (va_arg ()); |
|
65 endfor |
375
|
66 printf ("\n"); |
917
|
67 s = input ("pick a number, any number: ", "s"); |
3180
|
68 eval (sprintf ("num = %s;", s), "num = [];"); |
917
|
69 if (! is_scalar (num) || num < 1 || num > nopt) |
|
70 printf ("\nerror: input invalid or out of range\n\n"); |
|
71 else |
|
72 break; |
|
73 endif |
|
74 endwhile |
4
|
75 |
917
|
76 unwind_protect_cleanup |
|
77 |
|
78 page_screen_output = save_page_screen_output; |
|
79 |
|
80 end_unwind_protect |
4
|
81 |
|
82 endfunction |