comparison scripts/miscellaneous/what.m @ 7050:bd56a0609c4f

[project @ 2007-10-23 12:02:17 by dbateman]
author dbateman
date Tue, 23 Oct 2007 12:02:18 +0000
parents
children a730e47fda4d
comparison
equal deleted inserted replaced
7049:e426f849fc36 7050:bd56a0609c4f
1 ## Copyright (C) 2007 David Bateman
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 {Command} {} what
21 ## @deftypefnx {Command} {} what @var{dir}
22 ## @deftypefnx {Function File} {w =} what (@var{dir})
23 ## List the Octave specific files in a directory. If the variable @var{dir}
24 ## is given then check that directory rather than the current directory. If
25 ## a return argument is requested, the files found are returned in the
26 ## structure @var{w}.
27 ## @seealso{which}
28 ## @end deftypefn
29
30 ## PKG_ADD: mark_as_command what
31
32 function ret = what (d)
33
34 if (nargin == 0)
35 d = pwd ();
36 elseif (isempty (strfind (d, filesep ())))
37 ## Find the appropriate directory on the path
38 p = split (path (), pathsep());
39 p = cellfun (@(x) deblank (x), mat2cell (p, ones (1, size (p, 1)), ...
40 size (p, 2)), "UniformOutput", false);
41 d = p{find (cellfun (@(x) ! isempty (strfind (x, d)), p))(end)};
42 else
43 [status, msg, msgid] = fileattrib (d);
44 if (status != 1)
45 error ("could not find the file or path %s", d);
46 else
47 d = msg.Name;
48 endif
49 endif
50
51 files = dir (d);
52 w.path = d;
53 w.m = cell (0, 1);
54 w.mex = cell (0, 1);
55 w.oct = cell (0, 1);
56 w.mat = cell (0, 1);
57 w.mdl = cell (0, 1);
58 w.p = cell (0, 1);
59 w.classes = cell (0, 1);
60
61 for i = 1 : length (files)
62 n = files(i).name;
63 ## Ignore . and ..
64 if (strcmp (n, ".") || strcmp (n, ".."))
65 continue;
66 else
67 ## Ignore mdl and p files
68 [dummy, f, e] = fileparts (n);
69 if (strcmp (e, ".m"))
70 w.m {end+1} = n;
71 elseif (strcmp (e, mexext ()))
72 w.mex {end + 1} = n;
73 elseif (strcmp (e, ".oct"))
74 w.oct {end + 1} = n;
75 elseif (strcmp (e, ".mat"))
76 w.mat {end + 1} = n;
77 elseif(strcmp (n(1), "@"))
78 w.classes {end + 1} = n;
79 endif
80 endif
81 endfor
82
83 if (nargout == 0)
84 __display_filenames__ ("M-files in directory", w.path, w.m);
85 __display_filenames__ ("\nMEX-files in directory", w.path, w.mex);
86 __display_filenames__ ("\nOCT-files in directory", w.path, w.oct);
87 __display_filenames__ ("\nMAT-files in directory", w.path, w.mat);
88 __display_filenames__ ("\nClasses in directory", w.path, w.classes);
89 else
90 ret = w;
91 endif
92 endfunction
93
94 function __display_filenames__ (msg, p, f)
95 if (length (f) > 0)
96 printf ("%s %s:\n\n", msg, p)
97
98 maxlen = max (cellfun (@(x) length (x), f));
99 ncols = max (1, floor (terminal_size()(2) / (maxlen + 3)));
100 fmt = "";
101 for i = 1: ncols
102 fmt = sprintf ("%s %%-%ds", fmt, maxlen);
103 endfor
104 fmt = [fmt, "\n"];
105
106 nrows = ceil (length (f) / ncols);
107 for i = 1 : nrows
108 args = f(i:nrows:end);
109 if (length (args) < ncols)
110 n = ncols - length (args);
111 args{end : end + n} = "";
112 endif
113 printf (fmt, args{:});
114 endfor
115 endif
116 endfunction