prettyformatImpl

Formats an aggregate object, with all its printable members with all their printable values. This is an implementation template and should not be called directly; instead use prettyprint or prettyformat.

private
prettyformatImpl
(
Flag!"all" all = No.all
Flag!"coloured" coloured = Yes.coloured
Sink
Thing
)
(
auto ref Sink sink
,
const bool brightTerminal
,
const auto ref Thing thing
,
const uint typeWidth
,
const uint nameWidth
)

Parameters

all

Whether or not to also display members marked as Unserialisable; usually transitive information that doesn't carry between program runs. Also those annotated Hidden.

coloured

Whether to display in colours or not.

sink Sink

Output range to write to.

thing Thing

Aggregate object to enumerate and format.

typeWidth uint

The width with which to pad type names, to align properly.

nameWidth uint

The width with which to pad variable names, to align properly.

Return Value

Type: auto

The number of total members that were formatted, across all passed objects.

Examples

1     import lu.assert_ : assertMultilineEquals;
2     import std.algorithm.searching : canFind;
3     import std.array : Appender;
4     import std.conv : to;
5 
6     Appender!(char[]) sink;
7     sink.reserve(512);  // ~323
8 
9     {
10         struct Struct
11         {
12             string members;
13             int asdf;
14         }
15 
16         struct StructName
17         {
18             Struct struct_;
19             int i = 12_345;
20             string s = "the moon; the sign of hope! it appeared when we left the pain " ~
21                 "of the ice desert behind. we faced up to the curse and endured " ~
22                 "misery. condemned we are! we brought hope but also lies, and treachery...";
23             string p = "!";
24             string p2;
25             bool b = true;
26             float f = 3.14f;
27             double d = 99.9;
28             const(char)[] c = [ 'a', 'b', 'c' ];
29             const(char)[] emptyC;
30             string[] dynA = [ "foo", "bar", "baz" ];
31             int[] iA = [ 1, 2, 3, 4 ];
32             const(char)[char] cC;
33         }
34 
35         StructName s;
36         s.cC = [ 'k':'v', 'K':'V' ];
37 
38         enum theMoon = `"the moon; the sign of hope! it appeared when we left the ` ~
39             `pain of the ice desert behind. we faced up to the curse and endured mis"`;
40 
41         enum structNameSerialised =
42 `-- StructName
43        Struct struct_                    <struct> (init)
44           int i                           12345
45        string s                          ` ~ theMoon ~ ` ... (198)
46        string p                          "!"(1)
47        string p2                          ""(0)
48          bool b                           true
49         float f                           3.14
50        double d                           99.9
51        char[] c                           ['a', 'b', 'c'](3)
52        char[] emptyC                      [](0)
53      string[] dynA                        ["foo", "bar", "baz"](3)
54         int[] iA                          [1, 2, 3, 4](4)
55    char[char] cC                          ['k':'v', 'K':'V'](2)
56 `;
57 
58         auto numFormatted = sink.prettyformat!(No.all, No.coloured)(brightTerminal: false, s);
59         sink[].assertMultilineEquals(structNameSerialised);
60         assert((numFormatted == 13), numFormatted.to!string);
61         sink.clear();
62 
63         // Class copy
64         class ClassName
65         {
66             Struct struct_;
67             int i = 12_345;
68             string s = "foo";
69             string p = "!";
70             string p2;
71             bool b = true;
72             float f = 3.14f;
73             double d = 99.9;
74             const(char)[] c = [ 'a', 'b', 'c' ];
75             const(char)[] emptyC;
76             string[] dynA = [ "foo", "bar", "baz" ];
77             int[] iA = [ 1, 2, 3, 4 ];
78             int[] iA2 = [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
79             const(char)[char] cC;
80             bool[int] aa;
81             string[string] aa2;
82         }
83 
84         auto c1 = new ClassName;
85         c1.aa = [ 1 : true, 2 : false, 3 : true, 4 : false, 5: true, 6 : false];
86         c1.aa2 = [ "harbl" : "snarbl", "foo" : "bar"];
87 
88         enum classNameSerialised =
89 `-- ClassName
90          Struct struct_                    <struct>
91             int i                           12345
92          string s                          "foo"(3)
93          string p                          "!"(1)
94          string p2                          ""(0)
95            bool b                           true
96           float f                           3.14
97          double d                           99.9
98          char[] c                           ['a', 'b', 'c'](3)
99          char[] emptyC                      [](0)
100        string[] dynA                        ["foo", "bar", "baz"](3)
101           int[] iA                          [1, 2, 3, 4](4)
102           int[] iA2                         [5, 6, 7, 8, 9] ... (11)
103      char[char] cC                          [](0)
104       bool[int] aa                          [6:false, 4:false, 1:true, 3:true, 5:true] ... (6)
105  string[string] aa2                         ["foo":"bar", "harbl":"snarbl"](2)
106 `;
107 
108         numFormatted = sink.prettyformat!(No.all, No.coloured)(brightTerminal: false, c1);
109         sink[].assertMultilineEquals(classNameSerialised);
110         assert((numFormatted == 16), numFormatted.to!string);
111         sink.clear();
112     }
113     {
114         // Two at a time
115         struct Struct1
116         {
117             string members;
118             int asdf;
119         }
120 
121         struct Struct2
122         {
123             string mumburs;
124             int fdsa;
125         }
126 
127         Struct1 st1;
128         Struct2 st2;
129 
130         st1.members = "harbl";
131         st1.asdf = 42;
132         st2.mumburs = "hirrs";
133         st2.fdsa = -1;
134 
135         enum st1st2Formatted =
136 `-- Struct1
137        string members                    "harbl"(5)
138           int asdf                        42
139 
140 -- Struct2
141        string mumburs                    "hirrs"(5)
142           int fdsa                        -1
143 `;
144 
145         immutable numFormatted = sink.prettyformat!(No.all, No.coloured)(brightTerminal: false, st1, st2);
146         sink[].assertMultilineEquals(st1st2Formatted);
147         assert((numFormatted == 4), numFormatted.to!string);
148         sink.clear();
149     }
150     {
151         version(Colours)
152         {
153             // Colour
154             struct StructName2Settings
155             {
156                 int int_ = 12_345;
157                 string string_ = "foo";
158                 bool bool_ = true;
159                 float float_ = 3.14f;
160                 double double_ = 99.9;
161             }
162 
163             StructName2Settings s2;
164 
165             sink.clear();
166             sink.reserve(256);  // ~239
167             immutable numFormatted = sink.prettyformat!(No.all, Yes.coloured)(brightTerminal: false, s2);
168             assert((numFormatted == 5), numFormatted.to!string);
169 
170             assert((sink[].length > 12), "Empty sink after coloured fill");
171 
172             assert(sink[].canFind("-- StructName2\n"));  // Settings stripped
173             assert(sink[].canFind("int_"));
174             assert(sink[].canFind("12345"));
175 
176             assert(sink[].canFind("string_"));
177             assert(sink[].canFind(`"foo"`));
178 
179             assert(sink[].canFind("bool_"));
180             assert(sink[].canFind("true"));
181 
182             assert(sink[].canFind("float_"));
183             assert(sink[].canFind("3.14"));
184 
185             assert(sink[].canFind("double_"));
186             assert(sink[].canFind("99.9"));
187 
188             sink.clear();
189         }
190     }
191     {
192         class C
193         {
194             string a = "abc";
195             bool b = true;
196             int i = 42;
197         }
198 
199         C c2 = new C;
200 
201         enum cFormatted =
202 `-- C
203        string a                          "abc"(3)
204          bool b                           true
205           int i                           42
206 `;
207 
208         immutable numFormatted = sink.prettyformat!(No.all, No.coloured)(brightTerminal: false, c2);
209         sink[].assertMultilineEquals(cFormatted);
210         assert((numFormatted == 3), numFormatted.to!string);
211         sink.clear();
212     }
213     {
214         interface I3
215         {
216             void foo();
217         }
218 
219         class C3 : I3
220         {
221             void foo() {}
222             int i;
223         }
224 
225         class C4
226         {
227             I3 i3;
228             C3 c3;
229             int i = 42;
230         }
231 
232         C4 c4 = new C4;
233         //c4.i3 = new C3;
234         c4.c3 = new C3;
235         c4.c3.i = -1;
236 
237         enum c4Formatted =
238 `-- C4
239            I3 i3                         <interface> (null)
240            C3 c3                         <class>
241           int i                           42
242 
243 -- I3
244 
245 -- C3
246           int i                           -1
247 `;
248 
249         immutable numFormatted = sink.prettyformat!(No.all, No.coloured)(brightTerminal: false, c4, c4.i3, c4.c3);
250         sink[].assertMultilineEquals(c4Formatted);
251         assert((numFormatted == 4), numFormatted.to!string);
252         sink.clear();
253     }