diff --git a/pretty.zig b/pretty.zig index f8e8009..c68db64 100644 --- a/pretty.zig +++ b/pretty.zig @@ -15,7 +15,7 @@ pub const Options = struct { }; pub const Theme = struct { - pub const Colors = enum { + pub const Color = enum { dim, field, value, @@ -38,7 +38,7 @@ pub const Theme = struct { color_true: Io.Terminal.Color = .bright_green, color_false: Io.Terminal.Color = .bright_red, - pub inline fn getColor(comptime this: Theme, comptime color: Colors) Io.Terminal.Color { + pub inline fn getColor(comptime this: Theme, comptime color: Color) Io.Terminal.Color { return switch (color) { inline else => |col| @field(this, "color_" ++ @tagName(col)), }; @@ -59,7 +59,7 @@ pub fn PrettyWithOptions(comptime T: type, comptime options: Options) type { const global = struct { var tty: ?Io.Terminal = null; }; - const cctx: ComptimeContext = ComptimeContext{ .options = options }; + const ctx: Context = Context{ .options = options }; return struct { value: T, @@ -78,17 +78,17 @@ pub fn PrettyWithOptions(comptime T: type, comptime options: Options) type { global.tty.?.writer = w; } - var rctx = RuntimeContext{ + var run = Runtime{ .out = w, .tty = global.tty.?, }; - return innerFmt(T, cctx, &rctx, this.value, .{}); + return innerFmt(T, ctx, &run, this.value, .{}); } }; } -const RuntimeContext = struct { +const Runtime = struct { out: *Io.Writer, tty: Io.Terminal, @@ -97,35 +97,35 @@ const RuntimeContext = struct { indent_level: usize = 0, pub inline fn print( - this: *const RuntimeContext, + this: *const Runtime, comptime fmt: []const u8, args: anytype, ) error{WriteFailed}!void { return this.out.print(fmt, args); } - pub inline fn write(this: *const RuntimeContext, text: []const u8) error{WriteFailed}!void { + pub inline fn write(this: *const Runtime, text: []const u8) error{WriteFailed}!void { return this.out.writeAll(text); } pub inline fn setColor( - this: *const RuntimeContext, - comptime ctx: ComptimeContext, - comptime color: Theme.Colors, + this: *const Runtime, + comptime ctx: Context, + comptime color: Theme.Color, ) void { this.tty.setColor(ctx.options.theme.getColor(color)) catch {}; } - pub inline fn setColorRaw(this: *const RuntimeContext, color: Io.Terminal.Color) void { + pub inline fn setColorRaw(this: *const Runtime, color: Io.Terminal.Color) void { this.tty.setColor(color) catch {}; } - pub inline fn resetColor(this: *const RuntimeContext) void { + pub inline fn resetColor(this: *const Runtime) void { this.tty.setColor(.reset) catch {}; } }; -const ComptimeContext = struct { +const Context = struct { depth: comptime_int = 0, indent_level: comptime_int = 0, @@ -133,7 +133,7 @@ const ComptimeContext = struct { options: Options, exited_comptime: bool = false, - pub fn inComptime(comptime this: ComptimeContext) bool { + pub fn inComptime(comptime this: Context) bool { if (!this.exited_comptime) return false; return @inComptime(); } @@ -145,19 +145,19 @@ const InnerFmtOptions = struct { fn innerFmt( comptime T: type, - comptime cctx: ComptimeContext, - rctx: *RuntimeContext, + comptime ctx: Context, + run: *Runtime, value: T, opts: InnerFmtOptions, ) error{WriteFailed}!void { const info = @typeInfo(T); - if (opts.dont_skip_type_name) try printType(T, cctx, rctx); + if (opts.dont_skip_type_name) try printType(T, ctx, run); return switch (info) { - .bool => formatBool(cctx, rctx, value), - .null => formatNull(cctx, rctx), - .type => formatType(cctx, rctx, value), + .bool => formatBool(ctx, run, value), + .null => formatNull(ctx, run), + .type => formatType(ctx, run, value), // comptime types .comptime_int, @@ -168,23 +168,23 @@ fn innerFmt( // enum types .@"enum", .enum_literal, - => formatValue(cctx, rctx, value), + => formatValue(ctx, run, value), - .optional => |opt| formatOptional(opt.child, cctx, rctx, value), - .@"struct" => |st| formatStruct(T, st, cctx, rctx, value), + .optional => |opt| formatOptional(opt.child, ctx, run, value), + .@"struct" => |st| formatStruct(T, st, ctx, run, value), else => { - rctx.setColor(cctx, .@"error"); - try rctx.print("Unimplemented! ({} = {any})", .{ info, value }); - rctx.resetColor(); + run.setColor(ctx, .@"error"); + try run.print("Unimplemented! ({} = {any})", .{ info, value }); + run.resetColor(); }, }; } inline fn printType( comptime T: type, - comptime cctx: ComptimeContext, - rctx: *RuntimeContext, + comptime ctx: Context, + run: *Runtime, ) error{WriteFailed}!void { const active_type = comptime std.meta.activeTag(@typeInfo(T)); const excluded = comptime switch (active_type) { @@ -192,115 +192,115 @@ inline fn printType( else => false, }; - if ((cctx.depth != 0 or cctx.options.always_show_type_names) and !excluded) { - rctx.setColor(cctx, .dim); + if ((ctx.depth != 0 or ctx.options.always_show_type_names) and !excluded) { + run.setColor(ctx, .dim); - if (cctx.options.show_type_names) { - try rctx.write(@typeName(T)); + if (ctx.options.show_type_names) { + try run.write(@typeName(T)); } - try rctx.write(cctx.options.theme.type_value_sep); + try run.write(ctx.options.theme.type_value_sep); - rctx.resetColor(); + run.resetColor(); } } inline fn formatBool( - comptime cctx: ComptimeContext, - rctx: *const RuntimeContext, + comptime ctx: Context, + run: *const Runtime, value: bool, ) !void { if (value) - rctx.setColor(cctx, .true) + run.setColor(ctx, .true) else - rctx.setColor(cctx, .false); + run.setColor(ctx, .false); - try rctx.print("{}", .{value}); - rctx.resetColor(); + try run.print("{}", .{value}); + run.resetColor(); } -inline fn formatNull(comptime cctx: ComptimeContext, rctx: *const RuntimeContext) !void { - rctx.setColor(cctx, .null); - try rctx.write("null"); - rctx.resetColor(); +inline fn formatNull(comptime ctx: Context, run: *const Runtime) !void { + run.setColor(ctx, .null); + try run.write("null"); + run.resetColor(); } inline fn formatOptional( comptime T: type, - comptime cctx: ComptimeContext, - rctx: *RuntimeContext, + comptime ctx: Context, + run: *Runtime, value: ?T, ) !void { return if (value) |val| - innerFmt(T, cctx, rctx, val, .{ .dont_skip_type_name = false }) + innerFmt(T, ctx, run, val, .{ .dont_skip_type_name = false }) else - formatNull(cctx, rctx); + formatNull(ctx, run); } inline fn formatStruct( comptime T: type, comptime st: Type.Struct, - comptime cctx: ComptimeContext, - rctx: *RuntimeContext, + comptime ctx: Context, + run: *Runtime, value: T, ) !void { - const next_cctx = ComptimeContext{ - .depth = cctx.depth + 1, - .indent_level = cctx.indent_level + 1, - .exited_comptime = cctx.exited_comptime, - .options = cctx.options, + const next_ctx = Context{ + .depth = ctx.depth + 1, + .indent_level = ctx.indent_level + 1, + .exited_comptime = ctx.exited_comptime, + .options = ctx.options, }; - if (cctx.options.inline_structs) { - rctx.setColor(cctx, .dim); - try rctx.write(".{ "); - rctx.resetColor(); + if (ctx.options.inline_structs) { + run.setColor(ctx, .dim); + try run.write(".{ "); + run.resetColor(); } comptime var index: comptime_int = 0; inline for (st.fields) |field| { - indent(next_cctx, rctx); - if (index != 0 and cctx.options.inline_structs) try rctx.write(", "); + indent(next_ctx, run); + if (index != 0 and ctx.options.inline_structs) try run.write(", "); - rctx.setColor(cctx, .field); - try rctx.write("." ++ field.name ++ cctx.options.theme.field_name_type_sep); - rctx.resetColor(); + run.setColor(ctx, .field); + try run.write("." ++ field.name ++ ctx.options.theme.field_name_type_sep); + run.resetColor(); - try innerFmt(field.type, next_cctx, rctx, @field(value, field.name), .{}); + try innerFmt(field.type, next_ctx, run, @field(value, field.name), .{}); index += 1; } - if (cctx.options.inline_structs) { - rctx.setColor(cctx, .dim); - try rctx.write(" }"); - rctx.resetColor(); + if (ctx.options.inline_structs) { + run.setColor(ctx, .dim); + try run.write(" }"); + run.resetColor(); } } inline fn formatType( - comptime cctx: ComptimeContext, - rctx: *const RuntimeContext, + comptime ctx: Context, + run: *const Runtime, value: type, ) !void { - rctx.setColor(cctx, .type); - rctx.setColorRaw(.bold); - try rctx.write(@typeName(value)); - rctx.resetColor(); + run.setColor(ctx, .type); + run.setColorRaw(.bold); + try run.write(@typeName(value)); + run.resetColor(); } inline fn formatValue( - comptime cctx: ComptimeContext, - rctx: *const RuntimeContext, + comptime ctx: Context, + run: *const Runtime, value: anytype, ) !void { - rctx.setColor(cctx, .value); - try rctx.print("{}", .{value}); - rctx.resetColor(); + run.setColor(ctx, .value); + try run.print("{}", .{value}); + run.resetColor(); } -inline fn indent(comptime cctx: ComptimeContext, rctx: *const RuntimeContext) void { - if (cctx.options.inline_structs) return; +inline fn indent(comptime ctx: Context, run: *const Runtime) void { + if (ctx.options.inline_structs) return; - const text: [cctx.depth * cctx.options.indent_width]u8 = @splat(' '); - rctx.write("\n" ++ text) catch {}; + const text: [ctx.depth * ctx.options.indent_width]u8 = @splat(' '); + run.write("\n" ++ text) catch {}; }