comparison scripts/image/imformats.m @ 20170: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
comparison
equal deleted inserted replaced
20169:cf96961effdb 20170:c0f64bc26eee
77 77
78 mlock (); # prevent formats to be removed by "clear all" 78 mlock (); # prevent formats to be removed by "clear all"
79 persistent formats = default_formats (); 79 persistent formats = default_formats ();
80 80
81 if (nargin == 0 && nargout == 0) 81 if (nargin == 0 && nargout == 0)
82 pretty_print_formats(formats); 82 pretty_print_formats (formats);
83 elseif (nargin >= 1) 83 elseif (nargin >= 1)
84 if (isstruct (arg1)) 84 if (isstruct (arg1))
85 arrayfun (@is_valid_format, arg1); 85 arrayfun (@is_valid_format, arg1);
86 ## FIXME: what is the return value in this situation? 86 ## FIXME: what is the return value in this situation?
87 formats = arg1; 87 formats = arg1;
279 info = __magick_ping__ (filename, 1); 279 info = __magick_ping__ (filename, 1);
280 bool = strcmp (coder, info.Format); 280 bool = strcmp (coder, info.Format);
281 end_try_catch 281 end_try_catch
282 endfunction 282 endfunction
283 283
284 function pretty_print_formats(forms) 284 function pretty_print_formats (formats)
285
286 ## Compute the maximal length of the extensions column
287 h1 = "Extension";
288 ext_seperator = ", ";
289 max_ext_length = length (h1);
290 for m = 1:length (forms)
291 num_ext = length (forms(m).ext);
292 length_ext = sum (cellfun (@length, forms(m).ext));
293 ext_length = length_ext + (num_ext -1) * length(ext_seperator);
294 if ext_length > max_ext_length
295 max_ext_length = ext_length;
296 endif
297 endfor
298 h1 = postpad(h1, max_ext_length, " ");
299
300 ## define header names (none should be shorter than 3 characters) 285 ## define header names (none should be shorter than 3 characters)
301 h2 = "isa"; 286 headers = {"Extension", "isa", "Info", "Read", "Write", "Alpha", "Description"};
302 h3 = "Info"; 287 cols_length = cellfun (@numel, headers);
303 h4 = "Read"; 288
304 h5 = "Write"; 289 ## Adjust the maximal length of the extensions column
305 h6 = "Alpha"; 290 extensions = cellfun (@strjoin, {formats.ext}, {", "},
306 h7 = "Description"; 291 "UniformOutput", false);
307 292 cols_length(1) = max (max (cellfun (@numel, extensions)), cols_length(1));
308 ## print a header 293 headers{1} = postpad (headers{1}, cols_length(1), " ");
309 header = [h1, " | ", h2, " | ", h3, " | ", h4, " | ", h5, " | ", h6, " | ", h7, " "]; 294
310 disp(header); 295 ## Print the headers
311 lineunder = [repmat("-", 1, length(h1)), "-+-", repmat("-", 1, length(h2)), "-+-", ... 296 disp (strjoin (headers, " | "));
312 repmat("-", 1, length(h3)), "-+-", repmat("-", 1, length(h4)), "-+-", ... 297 under_headers = cellfun (@(x) repmat ("-", 1, numel (x)), headers,
313 repmat("-", 1, length(h5)), "-+-", repmat("-", 1, length(h6)), "-+-", ... 298 "UniformOutput", false);
314 repmat("-", 1, length(h7)), "-"]; 299 disp (strjoin (under_headers, "-+-"));
315 disp(lineunder); 300
316 301 template = strjoin (arrayfun (@(x) sprintf ("%%-%is", x), cols_length,
317 ## print the formats 302 "UniformOutput", false), " | ");
318 # ext (h1) 303
319 for m = 1:length(forms) 304 ## Print the function handle for this things won't be a pretty table. So
320 thisline = ""; 305 ## instead we replace them with "yes" or "no", based on the support it has.
321 num_exts = length(forms(m).ext); 306 yes_no_cols = cat (2, {formats.isa}(:), {formats.info}(:), {formats.read}(:),
322 ext_string = ""; 307 {formats.write}(:), {formats.alpha}(:));
323 for n = 1:num_exts 308 empty = cellfun (@isempty, yes_no_cols);
324 ext_string = [ext_string, forms(m).ext{n}]; 309 yes_no_cols(empty) = "no";
325 if n < num_exts 310 yes_no_cols(! empty) = "yes";
326 ext_string = [ext_string, ext_seperator]; 311
327 endif 312 descriptions = {formats.description};
328 endfor 313 table = cat (2, extensions(:), yes_no_cols, descriptions(:));
329 ext_string = postpad(ext_string, length(h1), " "); 314 printf ([template "\n"], table'{:});
330 thisline = [thisline, ext_string, " | "]; 315
331 # isa (h2)
332 if isempty(forms(m).isa)
333 thisline = [thisline, postpad(" no", length(h2), " ")];
334 else
335 thisline = [thisline, postpad("yes", length(h2), " ")];
336 endif
337 thisline = [thisline, " | "];
338 # info (h3)
339 if isempty(forms(m).info)
340 thisline = [thisline, postpad(" no", length(h3), " ")];
341 else
342 thisline = [thisline, postpad("yes", length(h3), " ")];
343 endif
344 thisline = [thisline, " | "];
345 # read (h4)
346 if isempty(forms(m).read)
347 thisline = [thisline, postpad(" no", length(h4), " ")];
348 else
349 thisline = [thisline, postpad("yes", length(h4), " ")];
350 endif
351 thisline = [thisline, " | "];
352 # write (h5)
353 if isempty(forms(m).write)
354 thisline = [thisline, postpad(" no", length(h5), " ")];
355 else
356 thisline = [thisline, postpad("yes", length(h5), " ")];
357 endif
358 thisline = [thisline, " | "];
359 # alpha (h6)
360 if isempty(forms(m).write)
361 thisline = [thisline, postpad(" no", length(h6), " ")];
362 else
363 thisline = [thisline, postpad("yes", length(h6), " ")];
364 endif
365 thisline = [thisline, " | "];
366 # description (h7)
367 thisline = [thisline, forms(m).description];
368 # print this format line
369 disp(thisline);
370 endfor
371 endfunction 316 endfunction
372 317
373 ## When imread or imfinfo are called, the file must exist or the 318 ## When imread or imfinfo are called, the file must exist or the
374 ## function defined by imformats will never be called. Because 319 ## function defined by imformats will never be called. Because
375 ## of this, we must create a file for the tests to work. 320 ## of this, we must create a file for the tests to work.