changeset 11911:f0de7bd67a4f octave-forge

color output
author abarth93
date Tue, 02 Jul 2013 20:37:59 +0000
parents dcd32f7bfabe
children 27d9f37e720d
files main/netcdf/inst/ncdisp.m
diffstat 1 files changed, 77 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/main/netcdf/inst/ncdisp.m	Tue Jul 02 20:10:11 2013 +0000
+++ b/main/netcdf/inst/ncdisp.m	Tue Jul 02 20:37:59 2013 +0000
@@ -1,36 +1,64 @@
+## Copyright (C) 2013 Alexander Barth
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; If not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn  {Function File} @var{info} = ncdisp (@var{filename})
+## display meta-data of the NetCDF file @var{filename}
+##
+## @seealso{ncinfo}
+##
+## @end deftypefn
+
 function ncdisp(filename)
 
 info = ncinfo(filename);
 
-fprintf('Source:\n');
-indent = repmat(' ',[1 11]);
-fprintf('%s%s\n',indent,fullfile(filename));
-fprintf('Format:\n');
-fprintf('%s%s\n',indent,info.Format);
+fprintf("Source:\n");
+indent = repmat(" ",[1 11]);
+fprintf("%s%s\n",indent,fullfile(filename));
+fprintf("Format:\n");
+fprintf("%s%s\n",indent,info.Format);
 
-printgroup('',info);
+colors.var = "red";
+colors.att = "cyan";
+colors.dim = "blue";
+group_color = "green";
+
+printgroup("",info,colors);
 
 function s = fmtattr(val)
 if ischar(val)
-  s = sprintf('''%s''',val);
+  s = sprintf("""%s""",val);
 else
   s = num2str(val);
 end
 
 function s = fmtsize(sz) 
-s = sprintf('%gx',sz);
+s = sprintf("%gx",sz);
 s = s(1:end-1);
 
 
-function printgroup(indent1,info)
+function printgroup(indent1,info,colors)
 
-indent2 = [indent1 repmat(' ',[1 11])];
-indent3 = [indent2 repmat(' ',[1 11])];
+indent2 = [indent1 repmat(" ",[1 11])];
+indent3 = [indent2 repmat(" ",[1 11])];
 
 % attributes
 if ~isempty(info.Attributes)
-  fprintf('%sGlobal Attributes:\n',indent1);
-  printattr(indent2,info.Attributes);
+  fprintf("%sGlobal Attributes:\n",indent1);
+  printattr(indent2,info.Attributes,colors);
 end
 
 % dimensions
@@ -38,39 +66,41 @@
   % length of the longest attribute name
   dim = info.Dimensions;
   maxlen = max(cellfun(@length,{dim.Name}));
-  fprintf('%sDimensions:\n',indent1);
+  fprintf("%sDimensions:\n",indent1);
   for i = 1:length(dim)
-    space = repmat(' ',[maxlen-length(dim(i).Name) 1]);
-    fprintf('%s%s %s= %d\n',indent2,dim(i).Name,space,dim(i).Length);
+    space = repmat(" ",[maxlen-length(dim(i).Name) 1]);
+    fprintf("%s",indent2);
+    colormsg(sprintf("%s %s= %d",dim(i).Name,space,dim(i).Length),colors.dim);
+    fprintf("\n");
   end
 end
 
 % variables
-if isfield(info,'Variables')
+if isfield(info,"Variables")
   if ~isempty(info.Variables)
     % length of the longest attribute name
     vars = info.Variables;
-    fprintf('%sVariables:\n',indent1);
+    fprintf("%sVariables:\n",indent1);
     for i = 1:length(vars)
-      fprintf('%s%s\n',indent2(1:end-7),vars(i).Name);
-      %colormsg(sprintf('%s%s\n',indent2(1:end-7),vars(i).Name),'red');
+      %fprintf("%s%s\n",indent2(1:end-7),vars(i).Name);
+      colormsg(sprintf("%s%s\n",indent2(1:end-7),vars(i).Name),colors.var);
       
       if ~isempty(vars(i).Size)
         sz = fmtsize(vars(i).Size);
-        dimname = sprintf('%s,',vars(i).Dimensions.Name);
+        dimname = sprintf("%s,",vars(i).Dimensions.Name);
         dimname = dimname(1:end-1);
       else
