tangled
alpha
login
or
join now
altagos.dev
/
pretty
0
fork
atom
A pretty printer for zig
zig
0
fork
atom
overview
issues
pulls
pipelines
rename symbols
altagos.dev
1 month ago
f506de43
b657057a
verified
This commit was signed with the committer's
known signature
.
altagos.dev
SSH Key Fingerprint:
SHA256:UbTjEcCZlc6GzQWLCuDK3D//HESWD2xFPkzue9XMras=
+85
-85
1 changed file
expand all
collapse all
unified
split
pretty.zig
+85
-85
pretty.zig
···
15
15
};
16
16
17
17
pub const Theme = struct {
18
18
-
pub const Colors = enum {
18
18
+
pub const Color = enum {
19
19
dim,
20
20
field,
21
21
value,
···
38
38
color_true: Io.Terminal.Color = .bright_green,
39
39
color_false: Io.Terminal.Color = .bright_red,
40
40
41
41
-
pub inline fn getColor(comptime this: Theme, comptime color: Colors) Io.Terminal.Color {
41
41
+
pub inline fn getColor(comptime this: Theme, comptime color: Color) Io.Terminal.Color {
42
42
return switch (color) {
43
43
inline else => |col| @field(this, "color_" ++ @tagName(col)),
44
44
};
···
59
59
const global = struct {
60
60
var tty: ?Io.Terminal = null;
61
61
};
62
62
-
const cctx: ComptimeContext = ComptimeContext{ .options = options };
62
62
+
const ctx: Context = Context{ .options = options };
63
63
64
64
return struct {
65
65
value: T,
···
78
78
global.tty.?.writer = w;
79
79
}
80
80
81
81
-
var rctx = RuntimeContext{
81
81
+
var run = Runtime{
82
82
.out = w,
83
83
.tty = global.tty.?,
84
84
};
85
85
86
86
-
return innerFmt(T, cctx, &rctx, this.value, .{});
86
86
+
return innerFmt(T, ctx, &run, this.value, .{});
87
87
}
88
88
};
89
89
}
90
90
91
91
-
const RuntimeContext = struct {
91
91
+
const Runtime = struct {
92
92
out: *Io.Writer,
93
93
tty: Io.Terminal,
94
94
···
97
97
indent_level: usize = 0,
98
98
99
99
pub inline fn print(
100
100
-
this: *const RuntimeContext,
100
100
+
this: *const Runtime,
101
101
comptime fmt: []const u8,
102
102
args: anytype,
103
103
) error{WriteFailed}!void {
104
104
return this.out.print(fmt, args);
105
105
}
106
106
107
107
-
pub inline fn write(this: *const RuntimeContext, text: []const u8) error{WriteFailed}!void {
107
107
+
pub inline fn write(this: *const Runtime, text: []const u8) error{WriteFailed}!void {
108
108
return this.out.writeAll(text);
109
109
}
110
110
111
111
pub inline fn setColor(
112
112
-
this: *const RuntimeContext,
113
113
-
comptime ctx: ComptimeContext,
114
114
-
comptime color: Theme.Colors,
112
112
+
this: *const Runtime,
113
113
+
comptime ctx: Context,
114
114
+
comptime color: Theme.Color,
115
115
) void {
116
116
this.tty.setColor(ctx.options.theme.getColor(color)) catch {};
117
117
}
118
118
119
119
-
pub inline fn setColorRaw(this: *const RuntimeContext, color: Io.Terminal.Color) void {
119
119
+
pub inline fn setColorRaw(this: *const Runtime, color: Io.Terminal.Color) void {
120
120
this.tty.setColor(color) catch {};
121
121
}
122
122
123
123
-
pub inline fn resetColor(this: *const RuntimeContext) void {
123
123
+
pub inline fn resetColor(this: *const Runtime) void {
124
124
this.tty.setColor(.reset) catch {};
125
125
}
126
126
};
127
127
128
128
-
const ComptimeContext = struct {
128
128
+
const Context = struct {
129
129
depth: comptime_int = 0,
130
130
131
131
indent_level: comptime_int = 0,
···
133
133
options: Options,
134
134
exited_comptime: bool = false,
135
135
136
136
-
pub fn inComptime(comptime this: ComptimeContext) bool {
136
136
+
pub fn inComptime(comptime this: Context) bool {
137
137
if (!this.exited_comptime) return false;
138
138
return @inComptime();
139
139
}
···
145
145
146
146
fn innerFmt(
147
147
comptime T: type,
148
148
-
comptime cctx: ComptimeContext,
149
149
-
rctx: *RuntimeContext,
148
148
+
comptime ctx: Context,
149
149
+
run: *Runtime,
150
150
value: T,
151
151
opts: InnerFmtOptions,
152
152
) error{WriteFailed}!void {
153
153
const info = @typeInfo(T);
154
154
155
155
-
if (opts.dont_skip_type_name) try printType(T, cctx, rctx);
155
155
+
if (opts.dont_skip_type_name) try printType(T, ctx, run);
156
156
157
157
return switch (info) {
158
158
-
.bool => formatBool(cctx, rctx, value),
159
159
-
.null => formatNull(cctx, rctx),
160
160
-
.type => formatType(cctx, rctx, value),
158
158
+
.bool => formatBool(ctx, run, value),
159
159
+
.null => formatNull(ctx, run),
160
160
+
.type => formatType(ctx, run, value),
161
161
162
162
// comptime types
163
163
.comptime_int,
···
168
168
// enum types
169
169
.@"enum",
170
170
.enum_literal,
171
171
-
=> formatValue(cctx, rctx, value),
171
171
+
=> formatValue(ctx, run, value),
172
172
173
173
-
.optional => |opt| formatOptional(opt.child, cctx, rctx, value),
174
174
-
.@"struct" => |st| formatStruct(T, st, cctx, rctx, value),
173
173
+
.optional => |opt| formatOptional(opt.child, ctx, run, value),
174
174
+
.@"struct" => |st| formatStruct(T, st, ctx, run, value),
175
175
176
176
else => {
177
177
-
rctx.setColor(cctx, .@"error");
178
178
-
try rctx.print("Unimplemented! ({} = {any})", .{ info, value });
179
179
-
rctx.resetColor();
177
177
+
run.setColor(ctx, .@"error");
178
178
+
try run.print("Unimplemented! ({} = {any})", .{ info, value });
179
179
+
run.resetColor();
180
180
},
181
181
};
182
182
}
183
183
184
184
inline fn printType(
185
185
comptime T: type,
186
186
-
comptime cctx: ComptimeContext,
187
187
-
rctx: *RuntimeContext,
186
186
+
comptime ctx: Context,
187
187
+
run: *Runtime,
188
188
) error{WriteFailed}!void {
189
189
const active_type = comptime std.meta.activeTag(@typeInfo(T));
190
190
const excluded = comptime switch (active_type) {
···
192
192
else => false,
193
193
};
194
194
195
195
-
if ((cctx.depth != 0 or cctx.options.always_show_type_names) and !excluded) {
196
196
-
rctx.setColor(cctx, .dim);
195
195
+
if ((ctx.depth != 0 or ctx.options.always_show_type_names) and !excluded) {
196
196
+
run.setColor(ctx, .dim);
197
197
198
198
-
if (cctx.options.show_type_names) {
199
199
-
try rctx.write(@typeName(T));
198
198
+
if (ctx.options.show_type_names) {
199
199
+
try run.write(@typeName(T));
200
200
}
201
201
-
try rctx.write(cctx.options.theme.type_value_sep);
201
201
+
try run.write(ctx.options.theme.type_value_sep);
202
202
203
203
-
rctx.resetColor();
203
203
+
run.resetColor();
204
204
}
205
205
}
206
206
207
207
inline fn formatBool(
208
208
-
comptime cctx: ComptimeContext,
209
209
-
rctx: *const RuntimeContext,
208
208
+
comptime ctx: Context,
209
209
+
run: *const Runtime,
210
210
value: bool,
211
211
) !void {
212
212
if (value)
213
213
-
rctx.setColor(cctx, .true)
213
213
+
run.setColor(ctx, .true)
214
214
else
215
215
-
rctx.setColor(cctx, .false);
215
215
+
run.setColor(ctx, .false);
216
216
217
217
-
try rctx.print("{}", .{value});
218
218
-
rctx.resetColor();
217
217
+
try run.print("{}", .{value});
218
218
+
run.resetColor();
219
219
}
220
220
221
221
-
inline fn formatNull(comptime cctx: ComptimeContext, rctx: *const RuntimeContext) !void {
222
222
-
rctx.setColor(cctx, .null);
223
223
-
try rctx.write("null");
224
224
-
rctx.resetColor();
221
221
+
inline fn formatNull(comptime ctx: Context, run: *const Runtime) !void {
222
222
+
run.setColor(ctx, .null);
223
223
+
try run.write("null");
224
224
+
run.resetColor();
225
225
}
226
226
227
227
inline fn formatOptional(
228
228
comptime T: type,
229
229
-
comptime cctx: ComptimeContext,
230
230
-
rctx: *RuntimeContext,
229
229
+
comptime ctx: Context,
230
230
+
run: *Runtime,
231
231
value: ?T,
232
232
) !void {
233
233
return if (value) |val|
234
234
-
innerFmt(T, cctx, rctx, val, .{ .dont_skip_type_name = false })
234
234
+
innerFmt(T, ctx, run, val, .{ .dont_skip_type_name = false })
235
235
else
236
236
-
formatNull(cctx, rctx);
236
236
+
formatNull(ctx, run);
237
237
}
238
238
239
239
inline fn formatStruct(
240
240
comptime T: type,
241
241
comptime st: Type.Struct,
242
242
-
comptime cctx: ComptimeContext,
243
243
-
rctx: *RuntimeContext,
242
242
+
comptime ctx: Context,
243
243
+
run: *Runtime,
244
244
value: T,
245
245
) !void {
246
246
-
const next_cctx = ComptimeContext{
247
247
-
.depth = cctx.depth + 1,
248
248
-
.indent_level = cctx.indent_level + 1,
249
249
-
.exited_comptime = cctx.exited_comptime,
250
250
-
.options = cctx.options,
246
246
+
const next_ctx = Context{
247
247
+
.depth = ctx.depth + 1,
248
248
+
.indent_level = ctx.indent_level + 1,
249
249
+
.exited_comptime = ctx.exited_comptime,
250
250
+
.options = ctx.options,
251
251
};
252
252
253
253
-
if (cctx.options.inline_structs) {
254
254
-
rctx.setColor(cctx, .dim);
255
255
-
try rctx.write(".{ ");
256
256
-
rctx.resetColor();
253
253
+
if (ctx.options.inline_structs) {
254
254
+
run.setColor(ctx, .dim);
255
255
+
try run.write(".{ ");
256
256
+
run.resetColor();
257
257
}
258
258
259
259
comptime var index: comptime_int = 0;
260
260
inline for (st.fields) |field| {
261
261
-
indent(next_cctx, rctx);
262
262
-
if (index != 0 and cctx.options.inline_structs) try rctx.write(", ");
261
261
+
indent(next_ctx, run);
262
262
+
if (index != 0 and ctx.options.inline_structs) try run.write(", ");
263
263
264
264
-
rctx.setColor(cctx, .field);
265
265
-
try rctx.write("." ++ field.name ++ cctx.options.theme.field_name_type_sep);
266
266
-
rctx.resetColor();
264
264
+
run.setColor(ctx, .field);
265
265
+
try run.write("." ++ field.name ++ ctx.options.theme.field_name_type_sep);
266
266
+
run.resetColor();
267
267
268
268
-
try innerFmt(field.type, next_cctx, rctx, @field(value, field.name), .{});
268
268
+
try innerFmt(field.type, next_ctx, run, @field(value, field.name), .{});
269
269
270
270
index += 1;
271
271
}
272
272
273
273
-
if (cctx.options.inline_structs) {
274
274
-
rctx.setColor(cctx, .dim);
275
275
-
try rctx.write(" }");
276
276
-
rctx.resetColor();
273
273
+
if (ctx.options.inline_structs) {
274
274
+
run.setColor(ctx, .dim);
275
275
+
try run.write(" }");
276
276
+
run.resetColor();
277
277
}
278
278
}
279
279
280
280
inline fn formatType(
281
281
-
comptime cctx: ComptimeContext,
282
282
-
rctx: *const RuntimeContext,
281
281
+
comptime ctx: Context,
282
282
+
run: *const Runtime,
283
283
value: type,
284
284
) !void {
285
285
-
rctx.setColor(cctx, .type);
286
286
-
rctx.setColorRaw(.bold);
287
287
-
try rctx.write(@typeName(value));
288
288
-
rctx.resetColor();
285
285
+
run.setColor(ctx, .type);
286
286
+
run.setColorRaw(.bold);
287
287
+
try run.write(@typeName(value));
288
288
+
run.resetColor();
289
289
}
290
290
291
291
inline fn formatValue(
292
292
-
comptime cctx: ComptimeContext,
293
293
-
rctx: *const RuntimeContext,
292
292
+
comptime ctx: Context,
293
293
+
run: *const Runtime,
294
294
value: anytype,
295
295
) !void {
296
296
-
rctx.setColor(cctx, .value);
297
297
-
try rctx.print("{}", .{value});
298
298
-
rctx.resetColor();
296
296
+
run.setColor(ctx, .value);
297
297
+
try run.print("{}", .{value});
298
298
+
run.resetColor();
299
299
}
300
300
301
301
-
inline fn indent(comptime cctx: ComptimeContext, rctx: *const RuntimeContext) void {
302
302
-
if (cctx.options.inline_structs) return;
301
301
+
inline fn indent(comptime ctx: Context, run: *const Runtime) void {
302
302
+
if (ctx.options.inline_structs) return;
303
303
304
304
-
const text: [cctx.depth * cctx.options.indent_width]u8 = @splat(' ');
305
305
-
rctx.write("\n" ++ text) catch {};
304
304
+
const text: [ctx.depth * ctx.options.indent_width]u8 = @splat(' ');
305
305
+
run.write("\n" ++ text) catch {};
306
306
}