prettyformat

Formats a struct object, with all its printable members with all their printable values. A string-returning overload that doesn't take an input range.

This is useful when you just want the object(s) formatted without having to pass it a sink.

  1. void prettyformat(Sink sink, Flag!"brightTerminal" bright, Things things)
  2. string prettyformat(Flag!"brightTerminal" bright, Things things)
    pure
    string
    prettyformat
    (
    Flag!"all" all = No.all
    Flag!"coloured" coloured = Yes.coloured
    Things...
    )
    (
    const Flag!"brightTerminal" bright
    ,
    const auto ref Things things
    )
    if (
    (Things.length > 0) &&
    !isOutputRange!(Things[0], char[])
    )

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.

bright Flag!"brightTerminal"

Whether or not to format for a bright terminal background.

things Things

Variadic list of structs to enumerate and format.

Return Value

Type: string

String with the object formatted, as per the passed arguments.

Examples

struct Foo
{
    int foo = 42;
    string bar = "arr matey";
    float f = 3.14f;
    double d = 9.99;
}

Foo foo, bar;

writeln(prettyformat!(No.all, Yes.coloured)(foo));
writeln(prettyformat!(Yes.all, No.coloured)(bar));
1     import kameloso.common : assertMultilineOpEquals;
2 
3     // Rely on the main unit tests of the output range version of prettyformat
4     {
5         struct Struct
6         {
7             string members;
8             int asdf;
9         }
10 
11         Struct s;
12         s.members = "foo";
13         s.asdf = 42;
14 
15         enum expected =
16 `-- Struct
17        string members                    "foo"(3)
18           int asdf                        42
19 `;
20 
21         immutable actual = prettyformat!(No.all, No.coloured)(No.brightTerminal, s);
22         actual.assertMultilineOpEquals(expected);
23     }
24     {
25         class Nested
26         {
27             int harbl;
28             string snarbl;
29         }
30 
31         class ClassSettings
32         {
33             string s = "arb";
34             int i;
35             string someLongConfiguration = "acdc adcadcad acacdadc";
36             int[] arrMatey = [ 1, 2, 3, 42 ];
37             Nested nest;
38         }
39 
40         auto c = new ClassSettings;
41         c.i = 2;
42 
43         enum expected =
44 `-- Class
45        string s                          "arb"(3)
46           int i                           2
47        string someLongConfiguration      "acdc adcadcad acacdadc"(22)
48         int[] arrMatey                   [1, 2, 3, 42](4)
49        Nested nest                       <class> (null)
50 `;
51 
52         immutable actual = prettyformat!(No.all, No.coloured)(No.brightTerminal, c);
53         actual.assertMultilineOpEquals(expected);
54 
55         c.nest = new Nested;
56         enum expected2 =
57 `-- Class
58        string s                          "arb"(3)
59           int i                           2
60        string someLongConfiguration      "acdc adcadcad acacdadc"(22)
61         int[] arrMatey                   [1, 2, 3, 42](4)
62        Nested nest                       <class>
63 `;
64 
65         immutable actual2 = prettyformat!(No.all, No.coloured)(No.brightTerminal, c);
66         actual2.assertMultilineOpEquals(expected2);
67     }
68     {
69         struct Reparse {}
70         struct Client {}
71         struct Server {}
72 
73         struct State
74         {
75             Client client;
76             Server server;
77             Reparse[] reparses;
78             bool hasReplays;
79         }
80 
81         State state;
82 
83         enum expected =
84 `-- State
85        Client client                     <struct> (init)
86        Server server                     <struct> (init)
87     Reparse[] reparses                    [](0)
88          bool hasReplays                  false
89 `;
90 
91         immutable actual = prettyformat!(No.all, No.coloured)(No.brightTerminal, state);
92         actual.assertMultilineOpEquals(expected);
93     }