-        sz = '1x1';
-        dimname = '';
+        sz = "1x1";
+        dimname = "";
       end
       
-      fprintf('%sSize:       %s\n',indent2,sz);    
-      fprintf('%sDimensions: %s\n',indent2,dimname);
-      fprintf('%sDatatype:   %s\n',indent2,vars(i).Datatype);
+      fprintf("%sSize:       %s\n",indent2,sz);    
+      fprintf("%sDimensions: %s\n",indent2,dimname);
+      fprintf("%sDatatype:   %s\n",indent2,vars(i).Datatype);
       
       if ~isempty(vars(i).Attributes);
-        fprintf('%sAttributes:\n',indent2);
-        printattr(indent3,vars(i).Attributes);
+        fprintf("%sAttributes:\n",indent2);
+        printattr(indent3,vars(i).Attributes,colors);
       end
     end
   end
@@ -80,71 +110,47 @@
 if ~isempty(info.Groups)
   % length of the longest attribute name
   grps = info.Groups;
-  fprintf('%sGroups:\n',indent1);
+  fprintf("%sGroups:\n",indent1);
   for i = 1:length(grps)
-    fprintf('%s%s\n',indent2(1:end-7),grps(i).Name);
-    printgroup(indent2,grps(i));
+    fprintf("%s%s\n",indent2(1:end-7),grps(i).Name);
+    printgroup(indent2,grps(i),colors);
   end
 end
 
 
 
-function printattr(indent,attr)
+function printattr(indent,attr,colors)
 % length of the longest attribute name
 maxlen = max(cellfun(@length,{attr.Name}));
 for i = 1:length(attr)
-  space = repmat(' ',[maxlen-length(attr(i).Name) 1]);
-  fprintf('%s%s %s= %s\n',indent,attr(i).Name,space,fmtattr(attr(i).Value));
+  space = repmat(" ",[maxlen-length(attr(i).Name) 1]);
+  %fprintf("%s%s %s= %s\n",indent,attr(i).Name,space,fmtattr(attr(i).Value));
+  fprintf("%s",indent);
+  colormsg(sprintf("%s %s= %s\n",attr(i).Name,space,fmtattr(attr(i).Value)),colors.att);
 end
 
 
 
 function colormsg (msg,color)
 
-%getenv('TERM')
-%if strcmp(getenv('TERM'),'xterm') % && exist('puts') > 1
-% only in octave
-if exist('puts') > 1
+if strcmp(getenv("TERM"),"xterm")
   esc = char(27);          
 
   % ANSI escape codes
-  colors.black   = [esc, '[40m'];
-  colors.red     = [esc, '[41m'];
-  colors.green   = [esc, '[42m'];
-  colors.yellow  = [esc, '[43m'];
-  colors.blue    = [esc, '[44m'];
-  colors.magenta = [esc, '[45m'];
-  colors.cyan    = [esc, '[46m'];
-  colors.white   = [esc, '[47m'];
+  colors.black   = [esc, "[30m"];
+  colors.red     = [esc, "[31m"];
+  colors.green   = [esc, "[32m"];
+  colors.yellow  = [esc, "[33m"];
+  colors.blue    = [esc, "[34m"];
+  colors.magenta = [esc, "[35m"];
+  colors.cyan    = [esc, "[36m"];
+  colors.white   = [esc, "[37m"];
 
-  reset = [esc, '[0m'];
+  reset = [esc, "[0m"];
   
   c = getfield(colors,color);
-           
-  %oldpso = page_screen_output (0);
 
-  try
-    fprintf([c, msg, reset]);
-    %puts (reset); % reset terminal
-  catch
-    %page_screen_output (oldpso);
-  end
+  fprintf([c, msg, reset]);
 else
   fprintf(msg);
 end
-
-
-%% Copyright (C) 2013 Alexander Barth
-%%
-%% This program is free software; you can redistribute it and/or modify
-%% it under the terms of the GNU General Public License as published by
-%% the Free Software Foundation; either version 2 of the License, or
-%% (at your option) any later version.
-%%
-%% This program is distributed in the hope that it will be useful,
-%% but WITHOUT ANY WARRANTY; without even the implied warranty of
-%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-%% GNU General Public License for more details.
-%%
-%% You should have received a copy of the GNU General Public License
-%% along with this program; If not, see <http://www.gnu.org/licenses/>.