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
improve consistency
altagos.dev
1 month ago
07bf4d26
99153376
verified
This commit was signed with the committer's
known signature
.
altagos.dev
SSH Key Fingerprint:
SHA256:UbTjEcCZlc6GzQWLCuDK3D//HESWD2xFPkzue9XMras=
+32
-26
2 changed files
expand all
collapse all
unified
split
example
main.zig
pretty.zig
+18
-18
example/main.zig
···
3
3
4
4
const pretty_mod = @import("pretty");
5
5
const pretty = pretty_mod.pretty;
6
6
-
const Pretty = pretty_mod.PrettyWithOptions;
6
6
+
const prettyO = pretty_mod.prettyO;
7
7
8
8
pub const pretty_options = pretty_mod.Options{
9
9
.always_show_type_names = true,
···
45
45
print("Pretty null - {f}\n", .{pretty(null)});
46
46
47
47
print("\nBooleans\n", .{});
48
48
-
print("Boolean true - {f}\n", .{pretty(true)});
49
49
-
print("Boolean false - {f}\n", .{pretty(false)});
48
48
+
print("Pretty bool true - {f}\n", .{pretty(true)});
49
49
+
print("Pretty bool false - {f}\n", .{pretty(false)});
50
50
print(
51
51
-
"Boolean true always with type name - {f}\n",
52
52
-
.{Pretty(bool, .{
51
51
+
"Pretty bool true always with type name - {f}\n",
52
52
+
.{prettyO(true, .{
53
53
.always_show_type_names = true,
54
54
.theme = .{ .type_value_sep = ": " },
55
55
-
}).init(true)},
55
55
+
})},
56
56
);
57
57
58
58
print("\nUnsigned Integers\n", .{});
···
101
101
print("Pretty simple struct - {f}\n", .{pretty(person)});
102
102
print(
103
103
"Pretty simple inline struct - {f}\n",
104
104
-
.{Pretty(Person, .{ .inline_structs = true }).init(person)},
104
104
+
.{prettyO(person, .{ .inline_structs = true })},
105
105
);
106
106
print("Pretty nested struct - {f}\n", .{pretty(nested)});
107
107
108
108
print("\nArrays\n", .{});
109
109
-
print("Array of floats - {f}\n", .{pretty([_]f32{ 0.1, 0.2, 0.3 })});
109
109
+
print("Pretty array of floats - {f}\n", .{pretty([_]f32{ 0.1, 0.2, 0.3 })});
110
110
print(
111
111
-
"Array of floats inline - {f}\n",
112
112
-
.{Pretty([3]f32, .{ .inline_arrays = true }).init([_]f32{ 0.1, 0.2, 0.3 })},
111
111
+
"Pretty array of floats inline - {f}\n",
112
112
+
.{prettyO([3]f32{ 0.1, 0.2, 0.3 }, .{ .inline_arrays = true })},
113
113
);
114
114
print(
115
115
-
"Array of floats inline with indices - {f}\n",
116
116
-
.{Pretty([3]f32, .{
115
115
+
"Pretty array of floats inline with indices - {f}\n",
116
116
+
.{prettyO([3]f32{ 0.1, 0.2, 0.3 }, .{
117
117
.inline_arrays = true,
118
118
.always_show_index = true,
119
119
-
}).init([_]f32{ 0.1, 0.2, 0.3 })},
119
119
+
})},
120
120
);
121
121
122
122
const eu_error: Result = error.OutOfMemory;
123
123
const eu_ok: Result = .world;
124
124
125
125
print("\nError types\n", .{});
126
126
-
print("Error set - {f}\n", .{pretty(ErrorSet.OutOfMemory)});
127
127
-
print("Error union error - {f}\n", .{pretty(eu_error)});
128
128
-
print("Error union ok - {f}\n", .{pretty(eu_ok)});
126
126
+
print("Pretty error set - {f}\n", .{pretty(ErrorSet.OutOfMemory)});
127
127
+
print("Pretty error union error - {f}\n", .{pretty(eu_error)});
128
128
+
print("Pretty error union ok - {f}\n", .{pretty(eu_ok)});
129
129
130
130
print("\nFunctions\n", .{});
131
131
-
print("Function Pretty - {f}\n", .{pretty(Pretty)});
132
132
-
print("Function main - {f}\n", .{pretty(main)});
131
131
+
print("Pretty function pretty - {f}\n", .{pretty(pretty)});
132
132
+
print("Pretty function main - {f}\n", .{pretty(main)});
133
133
}
+14
-8
pretty.zig
···
2
2
const Io = std.Io;
3
3
const Type = std.builtin.Type;
4
4
5
5
+
pub fn pretty(value: anytype) Pretty(@TypeOf(value)) {
6
6
+
return Pretty(@TypeOf(value)).init(value);
7
7
+
}
8
8
+
9
9
+
pub fn prettyO(value: anytype, comptime opts: Options) PrettyWithOptions(@TypeOf(value), opts) {
10
10
+
return PrettyWithOptions(@TypeOf(value), opts).init(value);
11
11
+
}
12
12
+
5
13
const root = @import("root");
14
14
+
const default_options: Options = if (@hasDecl(root, "pretty_options"))
15
15
+
root.pretty_options
16
16
+
else
17
17
+
Options{};
6
18
7
19
pub const Options = struct {
8
20
indent_width: comptime_int = 2,
···
55
67
}
56
68
};
57
69
58
58
-
const default_options: Options = if (@hasDecl(root, "pretty_options")) root.pretty_options else .{};
59
59
-
60
60
-
pub fn pretty(value: anytype) Pretty(@TypeOf(value)) {
61
61
-
return Pretty(@TypeOf(value)).init(value);
62
62
-
}
63
63
-
64
70
pub fn Pretty(comptime T: type) type {
65
71
return PrettyWithOptions(T, default_options);
66
72
}
···
178
184
=> formatValue(ctx, run, value),
179
185
180
186
.optional => |opt| formatOptional(opt.child, ctx, run, value),
181
181
-
.@"struct" => |st| formatStruct(T, st, ctx, run, value),
187
187
+
.@"struct" => |st| formatStruct(T, ctx, st, run, value),
182
188
183
189
.error_set => formatErrorSet(ctx, run, value),
184
190
.error_union => formatErrorUnion(ctx, run, value),
···
254
260
255
261
inline fn formatStruct(
256
262
comptime T: type,
257
257
-
comptime st: Type.Struct,
258
263
comptime ctx: Context,
264
264
+
comptime st: Type.Struct,
259
265
run: *Runtime,
260
266
value: T,
261
267
) !void {