1 |
gforget |
1.1 |
function [msgOut]=gcmfaces_msg(msgIn,headerOut,widthOut); |
2 |
|
|
%object: print formatted message to screen |
3 |
|
|
%input: msgIn is a char or a cell array of char |
4 |
|
|
%optional: headerOut is the header(default: no header if |
5 |
|
|
% myenv.verbose<2; calling tree if myenv.verbose>=2) |
6 |
|
|
% widthOut is the line width (75 by default) |
7 |
|
|
%output: msgOut is a char where line breaks were set |
8 |
|
|
% so that all lines have approx. the same width. |
9 |
|
|
%note: could also introduce form feeds (page break for print) or |
10 |
|
|
|
11 |
|
|
%test case: |
12 |
|
|
% msgIn={'xxxx \ryy\tzz\n' ['1 ' char(9) '2' char(10) '3' char(11) '4' char(12) '5']} |
13 |
|
|
% widthOut=3; |
14 |
|
|
% gcmfaces_msg(msgIn,widthOut); |
15 |
|
|
|
16 |
|
|
gcmfaces_global; |
17 |
|
|
if isempty(who('headerOut'))&myenv.verbose<2; |
18 |
|
|
headerOut=''; |
19 |
|
|
elseif isempty(who('headerOut'))&myenv.verbose>=2; |
20 |
|
|
%pause before each event |
21 |
|
|
if myenv.verbose>=3; fprintf('\n > hit return to continue <\n'); pause; end; |
22 |
|
|
% |
23 |
|
|
[ST,I]=dbstack; |
24 |
|
|
headerOut={'gcmfaces message from '}; |
25 |
|
|
|
26 |
|
|
if isempty(ST); |
27 |
|
|
headerOut={headerOut{:} 'main workspace'}; |
28 |
|
|
else; |
29 |
|
|
for ii=length(ST):-1:2; |
30 |
|
|
tmp1=[char(45)*ones(1,length(ST)-ii+1)]; |
31 |
|
|
headerOut={headerOut{:} [tmp1 ST(ii).name ' at line ' num2str(ST(ii).line)]}; |
32 |
|
|
end; |
33 |
|
|
end; |
34 |
|
|
|
35 |
|
|
m=0; for ii=1:length(headerOut); m=max(m,length(headerOut{ii})); end; |
36 |
|
|
tmpOut=45*ones(1,m+2); |
37 |
|
|
for ii=1:length(headerOut); |
38 |
|
|
n=length(headerOut{ii}); |
39 |
|
|
tmpOut=[tmpOut char(10) '|' headerOut{ii} 32*ones(1,m-n) '|']; |
40 |
|
|
end; |
41 |
|
|
tmpOut=[tmpOut char(10) 45*ones(1,m+2) char(10)]; |
42 |
|
|
|
43 |
|
|
headerOut=tmpOut; |
44 |
|
|
end; |
45 |
|
|
|
46 |
|
|
if isempty(who('widthOut')); widthOut=75; end; |
47 |
|
|
|
48 |
|
|
%step 1: reformat msgIn to one char line, if needed |
49 |
|
|
%======= |
50 |
|
|
|
51 |
|
|
%1.1) make cell array |
52 |
|
|
if ischar(msgIn)&size(msgIn,2)>1; |
53 |
|
|
nLinesIn=size(msgIn,1); tmpIn={}; |
54 |
|
|
for ii=1:nLinesIn; tmpIn{ii}=msgIn(ii,:); end; |
55 |
|
|
msgIn=tmpIn; |
56 |
|
|
end; |
57 |
|
|
if iscell(msgIn)&size(msgIn,2)>1; msgIn=msgIn'; end; |
58 |
|
|
|
59 |
|
|
%2.2) cat to one char line |
60 |
|
|
msgIn=strcat(cell2mat(msgIn')); |
61 |
|
|
|
62 |
|
|
%step 2: deformat text -- rm/replace control characters from caller |
63 |
|
|
%======= |
64 |
|
|
|
65 |
|
|
%2.1) matlab control characters |
66 |
|
|
ii=strfind(msgIn,'\n'); for jj=ii; msgIn=[msgIn(1:jj-1) ' ' msgIn(jj+2:end)]; end; |
67 |
|
|
ii=strfind(msgIn,'\r'); for jj=ii; msgIn=[msgIn(1:jj-1) ' ' msgIn(jj+2:end)]; end; |
68 |
|
|
ii=strfind(msgIn,'\t'); for jj=ii; msgIn=[msgIn(1:jj-1) ' ' msgIn(jj+2:end)]; end; |
69 |
|
|
ii=strfind(msgIn,'\b'); for jj=ii; msgIn=[msgIn(1:jj-1) ' ' msgIn(jj+2:end)]; end; |
70 |
|
|
ii=strfind(msgIn,'\f'); for jj=ii; msgIn=[msgIn(1:jj-1) ' ' msgIn(jj+2:end)]; end; |
71 |
|
|
|
72 |
|
|
%2.2) ANSI C control characters |
73 |
|
|
tmp1=msgIn; tmp2=double(tmp1); |
74 |
|
|
%substitute some with a space |
75 |
|
|
jj=find(tmp2>8&tmp2<13); tmp1(jj)=32; |
76 |
|
|
%remove the others |
77 |
|
|
jj=find(tmp2>=32); tmp1=tmp1(jj); |
78 |
|
|
msgIn=char(tmp1); |
79 |
|
|
|
80 |
|
|
%2.3) double spaces |
81 |
|
|
ii=strfind(msgIn,' '); |
82 |
|
|
while ~isempty(ii); |
83 |
|
|
jj=ii(1); |
84 |
|
|
msgIn=[msgIn(1:jj-1) ' ' msgIn(jj+2:end)]; |
85 |
|
|
ii=strfind(msgIn,' '); |
86 |
|
|
end; |
87 |
|
|
|
88 |
|
|
%step 3: reformat text -- according to to gcmfaces_msg standards |
89 |
|
|
%======= |
90 |
|
|
|
91 |
|
|
ii=strfind(msgIn,' '); |
92 |
|
|
if isempty(ii); ii=[ii length(msgIn)+1]; end; |
93 |
|
|
if ii(end)~=length(msgIn); ii=[ii length(msgIn)+1]; end; |
94 |
|
|
if ii(1)~=1; ii=[0 ii]; end; |
95 |
|
|
|
96 |
|
|
msgOut=headerOut; |
97 |
|
|
nn=0; |
98 |
|
|
for jj=1:length(ii)-1; |
99 |
|
|
kk=[ii(jj)+1 :ii(jj+1)-1]; |
100 |
|
|
tmp1=msgIn(kk); |
101 |
|
|
if nn+length(tmp1)+1<=widthOut; |
102 |
|
|
msgOut=[msgOut tmp1 ' ']; |
103 |
|
|
nn=nn+length(tmp1)+1; |
104 |
|
|
else; |
105 |
|
|
msgOut=[msgOut char(10)]; |
106 |
|
|
nn=0; |
107 |
|
|
msgOut=[msgOut ' ' tmp1 ' '];%start new line with two spaces |
108 |
|
|
nn=nn+length(tmp1)+1; |
109 |
|
|
end; |
110 |
|
|
end; |
111 |
|
|
msgOut=[msgOut char(10)]; |
112 |
|
|
nn=0; |
113 |
|
|
|
114 |
|
|
%step 4: print to screen |
115 |
|
|
%======= |
116 |
|
|
fprintf(msgOut); |
117 |
|
|
|