A pretty printer for zig
zig
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

update to latest zig master

Altagos eabcb982 0c6719d7

+97 -9
+12
example/main.zig
··· 28 28 run.setColor(ctx, .dim); 29 29 try run.write("AGE = "); 30 30 run.resetColor(); 31 + run.setColorRaw(.bright_magenta); 31 32 try run.print("{d}", .{self.age}); 33 + run.resetColor(); 32 34 run.setColor(ctx, .dim); 33 35 try run.write(", GENDER = "); 34 36 run.resetColor(); 37 + run.setColorRaw(.bright_magenta); 35 38 try run.print("{?}", .{self.gender}); 39 + run.resetColor(); 36 40 } 37 41 }; 38 42 ··· 113 117 }; 114 118 115 119 pub fn main(init: std.process.Init) !void { 120 + const start = std.Io.Timestamp.now(init.io, .cpu_thread); 121 + 116 122 print("Pretty type - {f}\n", .{pretty(Hello)}); 117 123 print("Pretty null - {f}\n", .{pretty(null)}); 118 124 ··· 243 249 print("\nJson\n", .{}); 244 250 print("Data - {f}\n", pretty(pretty(employees))); 245 251 print("{f}\n", .{pretty(parsed.value)}); 252 + 253 + const end = std.Io.Timestamp.now(init.io, .cpu_thread); 254 + print( 255 + "\nTime - {}ms\n", 256 + .{(@as(f64, @floatFromInt(end.toMicroseconds())) - @as(f64, @floatFromInt(start.toMicroseconds()))) / 1000.0}, 257 + ); 246 258 }
+85 -9
pretty.zig
··· 2 2 const Io = std.Io; 3 3 const Type = std.builtin.Type; 4 4 5 - pub fn pretty(value: anytype) Pretty(@TypeOf(value)) { 6 - return Pretty(@TypeOf(value)).init(value); 5 + pub fn pretty(value: anytype) Pretty( 6 + @TypeOf(value), 7 + if (isComptimeType(@TypeOf(value))) value else @TypeOf(value), 8 + ) { 9 + return Pretty( 10 + @TypeOf(value), 11 + if (isComptimeType(@TypeOf(value))) value else @TypeOf(value), 12 + ).init(value); 7 13 } 8 14 9 - pub fn prettyO(value: anytype, comptime opts: Options) PrettyWithOptions(@TypeOf(value), opts) { 10 - return PrettyWithOptions(@TypeOf(value), opts).init(value); 15 + pub fn prettyO(value: anytype, comptime opts: Options) PrettyWithOptions( 16 + @TypeOf(value), 17 + opts, 18 + if (isComptimeType(@TypeOf(value))) value else @TypeOf(value), 19 + ) { 20 + return PrettyWithOptions( 21 + @TypeOf(value), 22 + opts, 23 + if (isComptimeType(@TypeOf(value))) value else @TypeOf(value), 24 + ).init(value); 11 25 } 12 26 13 27 const root = @import("root"); ··· 97 111 } 98 112 }; 99 113 100 - pub fn Pretty(comptime T: type) type { 101 - return PrettyWithOptions(T, default_options); 114 + pub fn Pretty(comptime T: type, comptime value: anytype) type { 115 + return PrettyWithOptions(T, default_options, value); 116 + } 117 + 118 + pub fn PrettyWithOptions(comptime T: type, comptime options: Options, comptime value: anytype) type { 119 + if (default_options.disable) return PrettyDisabled(T); 120 + 121 + return if (isComptimeType(T)) 122 + PrettyComptimeValue(T, options, value) 123 + else 124 + PrettyRuntimeValue(T, options); 102 125 } 103 126 104 - pub fn PrettyWithOptions(comptime T: type, comptime options: Options) type { 105 - if (default_options.disable) return struct { 127 + pub fn PrettyDisabled(comptime T: type) type { 128 + return struct { 106 129 value: T, 107 130 108 131 pub fn init(val: T) @This() { ··· 113 136 try w.print("{any}", .{this.value}); 114 137 } 115 138 }; 139 + } 140 + 141 + fn isComptimeType(comptime T: type) bool { 142 + switch (@typeInfo(T)) { 143 + .comptime_int, 144 + .comptime_float, 145 + .enum_literal, 146 + .@"fn", 147 + .type, 148 + .null, 149 + => return true, 150 + else => return false, 151 + } 152 + } 153 + 154 + pub fn PrettyComptimeValue(comptime T: type, comptime options: Options, comptime value: T) type { 155 + if (default_options.disable) return PrettyDisabled(T); 156 + 157 + const global = struct { 158 + var tty: ?Io.Terminal = null; 159 + }; 160 + const ctx: Context = Context{ .options = options }; 161 + 162 + return struct { 163 + comptime value: T = value, 164 + 165 + pub fn init(_: T) @This() { 166 + return .{}; 167 + } 168 + 169 + pub fn format(this: *const @This(), w: *std.Io.Writer) error{WriteFailed}!void { 170 + if (global.tty == null) { 171 + var buffer: [1]u8 = undefined; 172 + const stderr = std.debug.lockStderr(&buffer).terminal(); 173 + defer std.debug.unlockStderr(); 174 + 175 + global.tty = stderr; 176 + global.tty.?.writer = w; 177 + } 178 + 179 + var run = Runtime{ 180 + .out = w, 181 + .tty = global.tty.?, 182 + .no_color = options.no_color, 183 + }; 184 + 185 + try run.pretty(ctx, this.value, .{}); 186 + } 187 + }; 188 + } 189 + 190 + pub fn PrettyRuntimeValue(comptime T: type, comptime options: Options) type { 191 + if (default_options.disable) return PrettyDisabled(T); 116 192 117 193 const global = struct { 118 194 var tty: ?Io.Terminal = null; ··· 142 218 .no_color = options.no_color, 143 219 }; 144 220 145 - return run.pretty(ctx, this.value, .{}); 221 + try run.pretty(ctx, this.value, .{}); 146 222 } 147 223 }; 148 224 }