changeset 20205:c0f64bc26eee

imformats.m: rewrite pretty print of format table. * scripts/image/imformats.m: rewrite the code to create pretty format table; initially to follow octave coding guidelines but then to vectorize, simplify, and reduce code duplication.
author Carnë Draug <carandraug@octave.org>
date Wed, 06 May 2015 20:52:16 +0100
parents cf96961effdb
children 5212cda4ac0e
files scripts/image/imformats.m
diffstat 1 files changed, 32 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/image/imformats.m	Wed Feb 04 22:49:04 2015 +0100
+++ b/scripts/image/imformats.m	Wed May 06 20:52:16 2015 +0100
@@ -79,7 +79,7 @@
   persistent formats = default_formats ();
 
   if (nargin == 0 && nargout == 0)
-    pretty_print_formats(formats);
+    pretty_print_formats (formats);
   elseif (nargin >= 1)
     if (isstruct (arg1))
       arrayfun (@is_valid_format, arg1);
@@ -281,93 +281,38 @@
   end_try_catch
 endfunction
 
-function pretty_print_formats(forms)
-
-  ## Compute the maximal length of the extensions column
-  h1 = "Extension";
-  ext_seperator = ", ";
-  max_ext_length = length (h1);
-  for m = 1:length (forms)
-    num_ext = length (forms(m).ext);
-    length_ext = sum (cellfun (@length, forms(m).ext));
-    ext_length = length_ext + (num_ext -1) * length(ext_seperator); 
-    if ext_length > max_ext_length
-      max_ext_length = ext_length;
-    endif
-  endfor
-  h1 = postpad(h1, max_ext_length, " ");
-  
+function pretty_print_formats (formats)
   ## define header names (none should be shorter than 3 characters)
-  h2 = "isa";
-  h3 = "Info";
-  h4 = "Read";
-  h5 = "Write";
-  h6 = "Alpha";
-  h7 = "Description";
-  
-  ## print a header
-  header = [h1, " | ", h2, " | ", h3, " | ", h4, " | ", h5, " | ", h6, " | ", h7, " "]; 
-  disp(header);
-  lineunder = [repmat("-", 1, length(h1)), "-+-", repmat("-", 1, length(h2)), "-+-", ...
-    repmat("-", 1, length(h3)), "-+-", repmat("-", 1, length(h4)), "-+-", ...
-    repmat("-", 1, length(h5)), "-+-", repmat("-", 1, length(h6)), "-+-", ...
-    repmat("-", 1, length(h7)), "-"];
-  disp(lineunder);
-  
-  ## print the formats
-  # ext (h1)
-  for m = 1:length(forms)
-    thisline = "";
-    num_exts = length(forms(m).ext);
-    ext_string = "";
-    for n = 1:num_exts
-      ext_string = [ext_string, forms(m).ext{n}];
-      if n < num_exts
-        ext_string = [ext_string, ext_seperator];
-      endif
-    endfor
-    ext_string = postpad(ext_string, length(h1), " ");
-    thisline = [thisline, ext_string, " | "];
-    # isa (h2)
-    if isempty(forms(m).isa)
-      thisline = [thisline, postpad(" no", length(h2), " ")];
-    else
-      thisline = [thisline, postpad("yes", length(h2), " ")];
-    endif
-    thisline = [thisline, " | "];
-    # info (h3)
-    if isempty(forms(m).info)
-    thisline = [thisline, postpad(" no", length(h3), " ")];
-    else
-      thisline = [thisline, postpad("yes", length(h3), " ")];
-    endif
-    thisline = [thisline, " | "];
-    # read (h4)
-    if isempty(forms(m).read)
-    thisline = [thisline, postpad(" no", length(h4), " ")];
-    else
-      thisline = [thisline, postpad("yes", length(h4), " ")];
-    endif
-    thisline = [thisline, " | "];
-    # write (h5)
-    if isempty(forms(m).write)
-    thisline = [thisline, postpad(" no", length(h5), " ")];
-    else
-      thisline = [thisline, postpad("yes", length(h5), " ")];
-    endif
-    thisline = [thisline, " | "];
-    # alpha (h6)
-    if isempty(forms(m).write)
-    thisline = [thisline, postpad(" no", length(h6), " ")];
-    else
-      thisline = [thisline, postpad("yes", length(h6), " ")];
-    endif
-    thisline = [thisline, " | "];
-    # description (h7)
-    thisline = [thisline, forms(m).description];
-    # print this format line
-    disp(thisline);
-  endfor
+  headers = {"Extension", "isa", "Info", "Read", "Write", "Alpha", "Description"};
+  cols_length = cellfun (@numel, headers);
+
+  ## Adjust the maximal length of the extensions column
+  extensions = cellfun (@strjoin, {formats.ext}, {", "},
+                        "UniformOutput", false);
+  cols_length(1) = max (max (cellfun (@numel, extensions)), cols_length(1));
+  headers{1} = postpad (headers{1}, cols_length(1), " ");
+
+  ## Print the headers
+  disp (strjoin (headers, " | "));
+  under_headers = cellfun (@(x) repmat ("-", 1, numel (x)), headers,
+                           "UniformOutput", false);
+  disp (strjoin (under_headers, "-+-"));
+
+  template = strjoin (arrayfun (@(x) sprintf ("%%-%is", x), cols_length,
+                                "UniformOutput", false), " | ");
+
+  ## Print the function handle for this things won't be a pretty table.  So
+  ## instead we replace them with "yes" or "no", based on the support it has.
+  yes_no_cols = cat (2, {formats.isa}(:), {formats.info}(:), {formats.read}(:),
+                     {formats.write}(:), {formats.alpha}(:));
+  empty = cellfun (@isempty, yes_no_cols);
+  yes_no_cols(empty) = "no";
+  yes_no_cols(! empty) = "yes";
+
+  descriptions = {formats.description};
+  table = cat (2, extensions(:), yes_no_cols, descriptions(:));
+  printf ([template "\n"], table'{:});
+
 endfunction
 
 ## When imread or imfinfo are called, the file must exist or the