diff --git a/example/main.zig b/example/main.zig index 8e7be5d..55b132f 100644 --- a/example/main.zig +++ b/example/main.zig @@ -28,11 +28,15 @@ const Person = struct { run.setColor(ctx, .dim); try run.write("AGE = "); run.resetColor(); + run.setColorRaw(.bright_magenta); try run.print("{d}", .{self.age}); + run.resetColor(); run.setColor(ctx, .dim); try run.write(", GENDER = "); run.resetColor(); + run.setColorRaw(.bright_magenta); try run.print("{?}", .{self.gender}); + run.resetColor(); } }; @@ -113,6 +117,8 @@ const HelloResult = union(ResultType) { }; pub fn main(init: std.process.Init) !void { + const start = std.Io.Timestamp.now(init.io, .cpu_thread); + print("Pretty type - {f}\n", .{pretty(Hello)}); print("Pretty null - {f}\n", .{pretty(null)}); @@ -243,4 +249,10 @@ pub fn main(init: std.process.Init) !void { print("\nJson\n", .{}); print("Data - {f}\n", pretty(pretty(employees))); print("{f}\n", .{pretty(parsed.value)}); + + const end = std.Io.Timestamp.now(init.io, .cpu_thread); + print( + "\nTime - {}ms\n", + .{(@as(f64, @floatFromInt(end.toMicroseconds())) - @as(f64, @floatFromInt(start.toMicroseconds()))) / 1000.0}, + ); } diff --git a/pretty.zig b/pretty.zig index c953d10..302d975 100644 --- a/pretty.zig +++ b/pretty.zig @@ -2,12 +2,26 @@ const std = @import("std"); const Io = std.Io; const Type = std.builtin.Type; -pub fn pretty(value: anytype) Pretty(@TypeOf(value)) { - return Pretty(@TypeOf(value)).init(value); +pub fn pretty(value: anytype) Pretty( + @TypeOf(value), + if (isComptimeType(@TypeOf(value))) value else @TypeOf(value), +) { + return Pretty( + @TypeOf(value), + if (isComptimeType(@TypeOf(value))) value else @TypeOf(value), + ).init(value); } -pub fn prettyO(value: anytype, comptime opts: Options) PrettyWithOptions(@TypeOf(value), opts) { - return PrettyWithOptions(@TypeOf(value), opts).init(value); +pub fn prettyO(value: anytype, comptime opts: Options) PrettyWithOptions( + @TypeOf(value), + opts, + if (isComptimeType(@TypeOf(value))) value else @TypeOf(value), +) { + return PrettyWithOptions( + @TypeOf(value), + opts, + if (isComptimeType(@TypeOf(value))) value else @TypeOf(value), + ).init(value); } const root = @import("root"); @@ -97,12 +111,21 @@ pub const Theme = struct { } }; -pub fn Pretty(comptime T: type) type { - return PrettyWithOptions(T, default_options); +pub fn Pretty(comptime T: type, comptime value: anytype) type { + return PrettyWithOptions(T, default_options, value); } -pub fn PrettyWithOptions(comptime T: type, comptime options: Options) type { - if (default_options.disable) return struct { +pub fn PrettyWithOptions(comptime T: type, comptime options: Options, comptime value: anytype) type { + if (default_options.disable) return PrettyDisabled(T); + + return if (isComptimeType(T)) + PrettyComptimeValue(T, options, value) + else + PrettyRuntimeValue(T, options); +} + +pub fn PrettyDisabled(comptime T: type) type { + return struct { value: T, pub fn init(val: T) @This() { @@ -113,6 +136,59 @@ pub fn PrettyWithOptions(comptime T: type, comptime options: Options) type { try w.print("{any}", .{this.value}); } }; +} + +fn isComptimeType(comptime T: type) bool { + switch (@typeInfo(T)) { + .comptime_int, + .comptime_float, + .enum_literal, + .@"fn", + .type, + .null, + => return true, + else => return false, + } +} + +pub fn PrettyComptimeValue(comptime T: type, comptime options: Options, comptime value: T) type { + if (default_options.disable) return PrettyDisabled(T); + + const global = struct { + var tty: ?Io.Terminal = null; + }; + const ctx: Context = Context{ .options = options }; + + return struct { + comptime value: T = value, + + pub fn init(_: T) @This() { + return .{}; + } + + pub fn format(this: *const @This(), w: *std.Io.Writer) error{WriteFailed}!void { + if (global.tty == null) { + var buffer: [1]u8 = undefined; + const stderr = std.debug.lockStderr(&buffer).terminal(); + defer std.debug.unlockStderr(); + + global.tty = stderr; + global.tty.?.writer = w; + } + + var run = Runtime{ + .out = w, + .tty = global.tty.?, + .no_color = options.no_color, + }; + + try run.pretty(ctx, this.value, .{}); + } + }; +} + +pub fn PrettyRuntimeValue(comptime T: type, comptime options: Options) type { + if (default_options.disable) return PrettyDisabled(T); const global = struct { var tty: ?Io.Terminal = null; @@ -142,7 +218,7 @@ pub fn PrettyWithOptions(comptime T: type, comptime options: Options) type { .no_color = options.no_color, }; - return run.pretty(ctx, this.value, .{}); + try run.pretty(ctx, this.value, .{}); } }; }