]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/menu.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / menu.m
1 ## Copyright (C) 1993-2012 John W. Eaton
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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} menu (@var{title}, @var{opt1}, @dots{})
21 ## Print a title string followed by a series of options.  Each option will
22 ## be printed along with a number.  The return value is the number of the
23 ## option selected by the user.  This function is useful for interactive
24 ## programs.  There is no limit to the number of options that may be passed
25 ## in, but it may be confusing to present more than will fit easily on one
26 ## screen.
27 ## @seealso{disp, printf, input}
28 ## @end deftypefn
29
30 ## Author: jwe
31
32 function num = menu (title, varargin)
33
34   if (nargin < 2)
35     print_usage ();
36   endif
37
38   ## Force pending output to appear before the menu.
39
40   fflush (stdout);
41
42   ## Don't send the menu through the pager since doing that can cause
43   ## major confusion.
44
45   page_screen_output (0, "local");
46
47   if (! isempty (title))
48     disp (title);
49     printf ("\n");
50   endif
51
52   nopt = nargin - 1;
53
54   while (1)
55     for i = 1:nopt
56       printf ("  [%2d] ", i);
57       disp (varargin{i});
58     endfor
59     printf ("\n");
60     s = input ("pick a number, any number: ", "s");
61     num = sscanf (s, "%d");
62     if (! isscalar (num) || num < 1 || num > nopt)
63       printf ("\nerror: input invalid or out of range\n\n");
64     else
65       break;
66     endif
67   endwhile
68
69 endfunction
70