changeset 20515:ad7fe3cb6fd2

pkg: fix regression on output of pkg list (bug #45873) * scripts/pkg/pkg.m: cset 7fa1970a655d reduced the checks to nargout to is there nargout or not. However, the subfunction installed_packages() returns alternative things based on its own nargout (it probably should not since that makes for a messy code). Fix, pkg to call the subfunction correctly based on its nargout too (but still, use "nargout > 1" instead of "nargout == 2". * scripts/pkg/private/installed_packages.m: do not check for exact number of nargout and reduce number of function return points.
author Carnë Draug <carandraug@octave.org>
date Fri, 04 Sep 2015 18:47:54 +0100
parents ac0f7acdc3fd
children fb6fe9b45c41
files scripts/pkg/pkg.m scripts/pkg/private/installed_packages.m
diffstat 2 files changed, 59 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/pkg/pkg.m	Fri Sep 04 12:45:46 2015 -0400
+++ b/scripts/pkg/pkg.m	Fri Sep 04 18:47:54 2015 +0100
@@ -368,7 +368,9 @@
           list_forge_packages ();
         endif
       else
-        if (nargout)
+        if (nargout == 1)
+          local_packages = installed_packages (local_list, global_list, files);
+        elseif (nargout > 1)
           [local_packages, global_packages] = installed_packages (local_list,
                                                                   global_list,
                                                                   files);
--- a/scripts/pkg/private/installed_packages.m	Fri Sep 04 12:45:46 2015 -0400
+++ b/scripts/pkg/private/installed_packages.m	Fri Sep 04 18:47:54 2015 +0100
@@ -83,73 +83,69 @@
   endfor
 
   ## Should we return something?
-  if (nargout == 2)
+  if (nargout == 1)
+    out1 = installed_pkgs_lst;
+  elseif (nargout > 1)
     out1 = local_packages;
     out2 = global_packages;
-    return;
-  elseif (nargout == 1)
-    out1 = installed_pkgs_lst;
-    return;
-  endif
-
-  ## Don't return anything, instead we'll print something.
-  num_packages = numel (installed_pkgs_lst);
-  if (num_packages == 0)
-    if (isempty (pkgname))
-      printf ("no packages installed.\n");
-    else
-      printf ("package %s is not installed.\n", pkgname{1});
+  else
+    ## Don't return anything, instead we'll print something.
+    num_packages = numel (installed_pkgs_lst);
+    if (num_packages == 0)
+      if (isempty (pkgname))
+        printf ("no packages installed.\n");
+      else
+        printf ("package %s is not installed.\n", pkgname{1});
+      endif
+      return;
     endif
-    return;
-  endif
 
-  ## Compute the maximal lengths of name, version, and dir.
-  h1 = "Package Name";
-  h2 = "Version";
-  h3 = "Installation directory";
-  max_name_length = max ([length(h1), cellfun(@length, installed_names)]);
-  version_lengths = cellfun (@(x) length (x.version), installed_pkgs_lst);
-  max_version_length = max ([length(h2), version_lengths]);
-  ncols = terminal_size ()(2);
-  max_dir_length = ncols - max_name_length - max_version_length - 7;
-  if (max_dir_length < 20)
-    max_dir_length = Inf;
-  endif
+    ## Compute the maximal lengths of name, version, and dir.
+    h1 = "Package Name";
+    h2 = "Version";
+    h3 = "Installation directory";
+    max_name_length = max ([length(h1), cellfun(@length, installed_names)]);
+    version_lengths = cellfun (@(x) length (x.version), installed_pkgs_lst);
+    max_version_length = max ([length(h2), version_lengths]);
+    ncols = terminal_size ()(2);
+    max_dir_length = ncols - max_name_length - max_version_length - 7;
+    if (max_dir_length < 20)
+      max_dir_length = Inf;
+    endif
 
-  h1 = postpad (h1, max_name_length + 1, " ");
-  h2 = postpad (h2, max_version_length, " ");;
+    h1 = postpad (h1, max_name_length + 1, " ");
+    h2 = postpad (h2, max_version_length, " ");;
 
-  ## Print a header.
-  header = sprintf ("%s | %s | %s\n", h1, h2, h3);
-  printf (header);
-  tmp = sprintf (repmat ("-", 1, length (header) - 1));
-  tmp(length(h1)+2) = "+";
-  tmp(length(h1)+length(h2)+5) = "+";
-  printf ("%s\n", tmp);
+    ## Print a header.
+    header = sprintf ("%s | %s | %s\n", h1, h2, h3);
+    printf (header);
+    tmp = sprintf (repmat ("-", 1, length (header) - 1));
+    tmp(length(h1)+2) = "+";
+    tmp(length(h1)+length(h2)+5) = "+";
+    printf ("%s\n", tmp);
 
-  ## Print the packages.
-  format = sprintf ("%%%ds %%1s| %%%ds | %%s\n",
-                    max_name_length, max_version_length);
-  for i = 1:num_packages
-    cur_name = installed_pkgs_lst{i}.name;
-    cur_version = installed_pkgs_lst{i}.version;
-    cur_dir = installed_pkgs_lst{i}.dir;
-    if (length (cur_dir) > max_dir_length)
-      first_char = length (cur_dir) - max_dir_length + 4;
-      first_filesep = strfind (cur_dir(first_char:end), filesep ());
-      if (! isempty (first_filesep))
-        cur_dir = ["..." cur_dir((first_char + first_filesep(1) - 1):end)];
+    ## Print the packages.
+    format = sprintf ("%%%ds %%1s| %%%ds | %%s\n",
+                      max_name_length, max_version_length);
+    for i = 1:num_packages
+      cur_name = installed_pkgs_lst{i}.name;
+      cur_version = installed_pkgs_lst{i}.version;
+      cur_dir = installed_pkgs_lst{i}.dir;
+      if (length (cur_dir) > max_dir_length)
+        first_char = length (cur_dir) - max_dir_length + 4;
+        first_filesep = strfind (cur_dir(first_char:end), filesep ());
+        if (! isempty (first_filesep))
+          cur_dir = ["..." cur_dir((first_char + first_filesep(1) - 1):end)];
+        else
+          cur_dir = ["..." cur_dir(first_char:end)];
+        endif
+      endif
+      if (installed_pkgs_lst{i}.loaded)
+        cur_loaded = "*";
       else
-        cur_dir = ["..." cur_dir(first_char:end)];
+        cur_loaded = " ";
       endif
-    endif
-    if (installed_pkgs_lst{i}.loaded)
-      cur_loaded = "*";
-    else
-      cur_loaded = " ";
-    endif
-    printf (format, cur_name, cur_loaded, cur_version, cur_dir);
-  endfor
-
+      printf (format, cur_name, cur_loaded, cur_version, cur_dir);
+    endfor
+  endif
 endfunction
-