From 2aff87926654946c2201b3f822fb61a7eccb86b3 Mon Sep 17 00:00:00 2001 From: Altagos Date: Sun, 25 Jan 2026 11:04:07 +0000 Subject: [PATCH] pretty pointers --- pretty.zig | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- example/employees.json | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ example/main.zig | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 file(s) changed, 241 insertion(s)(+), 4 deletion(s)(-) diff --git a/pretty.zig b/pretty.zig --- a/pretty.zig +++ b/pretty.zig @@ -24,7 +24,10 @@ inline_arrays: bool = false, always_show_index: bool = false, - treat_u8_arrays_as_strings: bool = true, + + treat_u8_ptr_as_string: bool = true, + treat_u8_ptr_slice_as_string: bool = true, + inline_slice: bool = false, inline_structs: bool = false, @@ -41,6 +44,8 @@ value, @"error", type, + string, + many_ptr, null, true, false, @@ -58,6 +63,8 @@ color_value: Io.Terminal.Color = .blue, color_error: Io.Terminal.Color = .red, color_type: Io.Terminal.Color = .bright_blue, + color_string: Io.Terminal.Color = .magenta, + color_many_ptr: Io.Terminal.Color = .magenta, color_null: Io.Terminal.Color = .cyan, color_true: Io.Terminal.Color = .bright_green, color_false: Io.Terminal.Color = .bright_red, @@ -206,10 +213,20 @@ .error_union => formatErrorUnion(ctx, run, value), .array => |arr| formatArray(ctx, arr, run, value), - // TODO: .pointer => |ptr| {}, .vector => |vec| formatVector(ctx, vec, run, value), .@"fn" => formatFn(T, ctx, run), + + .pointer => |ptr| switch (ptr.size) { + .one => formatPtrOne(ctx, ptr, run, value), + .slice => formatPtrSlice(ctx, ptr, run, value), + .many => formatPtrMany(ctx, ptr, run, value), + else => { + run.setColor(ctx, .@"error"); + try run.print("Unimplemented! ({} = {any})", .{ info, value }); + run.resetColor(); + }, + }, else => { run.setColor(ctx, .@"error"); @@ -413,6 +430,91 @@ run.setColor(ctx, .dim); try run.write(" )"); + run.resetColor(); +} + +inline fn formatPtrOne( + comptime ctx: Context, + comptime ptr: Type.Pointer, + run: *Runtime, + value: anytype, +) !void { + const is_string = switch (@typeInfo(ptr.child)) { + .array => |arr| arr.child == u8 and ctx.options.treat_u8_ptr_as_string, + else => false, + }; + if (is_string) return formatString(ctx, run, value); + + try innerFmt(ptr.child, ctx, run, value.*, .{ .skip_type_name = true }); +} + +inline fn formatPtrSlice( + comptime ctx: Context, + comptime ptr: Type.Pointer, + run: *Runtime, + value: anytype, +) !void { + if (ptr.child == u8 and ctx.options.treat_u8_ptr_slice_as_string) + return formatString(ctx, run, value); + + const next_ctx = Context{ + .depth = ctx.depth + 1, + .exited_comptime = ctx.exited_comptime, + .options = ctx.options, + }; + + if (ctx.options.inline_slice) { + run.setColor(ctx, .dim); + try run.write("{ "); + run.resetColor(); + } + + for (value, 0..) |val, idx| { + indent( + next_ctx, + next_ctx.options.inline_slice, + run, + ); + + run.setColor(ctx, .dim); + if (idx != 0 and ctx.options.inline_slice) + try run.write(", "); + + if (!ctx.options.inline_slice or ctx.options.always_show_index) { + try run.write(ctx.options.theme.index_open); + try run.print("{}", .{idx}); + try run.write(ctx.options.theme.index_close ++ + ctx.options.theme.index_value_sep); + } + + run.resetColor(); + + try innerFmt(ptr.child, next_ctx, run, val, .{ .skip_type_name = true }); + } +} + +inline fn formatPtrMany( + comptime ctx: Context, + comptime ptr: Type.Pointer, + run: *Runtime, + value: anytype, +) !void { + _ = ptr; + run.setColor(ctx, .dim); + run.setColor(ctx, .many_ptr); + try run.print("{*}", .{value}); + run.resetColor(); +} + +inline fn formatString( + comptime ctx: Context, + run: *Runtime, + value: anytype, +) !void { + run.setColor(ctx, .string); + try run.write("\""); + try run.write(value); + try run.write("\""); run.resetColor(); } diff --git a/example/employees.json b/example/employees.json new file mode 100644 --- /dev/null +++ b/example/employees.json @@ -0,0 +1,56 @@ +{ + "employee": { + "id": "E00001", + "name": "Bryan Smith", + "position": "Chemical engineer", + "department": { + "id": "D080", + "name": "Incentivize", + "manager": { + "id": "M2858", + "name": "Rebecca Andrews", + "contact": { + "email": "amanda52@example.net", + "phone": "815-823-1247x52270" + } + } + }, + "projects": [ + { + "projectId": "P5216", + "projectName": "Multi-tiered discrete Internet solution", + "startDate": "2025-05-25", + "tasks": [ + { + "taskId": "T522", + "title": "Evolve Best-Of-Breed E-Tailers", + "status": "In Progress", + "details": { + "hoursSpent": 41, + "technologiesUsed": [ + "Python", + "SQL" + ], + "expectedCompletion": "2025-09-11" + } + }, + { + "taskId": "T405", + "title": "Empower Cross-Media Users", + "status": "In Progress", + "details": { + "hoursSpent": 41, + "technologiesUsed": [ + "PyTorch", + "JavaScript", + "Python", + "Scikit-learn" + ], + "expectedCompletion": "2025-08-17" + } + } + ] + } + ] + } +} \ No newline at end of file diff --git a/example/main.zig b/example/main.zig --- a/example/main.zig +++ b/example/main.zig @@ -9,6 +9,8 @@ .always_show_type_names = true, }; +const employees = @embedFile("employees.json"); + const Hello = enum { world, developer }; const Gender = enum(u8) { male, female, nonbinary, other, _ }; @@ -35,12 +37,59 @@ person: [2]Person, }; +const Employee = struct { + id: []const u8, + name: []const u8, + position: []const u8, + department: Department, + projects: []Project, +}; + +const Department = struct { + id: []const u8, + name: []const u8, + manager: Manager, + + const Manager = struct { + id: []const u8, + name: []const u8, + contact: Contact, + + const Contact = struct { + email: []const u8, + phone: []const u8, + }; + }; +}; + +const Project = struct { + projectId: []const u8, + projectName: []const u8, + startDate: []const u8, + tasks: []Task, + + const Task = struct { + taskId: []const u8, + title: []const u8, + status: []const u8, + details: Details, + + const Details = struct { + hoursSpent: u32, + technologiesUsed: [][]const u8, + expectedCompletion: []const u8, + }; + }; +}; + +const JsonData = union(enum) { + employee: Employee, +}; + const ErrorSet = error{ OutOfMemory, WriteFailed }; const Result = ErrorSet!Hello; pub fn main(init: std.process.Init) !void { - _ = init; - print("Pretty type - {f}\n", .{pretty(Hello)}); print("Pretty null - {f}\n", .{pretty(null)}); @@ -124,6 +173,30 @@ print("\nVectors\n", .{}); print("Pretty vector of u8s - {f}\n", .{pretty(zig_logo_color)}); + const ptr_array = [_]f32{ 0.1, 0.3, 0.9 }; + const persons_allocated = try init.gpa.alloc(Person, 10); + defer init.gpa.free(persons_allocated); + + var str_allocated = try init.gpa.alloc(u8, 5); + defer init.gpa.free(str_allocated); + str_allocated[0] = 'H'; + str_allocated[1] = 'e'; + str_allocated[2] = 'l'; + str_allocated[3] = 'l'; + str_allocated[4] = 'o'; + + var buffer: [100]u8 = [_]u8{1} ** 100; + const buffer_ptr: *[100]u8 = &buffer; + const buffer_many_ptr: [*]u8 = buffer_ptr; + + print("\nPointers\n", .{}); + print("Pretty string - {f}\n", .{pretty("Hello World!")}); + print("Pretty ptr to an array - {f}\n", .{pretty(&ptr_array)}); + print("Pretty ptr to nested struct - {f}\n", .{pretty(&nested)}); + print("Pretty ptr to slice of structs - {f}\n", .{pretty(persons_allocated)}); + print("Pretty ptr to slice of u8 - {f}\n", .{pretty(str_allocated)}); + print("Pretty ptr to buffer many ptr - {f}\n", .{pretty(buffer_many_ptr)}); + const eu_error: Result = error.OutOfMemory; const eu_ok: Result = .world; @@ -135,4 +208,10 @@ print("\nFunctions\n", .{}); print("Pretty function pretty - {f}\n", .{pretty(pretty)}); print("Pretty function main - {f}\n", .{pretty(main)}); + + const parsed: std.json.Parsed(JsonData) = try std.json.parseFromSlice(JsonData, init.gpa, employees, .{}); + defer parsed.deinit(); + + print("\nJson\n", .{}); + print("{}", .{parsed.value}); } -- tangled.sh