-
Notifications
You must be signed in to change notification settings - Fork 2
/
writer.d
128 lines (106 loc) · 4.67 KB
/
writer.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module sidero.base.console.internal.writer;
import sidero.base.console.internal.rawwrite;
import sidero.base.console.inbandinfo;
import sidero.base.allocators;
import sidero.base.text;
import sidero.base.traits;
struct Writer {
uint prettyPrintDepth;
bool prettyPrintActive, deliminateArguments, setPrettyDelim;
bool isFirstPrettyPrint;
bool useErrorStream;
export @safe nothrow @nogc:
void handle(Args...)(scope Args args) @trusted {
static if (Args.length == 1) {
alias ArgType = typeof(args[0]);
handleOneWrite!ArgType(this, 0, args[0]);
} else {
size_t gotPrintable, printing;
bool wasDeliminted;
static foreach (i; 0 .. Args.length) {
{
alias ArgType = typeof(args[i]);
static if (!is(ArgType == InBandInfo)) {
if (deliminateArguments) {
if (gotPrintable > 0)
rawWriteImpl(String_UTF8(", "), useErrorStream);
gotPrintable++;
wasDeliminted = true;
} else if (wasDeliminted) {
static if (!(isAnyString!ArgType)) {
rawWriteImpl(String_UTF8(", "), useErrorStream);
} else {
if (args[i] != "\n")
rawWriteImpl(String_UTF8(", "), useErrorStream);
}
wasDeliminted = false;
}
}
handleOneWrite!ArgType(this, printing, args[i]);
static if (!is(ArgType == InBandInfo))
printing++;
if (!deliminateArguments && is(ArgType == InBandInfo))
gotPrintable = 0;
}
}
}
rawWriteImpl(String_UTF8(""), useErrorStream);
}
}
private:
void handleOneWrite(Type)(scope ref Writer writer, size_t argumentId, scope ref Type arg) {
static void perform(scope ref Writer writer, size_t argumentId, scope ref Type arg) {
static if (isAnyString!Type) {
if (writer.deliminateArguments) {
StringBuilder_UTF8 builder;
builder ~= arg;
builder.escape('"');
builder.prepend(`"`);
builder.append(`"`);
rawWriteImpl(builder, writer.useErrorStream);
} else {
static if (isReadOnlyString!Type || isBuilderString!Type) {
auto temp = arg.byUTF8;
rawWriteImpl(temp, writer.useErrorStream);
} else {
rawWriteImpl(String_UTF8(arg), writer.useErrorStream);
}
}
} else static if (is(Type == InBandInfo)) {
if (arg.resetDefaults) {
writer.prettyPrintDepth = 0;
writer.prettyPrintActive = false;
writer.deliminateArguments = false;
writer.setPrettyDelim = false;
writer.isFirstPrettyPrint = true;
}
if (!arg.prettyPrintActive.isNull)
writer.prettyPrintActive = arg.prettyPrintActive.get;
if (!arg.deliminateArguments.isNull)
writer.deliminateArguments = arg.deliminateArguments.get;
if (!writer.setPrettyDelim && !arg.prettyPrintActive.isNull && arg.deliminateArguments.isNull) {
writer.deliminateArguments = arg.prettyPrintActive.get;
writer.setPrettyDelim = true;
}
if (!arg.useError.isNull)
writer.useErrorStream = arg.useError.get;
rawWriteImpl(arg, writer.useErrorStream);
} else {
StringBuilder_UTF8 builder = StringBuilder_UTF8(globalAllocator());
if (writer.prettyPrintActive) {
PrettyPrint prettyPrint = PrettyPrint.defaults;
prettyPrint.useQuotes = writer.deliminateArguments;
prettyPrint.startWithoutPrefix = argumentId == 0 || !writer.deliminateArguments;
if (!writer.isFirstPrettyPrint)
builder ~= "\n";
writer.isFirstPrettyPrint = false;
prettyPrint.depth = writer.prettyPrintDepth;
prettyPrint(builder, arg);
} else {
builder.formattedWrite("", arg);
}
rawWriteImpl(builder, writer.useErrorStream);
}
}
(cast(void function(scope ref Writer, size_t, scope ref Type)@safe nothrow @nogc)&perform)(writer, argumentId, arg);
}