A pretty printer for zig
zig

improve options

altagos.dev ab1d20dc e9c238f9

verified
+78 -52
+7 -5
example/main.zig
··· 9 9 10 10 pub const pretty_options = pretty_mod.Options{ 11 11 .skip_root_type_name = build_options.skip_root_type_name, 12 - .indent_width = build_options.indent_width, 12 + .theme = .{ 13 + .indent_width = build_options.indent_width, 14 + }, 13 15 }; 14 16 15 17 const employees = @embedFile("employees.json"); ··· 160 162 print("Pretty simple struct - {f}\n", .{pretty(person)}); 161 163 print( 162 164 "Pretty simple inline struct - {f}\n", 163 - .{prettyO(person, .{ .inline_structs = true })}, 165 + .{prettyO(person, .{ .struct_inline = true })}, 164 166 ); 165 167 print("Pretty nested struct - {f}\n", .{pretty(nested)}); 166 168 ··· 173 175 print("Pretty array of floats - {f}\n", .{pretty([_]f32{ 0.1, 0.2, 0.3 })}); 174 176 print( 175 177 "Pretty array of floats inline - {f}\n", 176 - .{prettyO([3]f32{ 0.1, 0.2, 0.3 }, .{ .inline_arrays = true })}, 178 + .{prettyO([3]f32{ 0.1, 0.2, 0.3 }, .{ .array_inline = true })}, 177 179 ); 178 180 print( 179 181 "Pretty array of floats inline with indices - {f}\n", 180 182 .{prettyO([3]f32{ 0.1, 0.2, 0.3 }, .{ 181 - .inline_arrays = true, 182 - .always_show_index = true, 183 + .array_inline = true, 184 + .array_always_show_index = true, 183 185 })}, 184 186 ); 185 187
+71 -47
pretty.zig
··· 17 17 Options{}; 18 18 19 19 pub const Options = struct { 20 - no_color: bool = false, 20 + /// Disable pretty printing. 21 + /// 22 + /// If enabled will format values using `{any}` 21 23 disable: bool = false, 22 24 23 - indent_width: comptime_int = 2, 25 + /// Show type names 26 + show_type_names: bool = true, 27 + /// Don't show type names for the root value 28 + skip_root_type_name: bool = false, 24 29 25 - inline_arrays: bool = false, 26 - always_show_index: bool = false, 30 + /// Disable color output 31 + no_color: bool = false, 32 + /// Customize the theme, change colors or separators 33 + theme: Theme = .{}, 27 34 28 - treat_u8_ptr_as_string: bool = true, 29 - treat_u8_ptr_slice_as_string: bool = true, 30 - inline_slice: bool = false, 35 + /// Inline print arrays 36 + array_inline: bool = false, 37 + /// Always show array indices, even in inline mode 38 + array_always_show_index: bool = false, 31 39 32 - inline_structs: bool = false, 33 - highlight_union_tag: bool = true, 34 - 35 - show_type_names: bool = true, 36 - skip_root_type_name: bool = false, 40 + /// Treat pointers to `[]u8` / `[]const u8` as strings 41 + ptr_array_u8_as_string: bool = true, 42 + /// Treat pointers to a u8 slice as strings 43 + ptr_slice_u8_as_string: bool = true, 44 + /// Inline print slices 45 + ptr_slice_inline: bool = false, 46 + /// Always show slice indices, even in inline mode 47 + ptr_slice_always_show_index: bool = false, 37 48 38 - theme: Theme = .{}, 49 + /// Inline print structs 50 + struct_inline: bool = false, 51 + /// Highlight tag type of unions 52 + union_highlight_tag: bool = true, 39 53 }; 40 54 41 55 pub const Theme = struct { ··· 52 66 false, 53 67 }; 54 68 69 + indent_width: comptime_int = 2, 70 + 55 71 field_name_type_sep: []const u8 = ": ", 56 72 type_value_sep: []const u8 = " = ", 57 73 index_value_sep: []const u8 = " = ", ··· 59 75 index_open: []const u8 = "[", 60 76 index_close: []const u8 = "]", 61 77 78 + array_open: []const u8 = "{", 79 + array_close: []const u8 = "}", 80 + 81 + vec_open: []const u8 = "(", 82 + vec_close: []const u8 = ")", 83 + 62 84 color_dim: Io.Terminal.Color = .dim, 63 85 color_field: Io.Terminal.Color = .green, 64 86 color_value: Io.Terminal.Color = .blue, ··· 149 171 comptime color: Theme.Color, 150 172 ) void { 151 173 if (this.no_color or default_options.no_color) return; 152 - this.tty.setColor(ctx.options.theme.getColor(color)) catch {}; 174 + this.tty.setColor(comptime ctx.options.theme.getColor(color)) catch {}; 153 175 } 154 176 155 177 pub inline fn setColorRaw(this: *const Runtime, color: Io.Terminal.Color) void { ··· 257 279 try run.write(@typeName(T)); 258 280 259 281 if (active_type == .@"union") { 260 - if (ctx.options.highlight_union_tag) { 282 + if (ctx.options.union_highlight_tag) { 261 283 run.resetColor(); 262 284 run.setColor(ctx, .field); 263 285 } 264 286 265 287 try run.print("{}", .{std.meta.activeTag(value)}); 266 288 267 - if (ctx.options.highlight_union_tag) { 289 + if (ctx.options.union_highlight_tag) { 268 290 run.resetColor(); 269 291 run.setColor(ctx, .dim); 270 292 } ··· 342 364 .options = ctx.options, 343 365 }; 344 366 345 - if (ctx.options.inline_structs) { 367 + if (ctx.options.struct_inline) { 346 368 run.setColor(ctx, .dim); 347 369 try run.write(".{ "); 348 370 run.resetColor(); ··· 350 372 351 373 comptime var index = 0; 352 374 inline for (st.fields) |field| { 353 - indent(next_ctx, next_ctx.options.inline_structs, run); 354 - if (index != 0 and ctx.options.inline_structs) { 375 + indent(next_ctx, next_ctx.options.struct_inline, run); 376 + if (index != 0 and ctx.options.struct_inline) { 355 377 run.setColor(ctx, .dim); 356 378 try run.write(", "); 357 379 run.resetColor(); ··· 367 389 index += 1; 368 390 } 369 391 370 - if (ctx.options.inline_structs) { 392 + if (ctx.options.struct_inline) { 371 393 run.setColor(ctx, .dim); 372 394 try run.write(" }"); 373 395 run.resetColor(); ··· 397 419 .options = ctx.options, 398 420 }; 399 421 400 - if (ctx.options.inline_arrays) { 401 - run.setColor(ctx, .dim); 402 - try run.write("{ "); 403 - run.resetColor(); 404 - } 422 + if (ctx.options.array_inline) try arrayOpen(ctx, run); 405 423 406 424 comptime var index = 0; 407 425 inline for (value) |val| { 408 426 indent( 409 427 next_ctx, 410 - next_ctx.options.inline_arrays, 428 + next_ctx.options.array_inline, 411 429 run, 412 430 ); 413 431 414 432 run.setColor(ctx, .dim); 415 - if (index != 0 and ctx.options.inline_arrays) 433 + if (index != 0 and ctx.options.array_inline) 416 434 try run.write(", "); 417 435 418 - if (!ctx.options.inline_arrays or ctx.options.always_show_index) { 436 + if (!ctx.options.array_inline or ctx.options.array_always_show_index) { 419 437 try run.write(ctx.options.theme.index_open); 420 438 try run.print("{}", .{index}); 421 439 try run.write(ctx.options.theme.index_close ++ ··· 429 447 index += 1; 430 448 } 431 449 432 - if (ctx.options.inline_arrays) { 433 - run.setColor(ctx, .dim); 434 - try run.write(" }"); 435 - run.resetColor(); 436 - } 450 + if (ctx.options.array_inline) try arrayClose(ctx, run); 437 451 } 438 452 439 453 inline fn formatVector( ··· 443 457 value: @Vector(vec.len, vec.child), 444 458 ) !void { 445 459 run.setColor(ctx, .dim); 446 - try run.write("( "); 460 + try run.write(ctx.options.theme.vec_open ++ " "); 447 461 run.resetColor(); 448 462 449 463 inline for (0..vec.len) |idx| { ··· 457 471 } 458 472 459 473 run.setColor(ctx, .dim); 460 - try run.write(" )"); 474 + try run.write(" " ++ ctx.options.theme.vec_close); 461 475 run.resetColor(); 462 476 } 463 477 ··· 468 482 value: anytype, 469 483 ) !void { 470 484 const is_string = switch (@typeInfo(ptr.child)) { 471 - .array => |arr| arr.child == u8 and ctx.options.treat_u8_ptr_as_string, 485 + .array => |arr| arr.child == u8 and ctx.options.ptr_array_u8_as_string, 472 486 else => false, 473 487 }; 474 488 if (is_string) return formatString(ctx, run, value); ··· 482 496 run: *Runtime, 483 497 value: anytype, 484 498 ) !void { 485 - if (ptr.child == u8 and ctx.options.treat_u8_ptr_slice_as_string) 499 + if (ptr.child == u8 and ctx.options.ptr_slice_u8_as_string) 486 500 return formatString(ctx, run, value); 487 501 488 502 const next_ctx = Context{ 489 - .depth = ctx.depth + 1, 503 + .depth = if (!ctx.options.ptr_slice_inline) ctx.depth + 1 else ctx.depth, 490 504 .exited_comptime = ctx.exited_comptime, 491 505 .options = ctx.options, 492 506 }; 493 507 494 - if (ctx.options.inline_slice) { 495 - run.setColor(ctx, .dim); 496 - try run.write("{ "); 497 - run.resetColor(); 498 - } 508 + if (ctx.options.ptr_slice_inline) try arrayOpen(ctx, run); 499 509 500 510 for (value, 0..) |val, idx| { 501 511 indent( 502 512 next_ctx, 503 - next_ctx.options.inline_slice, 513 + next_ctx.options.ptr_slice_inline, 504 514 run, 505 515 ); 506 516 507 517 run.setColor(ctx, .dim); 508 - if (idx != 0 and ctx.options.inline_slice) 518 + if (idx != 0 and ctx.options.ptr_slice_inline) 509 519 try run.write(", "); 510 520 511 - if (!ctx.options.inline_slice or ctx.options.always_show_index) { 521 + if (!ctx.options.ptr_slice_inline or ctx.options.ptr_slice_always_show_index) { 512 522 try run.write(ctx.options.theme.index_open); 513 523 try run.print("{}", .{idx}); 514 524 try run.write(ctx.options.theme.index_close ++ ··· 519 529 520 530 try innerFmt(ptr.child, next_ctx, run, val, .{ .skip_type_name = true }); 521 531 } 532 + 533 + if (ctx.options.ptr_slice_inline) try arrayClose(ctx, run); 522 534 } 523 535 524 536 inline fn formatPtrMany( ··· 593 605 ) void { 594 606 if (inline_option) return; 595 607 596 - const text: [ctx.depth * ctx.options.indent_width]u8 = @splat(' '); 608 + const text: [ctx.depth * ctx.options.theme.indent_width]u8 = @splat(' '); 597 609 run.write("\n" ++ text) catch {}; 598 610 } 611 + 612 + fn arrayOpen(comptime ctx: Context, run: *const Runtime) !void { 613 + run.setColor(ctx, .dim); 614 + try run.write(ctx.options.theme.array_open ++ " "); 615 + run.resetColor(); 616 + } 617 + 618 + fn arrayClose(comptime ctx: Context, run: *const Runtime) !void { 619 + run.setColor(ctx, .dim); 620 + try run.write(" " ++ ctx.options.theme.array_close); 621 + run.resetColor(); 622 + }