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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, 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 |
5053
|
29 ## |
3408
|
30 ## @seealso{disp, printf, and input} |
4
|
31 |
2314
|
32 ## Author: jwe |
|
33 |
3979
|
34 function num = menu (t, varargin) |
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 for i = 1:nopt |
3426
|
62 printf (" [%2d] ", i); |
3979
|
63 disp (varargin{i}); |
917
|
64 endfor |
375
|
65 printf ("\n"); |
917
|
66 s = input ("pick a number, any number: ", "s"); |
3180
|
67 eval (sprintf ("num = %s;", s), "num = [];"); |
4030
|
68 if (! isscalar (num) || num < 1 || num > nopt) |
3426
|
69 printf ("\nerror: input invalid or out of range\n\n"); |
917
|
70 else |
3426
|
71 break; |
917
|
72 endif |
|
73 endwhile |
4
|
74 |
917
|
75 unwind_protect_cleanup |
|
76 |
|
77 page_screen_output = save_page_screen_output; |
|
78 |
|
79 end_unwind_protect |
4
|
80 |
|
81 endfunction |