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
pretty print the rest of the primitive types
altagos.dev
1 month ago
e63c09e3
523ab221
verified
This commit was signed with the committer's
known signature
.
altagos.dev
SSH Key Fingerprint:
SHA256:UbTjEcCZlc6GzQWLCuDK3D//HESWD2xFPkzue9XMras=
+40
-15
2 changed files
expand all
collapse all
unified
split
example
main.zig
pretty.zig
+6
-6
example/main.zig
···
10
10
pub fn main(init: std.process.Init) !void {
11
11
_ = init;
12
12
13
13
-
print("Booleans\n", .{});
13
13
+
print("Pretty type - {f}\n", .{pretty(Hello)});
14
14
+
print("Pretty null - {f}\n", .{pretty(null)});
15
15
+
16
16
+
print("\nBooleans\n", .{});
14
17
print("Boolean true - {f}\n", .{pretty(true)});
15
15
-
print(
16
16
-
"Boolean false - {f}\n",
17
17
-
.{Pretty(bool, .{ .show_type_names = false, .type_value_seperator = "" }).init(false)},
18
18
-
);
18
18
+
print("Boolean false - {f}\n", .{pretty(false)});
19
19
print(
20
20
-
"Boolean true with type name - {f}\n",
20
20
+
"Boolean true always with type name - {f}\n",
21
21
.{Pretty(bool, .{ .always_show_type_names = true, .type_value_seperator = ": " }).init(true)},
22
22
);
23
23
+34
-9
pretty.zig
···
100
100
try printType(T, cctx, rctx);
101
101
102
102
return switch (info) {
103
103
+
.type => formatType(rctx, value),
104
104
+
.null => formatNull(rctx),
105
105
+
103
106
.bool => formatBool(rctx, value),
104
107
108
108
+
// comptime types
105
109
.comptime_int,
106
110
.comptime_float,
111
111
+
// number types
107
112
.int,
108
113
.float,
114
114
+
// enum types
109
115
.@"enum",
110
116
.enum_literal,
111
117
=> formatValue(rctx, value),
···
123
129
comptime cctx: ComptimeContext,
124
130
rctx: RuntimeContext,
125
131
) error{WriteFailed}!void {
126
126
-
if (cctx.depth != 0 or cctx.options.always_show_type_names) {
132
132
+
const active_type = comptime std.meta.activeTag(@typeInfo(T));
133
133
+
const excluded = comptime switch (active_type) {
134
134
+
.void, .noreturn, .undefined, .null => true,
135
135
+
else => false,
136
136
+
};
137
137
+
138
138
+
if ((cctx.depth != 0 or cctx.options.always_show_type_names) and !excluded) {
127
139
rctx.setColor(.dim);
128
140
129
141
if (cctx.options.show_type_names) {
···
135
147
}
136
148
}
137
149
138
138
-
inline fn formatBool(rctx: RuntimeContext, value: bool) !void {
139
139
-
rctx.setColor(if (value) .bright_green else .bright_red);
140
140
-
try rctx.print("{}", .{value});
141
141
-
rctx.resetColor();
150
150
+
inline fn formatBool(ctx: RuntimeContext, value: bool) !void {
151
151
+
ctx.setColor(if (value) .bright_green else .bright_red);
152
152
+
try ctx.print("{}", .{value});
153
153
+
ctx.resetColor();
154
154
+
}
155
155
+
156
156
+
inline fn formatNull(ctx: RuntimeContext) !void {
157
157
+
ctx.setColor(.cyan);
158
158
+
try ctx.write("null");
159
159
+
ctx.resetColor();
160
160
+
}
161
161
+
162
162
+
inline fn formatType(ctx: RuntimeContext, value: type) !void {
163
163
+
ctx.setColor(.bright_blue);
164
164
+
ctx.setColor(.bold);
165
165
+
try ctx.write(@typeName(value));
166
166
+
ctx.resetColor();
142
167
}
143
168
144
144
-
inline fn formatValue(rctx: RuntimeContext, value: anytype) !void {
145
145
-
rctx.setColor(.blue);
146
146
-
try rctx.print("{}", .{value});
147
147
-
rctx.resetColor();
169
169
+
inline fn formatValue(ctx: RuntimeContext, value: anytype) !void {
170
170
+
ctx.setColor(.blue);
171
171
+
try ctx.print("{}", .{value});
172
172
+
ctx.resetColor();
148
173
}