comparison scripts/pkg/private/installed_packages.m @ 19221:b54093acb8fe

ver.m: Overhaul function. Add ability to call "ver PKG_NAME". Add ability to call "pkg list PKG_NAME". * ver.m: Redo docstring. Rename output variable to retval rather than unnecessary use of varargout. Add ability to call "ver PKG_NAME" by passing PKG_NAME to 'pkg list' command. * pkg.m: Redo docstring for 'list' to explain new ability to supply a PKG_NAME. Use numel() rather than length() for clarity. Use isempty() rather than length() == 0 for clarity. * installed_packages.m: Add additional input argument pkgname. Use unique() rather than double for loop to uniquify package list. Add code to report only on package pkgname if given. Use numel() rather than length() for clarity. Replace for loops with cellfun calls to determine max length of string entries. * version.m: Add seealso reference to ver in docstring.
author Rik <rik@octave.org>
date Tue, 30 Sep 2014 08:29:16 -0700
parents d63878346099
children db92e7e28e1f
comparison
equal deleted inserted replaced
19220:1111d2d5ff95 19221:b54093acb8fe
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {[@var{out1}, @var{out2}] =} installed_packages (@var{local_list}, @var{global_list}) 21 ## @deftypefn {Function File} {[@var{out1}, @var{out2}] =} installed_packages (@var{local_list}, @var{global_list})
22 ## Undocumented internal function. 22 ## Undocumented internal function.
23 ## @end deftypefn 23 ## @end deftypefn
24 24
25 function [out1, out2] = installed_packages (local_list, global_list) 25 function [out1, out2] = installed_packages (local_list, global_list, pkgname = {})
26
26 ## Get the list of installed packages. 27 ## Get the list of installed packages.
27 try 28 try
28 local_packages = load (local_list).local_packages; 29 local_packages = load (local_list).local_packages;
29 catch 30 catch
30 local_packages = {}; 31 local_packages = {};
36 end_try_catch 37 end_try_catch
37 installed_pkgs_lst = {local_packages{:}, global_packages{:}}; 38 installed_pkgs_lst = {local_packages{:}, global_packages{:}};
38 39
39 ## Eliminate duplicates in the installed package list. 40 ## Eliminate duplicates in the installed package list.
40 ## Locally installed packages take precedence. 41 ## Locally installed packages take precedence.
41 dup = []; 42 installed_names = cellfun (@(x) x.name, installed_pkgs_lst,
42 for i = 1:length (installed_pkgs_lst) 43 "uniformoutput", false);
43 if (any (dup == i)) 44 [~, idx] = unique (installed_names, "first");
44 continue; 45 installed_names = installed_names(idx);
46 installed_pkgs_lst = installed_pkgs_lst(idx);
47
48 ## Check whether info on a particular package was requested
49 if (! isempty (pkgname))
50 idx = find (strcmp (pkgname{1}, installed_names));
51 if (isempty (idx))
52 installed_names = {};
53 installed_pkgs_lst = {};
54 else
55 installed_names = installed_names(idx);
56 installed_pkgs_lst = installed_pkgs_lst(idx);
45 endif 57 endif
46 for j = (i+1):length (installed_pkgs_lst)
47 if (any (dup == j))
48 continue;
49 endif
50 if (strcmp (installed_pkgs_lst{i}.name, installed_pkgs_lst{j}.name))
51 dup = [dup, j];
52 endif
53 endfor
54 endfor
55 if (! isempty (dup))
56 installed_pkgs_lst(dup) = [];
57 endif 58 endif
58 59
59 ## Now check if the package is loaded. 60 ## Now check if the package is loaded.
61 ## FIXME: couldn't dir_in_loadpath() be used here?
60 tmppath = strrep (path (), "\\", "/"); 62 tmppath = strrep (path (), "\\", "/");
61 for i = 1:length (installed_pkgs_lst) 63 for i = 1:numel (installed_pkgs_lst)
62 if (strfind (tmppath, strrep (installed_pkgs_lst{i}.dir, '\', '/'))) 64 if (strfind (tmppath, strrep (installed_pkgs_lst{i}.dir, '\', '/')))
63 installed_pkgs_lst{i}.loaded = true; 65 installed_pkgs_lst{i}.loaded = true;
64 else 66 else
65 installed_pkgs_lst{i}.loaded = false; 67 installed_pkgs_lst{i}.loaded = false;
66 endif 68 endif
67 endfor 69 endfor
68 for i = 1:length (local_packages) 70 for i = 1:numel (local_packages)
69 if (strfind (tmppath, strrep (local_packages{i}.dir, '\', '/'))) 71 if (strfind (tmppath, strrep (local_packages{i}.dir, '\', '/')))
70 local_packages{i}.loaded = true; 72 local_packages{i}.loaded = true;
71 else 73 else
72 local_packages{i}.loaded = false; 74 local_packages{i}.loaded = false;
73 endif 75 endif
74 endfor 76 endfor
75 for i = 1:length (global_packages) 77 for i = 1:numel (global_packages)
76 if (strfind (tmppath, strrep (global_packages{i}.dir, '\', '/'))) 78 if (strfind (tmppath, strrep (global_packages{i}.dir, '\', '/')))
77 global_packages{i}.loaded = true; 79 global_packages{i}.loaded = true;
78 else 80 else
79 global_packages{i}.loaded = false; 81 global_packages{i}.loaded = false;
80 endif 82 endif
88 elseif (nargout == 1) 90 elseif (nargout == 1)
89 out1 = installed_pkgs_lst; 91 out1 = installed_pkgs_lst;
90 return; 92 return;
91 endif 93 endif
92 94
93 ## We shouldn't return something, so we'll print something. 95 ## Don't return anything, instead we'll print something.
94 num_packages = length (installed_pkgs_lst); 96 num_packages = numel (installed_pkgs_lst);
95 if (num_packages == 0) 97 if (num_packages == 0)
96 printf ("no packages installed.\n"); 98 if (isempty (pkgname))
99 printf ("no packages installed.\n");
100 else
101 printf ("package %s is not installed.\n", pkgname{1});
102 endif
97 return; 103 return;
98 endif 104 endif
99 105
100 ## Compute the maximal lengths of name, version, and dir. 106 ## Compute the maximal lengths of name, version, and dir.
101 h1 = "Package Name"; 107 h1 = "Package Name";
102 h2 = "Version"; 108 h2 = "Version";
103 h3 = "Installation directory"; 109 h3 = "Installation directory";
104 max_name_length = length (h1); 110 max_name_length = max ([length(h1), cellfun(@length, installed_names)]);
105 max_version_length = length (h2); 111 version_lengths = cellfun (@(x) length (x.version), installed_pkgs_lst);
106 names = cell (num_packages, 1); 112 max_version_length = max ([length(h2), version_lengths]);
107 for i = 1:num_packages 113 ncols = terminal_size ()(2);
108 max_name_length = max (max_name_length, 114 max_dir_length = ncols - max_name_length - max_version_length - 7;
109 length (installed_pkgs_lst{i}.name));
110 max_version_length = max (max_version_length,
111 length (installed_pkgs_lst{i}.version));
112 names{i} = installed_pkgs_lst{i}.name;
113 endfor
114 max_dir_length = terminal_size ()(2) - max_name_length - ...
115 max_version_length - 7;
116 if (max_dir_length < 20) 115 if (max_dir_length < 20)
117 max_dir_length = Inf; 116 max_dir_length = Inf;
118 endif 117 endif
119 118
120 h1 = postpad (h1, max_name_length + 1, " "); 119 h1 = postpad (h1, max_name_length + 1, " ");
121 h2 = postpad (h2, max_version_length, " ");; 120 h2 = postpad (h2, max_version_length, " ");;
122 121
127 tmp(length(h1)+2) = "+"; 126 tmp(length(h1)+2) = "+";
128 tmp(length(h1)+length(h2)+5) = "+"; 127 tmp(length(h1)+length(h2)+5) = "+";
129 printf ("%s\n", tmp); 128 printf ("%s\n", tmp);
130 129
131 ## Print the packages. 130 ## Print the packages.
132 format = sprintf ("%%%ds %%1s| %%%ds | %%s\n", max_name_length, 131 format = sprintf ("%%%ds %%1s| %%%ds | %%s\n",
133 max_version_length); 132 max_name_length, max_version_length);
134 [dummy, idx] = sort (names);
135 for i = 1:num_packages 133 for i = 1:num_packages
136 cur_name = installed_pkgs_lst{idx(i)}.name; 134 cur_name = installed_pkgs_lst{i}.name;
137 cur_version = installed_pkgs_lst{idx(i)}.version; 135 cur_version = installed_pkgs_lst{i}.version;
138 cur_dir = installed_pkgs_lst{idx(i)}.dir; 136 cur_dir = installed_pkgs_lst{i}.dir;
139 if (length (cur_dir) > max_dir_length) 137 if (length (cur_dir) > max_dir_length)
140 first_char = length (cur_dir) - max_dir_length + 4; 138 first_char = length (cur_dir) - max_dir_length + 4;
141 first_filesep = strfind (cur_dir(first_char:end), filesep ()); 139 first_filesep = strfind (cur_dir(first_char:end), filesep ());
142 if (! isempty (first_filesep)) 140 if (! isempty (first_filesep))
143 cur_dir = ["..." cur_dir((first_char + first_filesep(1) - 1):end)]; 141 cur_dir = ["..." cur_dir((first_char + first_filesep(1) - 1):end)];
144 else 142 else
145 cur_dir = ["..." cur_dir(first_char:end)]; 143 cur_dir = ["..." cur_dir(first_char:end)];
146 endif 144 endif
147 endif 145 endif
148 if (installed_pkgs_lst{idx(i)}.loaded) 146 if (installed_pkgs_lst{i}.loaded)
149 cur_loaded = "*"; 147 cur_loaded = "*";
150 else 148 else
151 cur_loaded = " "; 149 cur_loaded = " ";
152 endif 150 endif
153 printf (format, cur_name, cur_loaded, cur_version, cur_dir); 151 printf (format, cur_name, cur_loaded, cur_version, cur_dir);
154 endfor 152 endfor
153
155 endfunction 154 endfunction
156 155