A pretty printer for zig
zig

optimize print statements in printType

altagos.dev 85e3cdf9 94f031c1

verified
+18 -18
+18 -18
pretty.zig
··· 59 59 60 60 indent_level: usize = 0, 61 61 62 - pub fn print(this: RuntimeContext, comptime fmt: []const u8, args: anytype) error{WriteFailed}!void { 62 + pub inline fn print(this: RuntimeContext, comptime fmt: []const u8, args: anytype) error{WriteFailed}!void { 63 63 return this.out.print(fmt, args); 64 64 } 65 65 66 - pub fn setColor(this: RuntimeContext, color: Io.Terminal.Color) void { 66 + pub inline fn write(this: RuntimeContext, text: []const u8) error{WriteFailed}!void { 67 + return this.out.writeAll(text); 68 + } 69 + 70 + pub inline fn setColor(this: RuntimeContext, color: Io.Terminal.Color) void { 67 71 this.tty.setColor(color) catch {}; 68 72 } 69 73 70 - pub fn resetColor(this: RuntimeContext) void { 74 + pub inline fn resetColor(this: RuntimeContext) void { 71 75 this.tty.setColor(.reset) catch {}; 72 76 } 73 77 }; ··· 125 129 126 130 if (cctx.options.show_type_names) { 127 131 switch (info) { 128 - .bool => try rctx.print("bool{s}", .{cctx.options.type_value_seperator}), 129 - .comptime_int => try rctx.print("comptime_int{s}", .{cctx.options.type_value_seperator}), 130 - .comptime_float => try rctx.print("comptime_float{s}", .{cctx.options.type_value_seperator}), 132 + .bool => try rctx.write("bool"), 133 + .comptime_int => try rctx.write("comptime_int"), 134 + .comptime_float => try rctx.write("comptime_float"), 131 135 .int => |int| try rctx.print( 132 - "{s}{}{s}", 133 - .{ 134 - if (int.signedness == .signed) "i" else "u", 135 - int.bits, 136 - cctx.options.type_value_seperator, 137 - }, 136 + "{s}{}", 137 + .{ if (int.signedness == .signed) "i" else "u", int.bits }, 138 138 ), 139 - .float => |float| try rctx.print("f{}{s}", .{ float.bits, cctx.options.type_value_seperator }), 140 - .enum_literal => try rctx.print("enum literal{s}", .{cctx.options.type_value_seperator}), 141 - .@"enum" => try rctx.print("{s}{s}", .{ @typeName(T), cctx.options.type_value_seperator }), 142 - else => try rctx.print("missing {}{s}", .{ info, cctx.options.type_value_seperator }), 139 + .float => |float| try rctx.print("f{}", .{float.bits}), 140 + .enum_literal => try rctx.write("enum literal"), 141 + .@"enum" => try rctx.write(@typeName(T)), 142 + else => try rctx.print("missing type {}", .{info}), 143 143 } 144 - } else { 145 - try rctx.print("{s}", .{cctx.options.type_value_seperator}); 146 144 } 145 + 146 + try rctx.write(cctx.options.type_value_seperator); 147 147 148 148 rctx.resetColor(); 149 149 }