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
void
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.

Examples

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