7017
|
1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, |
|
2 ## 2004, 2005, 2006, 2007 John W. Eaton |
2313
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
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. |
5642
|
28 ## @seealso{disp, printf, input} |
3372
|
29 ## @end deftypefn |
4
|
30 |
2314
|
31 ## Author: jwe |
|
32 |
3979
|
33 function num = menu (t, varargin) |
4
|
34 |
211
|
35 if (nargin < 2) |
6046
|
36 print_usage (); |
4
|
37 endif |
|
38 |
2303
|
39 ## Force pending output to appear before the menu. |
4
|
40 |
|
41 fflush (stdout); |
|
42 |
2303
|
43 ## Don't send the menu through the pager since doing that can cause |
|
44 ## major confusion. |
4
|
45 |
6521
|
46 save_page_screen_output = page_screen_output (); |
917
|
47 |
|
48 unwind_protect |
|
49 |
6521
|
50 page_screen_output (0); |
4
|
51 |
917
|
52 if (! isempty (t)) |
|
53 disp (t); |
|
54 printf ("\n"); |
|
55 endif |
4
|
56 |
917
|
57 nopt = nargin - 1; |
4
|
58 |
917
|
59 while (1) |
|
60 for i = 1:nopt |
3426
|
61 printf (" [%2d] ", i); |
3979
|
62 disp (varargin{i}); |
917
|
63 endfor |
375
|
64 printf ("\n"); |
917
|
65 s = input ("pick a number, any number: ", "s"); |
3180
|
66 eval (sprintf ("num = %s;", s), "num = [];"); |
4030
|
67 if (! isscalar (num) || num < 1 || num > nopt) |
3426
|
68 printf ("\nerror: input invalid or out of range\n\n"); |
917
|
69 else |
3426
|
70 break; |
917
|
71 endif |
|
72 endwhile |
4
|
73 |
917
|
74 unwind_protect_cleanup |
|
75 |
6521
|
76 page_screen_output (save_page_screen_output); |
917
|
77 |
|
78 end_unwind_protect |
4
|
79 |
|
80 endfunction |