Something went wrong. Try again.
A pretty printer for zig
zig
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259const std = @import("std");const print = std.debug.print;
const build_options = @import("build-options");
const pretty_mod = @import("pretty");const pretty = pretty_mod.pretty;const prettyO = pretty_mod.prettyO;
pub const pretty_options = pretty_mod.Options{ .skip_root_type_name = build_options.skip_root_type_name, .theme = .{ .indent_width = build_options.indent_width, },};
const employees = @embedFile("employees.json");
const Hello = enum { world, developer };
const Gender = enum(u8) { male, female, nonbinary, other, _ };
const Person = struct { age: u8, gender: ?Gender,
pub fn pretty(self: *const @This(), comptime ctx: pretty_mod.Context, run: *const pretty_mod.Runtime) !void { run.setColor(ctx, .dim); try run.write("AGE = "); run.resetColor(); run.setColorRaw(.bright_magenta); try run.print("{d}", .{self.age}); run.resetColor(); run.setColor(ctx, .dim); try run.write(", GENDER = "); run.resetColor(); run.setColorRaw(.bright_magenta); try run.print("{?}", .{self.gender}); run.resetColor(); }};
const Nested = struct { a: ChildA, b: ChildB,};
const ChildA = struct { child: ChildC,};
const ChildB = struct { hello: Hello,};
const ChildC = struct { 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;
const HelloResult = union(ResultType) { const ResultType = enum { ok, err };
ok: Hello, err: ErrorSet,};
pub fn main(init: std.process.Init) !void { const start = std.Io.Timestamp.now(init.io, .cpu_thread);
print("Pretty type - {f}\n", .{pretty(Hello)}); print("Pretty null - {f}\n", .{pretty(null)});
print("\nBooleans\n", .{}); print("Pretty bool true - {f}\n", .{pretty(true)}); print("Pretty bool false - {f}\n", .{pretty(false)}); print( "Pretty bool true without type name - {f}\n", .{prettyO(true, .{ .skip_root_type_name = true, .theme = .{ .type_value_sep = ": " }, })}, );
print("\nUnsigned Integers\n", .{}); print("Pretty u8 - {f}\n", .{pretty(@as(u8, 42))}); print("Pretty u16 - {f}\n", .{pretty(@as(u16, 42))}); print("Pretty u32 - {f}\n", .{pretty(@as(u32, 42))}); print("Pretty u64 - {f}\n", .{pretty(@as(u64, 42))}); print("Pretty usize - {f}\n", .{pretty(@as(usize, 42))});
print("\nSigned Integers\n", .{}); print("Pretty comptime_int - {f}\n", .{pretty(42)}); print("Pretty i8 - {f}\n", .{pretty(@as(i8, 42))}); print("Pretty i16 - {f}\n", .{pretty(@as(i16, 42))}); print("Pretty i32 - {f}\n", .{pretty(@as(i32, 42))}); print("Pretty i64 - {f}\n", .{pretty(@as(i64, 42))}); print("Pretty isize - {f}\n", .{pretty(@as(isize, 42))});
print("\nFloats\n", .{}); print("Pretty comptime_float - {f}\n", .{pretty(3.131)}); print("Pretty f16 - {f}\n", .{pretty(@as(f16, 3.141))}); print("Pretty f32 - {f}\n", .{pretty(@as(f32, 3.141))}); print("Pretty f64 - {f}\n", .{pretty(@as(f64, 3.141))});
print("\nEnums\n", .{}); print("Pretty enum - {f}\n", .{pretty(Hello.world)}); print("Pretty enum literal - {f}\n", .{pretty(.hello_world)});
const opt_null: ?Hello = null; const opt_not_null: ?Hello = .developer;
print("\nOptionals\n", .{}); print("Pretty optional = null - {f}\n", .{pretty(opt_null)}); print("Pretty optional = not null - {f}\n", .{pretty(opt_not_null)});
const person = Person{ .age = 13, .gender = null };
const nested = Nested{ .a = .{ .child = .{ .person = .{ person, Person{ .age = 42, .gender = .nonbinary, } } } }, .b = .{ .hello = .world }, };
print("\nStructs\n", .{}); print("Pretty simple struct - {f}\n", .{pretty(person)}); print( "Pretty simple inline struct - {f}\n", .{prettyO(person, .{ .struct_inline = true })}, ); print("Pretty nested struct - {f}\n", .{pretty(nested)});
const simple_union = HelloResult{ .ok = .world };
print("\nUnions\n", .{}); print("Pretty simple union - {f}\n", .{pretty(simple_union)});
print("\nArrays\n", .{}); print("Pretty array of floats - {f}\n", .{pretty([_]f32{ 0.1, 0.2, 0.3 })}); print( "Pretty array of floats inline - {f}\n", .{prettyO([3]f32{ 0.1, 0.2, 0.3 }, .{ .array_inline = true })}, ); print( "Pretty array of floats inline with indices - {f}\n", .{prettyO([3]f32{ 0.1, 0.2, 0.3 }, .{ .array_inline = true, .array_always_show_index = true, })}, );
const zig_logo_color: @Vector(4, u8) = .{ 247, 164, 29, 255 };
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 = @splat(1); 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;
print("\nError types\n", .{}); print("Pretty error set - {f}\n", .{pretty(ErrorSet.OutOfMemory)}); print("Pretty error union error - {f}\n", .{pretty(eu_error)}); print("Pretty error union ok - {f}\n", .{pretty(eu_ok)});
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("Data - {f}\n", pretty(pretty(employees))); print("{f}\n", .{pretty(parsed.value)});
const end = std.Io.Timestamp.now(init.io, .cpu_thread); print( "\nTime - {}ms\n", .{(@as(f64, @floatFromInt(end.toMicroseconds())) - @as(f64, @floatFromInt(start.toMicroseconds()))) / 1000.0}, );}