A pretty printer for zig
zig
0
fork

Configure Feed

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

clean up

+21 -28
+21 -28
pretty.zig
··· 151 151 } 152 152 } 153 153 154 + const global = struct { 155 + var tty: ?Io.Terminal = null; 156 + 157 + pub inline fn setup(w: *std.Io.Writer) void { 158 + if (tty == null) { 159 + var buffer: [1]u8 = undefined; 160 + const stderr = std.debug.lockStderr(&buffer).terminal(); 161 + defer std.debug.unlockStderr(); 162 + 163 + tty = stderr; 164 + tty.?.writer = w; 165 + } 166 + } 167 + }; 168 + 154 169 pub fn PrettyComptimeValue(comptime T: type, comptime options: Options, comptime value: T) type { 155 170 if (default_options.disable) return PrettyDisabled(T); 156 - 157 - const global = struct { 158 - var tty: ?Io.Terminal = null; 159 - }; 160 171 const ctx: Context = Context{ .options = options }; 161 172 162 173 return struct { 163 - comptime value: T = value, 164 - 165 174 pub fn init(_: T) @This() { 166 175 return .{}; 167 176 } 168 177 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 + pub fn format(_: *const @This(), w: *std.Io.Writer) error{WriteFailed}!void { 179 + global.setup(w); 178 180 179 181 var run = Runtime{ 180 182 .out = w, ··· 182 184 .no_color = options.no_color, 183 185 }; 184 186 185 - try run.pretty(ctx, this.value, .{}); 187 + try run.pretty(ctx, value, .{}); 188 + try w.flush(); 186 189 } 187 190 }; 188 191 } 189 192 190 193 pub fn PrettyRuntimeValue(comptime T: type, comptime options: Options) type { 191 194 if (default_options.disable) return PrettyDisabled(T); 192 - 193 - const global = struct { 194 - var tty: ?Io.Terminal = null; 195 - }; 196 195 const ctx: Context = Context{ .options = options }; 197 196 198 197 return struct { ··· 203 202 } 204 203 205 204 pub fn format(this: *const @This(), w: *std.Io.Writer) error{WriteFailed}!void { 206 - if (global.tty == null) { 207 - var buffer: [1]u8 = undefined; 208 - const stderr = std.debug.lockStderr(&buffer).terminal(); 209 - defer std.debug.unlockStderr(); 210 - 211 - global.tty = stderr; 212 - global.tty.?.writer = w; 213 - } 205 + global.setup(w); 214 206 215 207 var run = Runtime{ 216 208 .out = w, ··· 219 211 }; 220 212 221 213 try run.pretty(ctx, this.value, .{}); 214 + try w.flush(); 222 215 } 223 216 }; 224 217 }