home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / miscellaneous / menu.m < prev    next >
Text File  |  1999-12-15  |  2KB  |  83 lines

  1. ## Copyright (C) 1996, 1997 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 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.
  19.  
  20. ## -*- texinfo -*-
  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.  
  30. ## See also: disp, printf, input
  31.  
  32. ## Author: jwe
  33.  
  34. function num = menu (t, ...)
  35.  
  36.   if (nargin < 2)
  37.     usage ("menu (title, opt1, ...)");
  38.   endif
  39.  
  40.   ## Force pending output to appear before the menu.
  41.  
  42.   fflush (stdout);
  43.  
  44.   ## Don't send the menu through the pager since doing that can cause
  45.   ## major confusion.
  46.  
  47.   save_page_screen_output = page_screen_output;
  48.  
  49.   unwind_protect
  50.  
  51.     page_screen_output = 0;
  52.  
  53.     if (! isempty (t))
  54.       disp (t);
  55.       printf ("\n");
  56.     endif
  57.  
  58.     nopt = nargin - 1;
  59.  
  60.     while (1)
  61.       va_start ();
  62.       for i = 1:nopt
  63.     printf ("  [%2d] ", i);
  64.     disp (va_arg ());
  65.       endfor
  66.       printf ("\n");
  67.       s = input ("pick a number, any number: ", "s");
  68.       eval (sprintf ("num = %s;", s), "num = [];");
  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
  75.  
  76.   unwind_protect_cleanup
  77.  
  78.     page_screen_output = save_page_screen_output;
  79.  
  80.   end_unwind_protect
  81.  
  82. endfunction
  83.