formatObjects

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 formatObjects(Sink sink, Flag!"brightTerminal" bright, Things things)
  2. string formatObjects(Flag!"brightTerminal" bright, Things things)
    string
    formatObjects
    (
    Flag!"all" all = No.all
    Flag!"coloured" coloured = Yes.coloured
    Things...
    )
    (
    const Flag!"brightTerminal" bright
    ,
    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(formatObjects!(No.all, Yes.coloured)(foo));
writeln(formatObjects!(Yes.all, No.coloured)(bar));
1     // Rely on the main unit tests of the output range version of formatObjects
2 
3     struct Struct
4     {
5         string members;
6         int asdf;
7     }
8 
9     Struct s;
10     s.members = "foo";
11     s.asdf = 42;
12 
13     immutable formatted = formatObjects!(No.all, No.coloured)(No.brightTerminal, s);
14     assert((formatted ==
15 `-- Struct
16    string members                    "foo"(3)
17       int asdf                        42
18 `), '\n' ~ formatted);
19 
20     class Nested
21     {
22         int harbl;
23         string snarbl;
24     }
25 
26     class ClassSettings
27     {
28         string s = "arb";
29         int i;
30         string someLongConfiguration = "acdc adcadcad acacdadc";
31         int[] arrMatey = [ 1, 2, 3, 42 ];
32         Nested nest;
33     }
34 
35     auto c = new ClassSettings;
36     c.i = 2;
37 
38     immutable formattedClass = formatObjects!(No.all, No.coloured)(No.brightTerminal, c);
39     assert((formattedClass ==
40 `-- Class
41    string s                          "arb"(3)
42       int i                           2
43    string someLongConfiguration      "acdc adcadcad acacdadc"(22)
44     int[] arrMatey                   [1, 2, 3, 42](4)
45    Nested nest                       <class> (null)
46 `), '\n' ~ formattedClass);
47 
48     c.nest = new Nested;
49     immutable formattedClass2 = formatObjects!(No.all, No.coloured)(No.brightTerminal, c);
50     assert((formattedClass2 ==
51 `-- Class
52    string s                          "arb"(3)
53       int i                           2
54    string someLongConfiguration      "acdc adcadcad acacdadc"(22)
55     int[] arrMatey                   [1, 2, 3, 42](4)
56    Nested nest                       <class>
57 `), '\n' ~ formattedClass2);
58 
59     struct Reparse {}
60     struct Client {}
61     struct Server {}
62 
63     struct State
64     {
65         Client client;
66         Server server;
67         Reparse[] reparses;
68         bool hasReplays;
69     }
70 
71     State state;
72 
73     immutable formattedState = formatObjects!(No.all, No.coloured)(No.brightTerminal, state);
74     assert((formattedState ==
75 `-- State
76     Client client                     <struct> (init)
77     Server server                     <struct> (init)
78  Reparse[] reparses                    [](0)
79       bool hasReplays                  false
80 `), '\n' ~ formattedState);

Meta