Skip to content

Instantly share code, notes, and snippets.

@ivanstepanovftw
Created June 27, 2026 10:03
Show Gist options
  • Select an option

  • Save ivanstepanovftw/1772b25c49e0f12fd899536bf80567cb to your computer and use it in GitHub Desktop.

Select an option

Save ivanstepanovftw/1772b25c49e0f12fd899536bf80567cb to your computer and use it in GitHub Desktop.
const std = @import("std");
fn Wrapper(comptime format_string: []const u8, comptime T: type) type {
return struct {
pub const fmt = format_string;
value: T,
};
}
/// Placeholder
pub fn p(comptime format_string: []const u8, value: anytype) Wrapper("{" ++ format_string ++ "}", @TypeOf(value)) {
return .{ .value = value };
}
// std.Io.Writer.print
pub fn format(io_w: *std.Io.Writer, args: anytype) std.Io.Writer.Error!void {
inline for (args) |arg| {
const T = @TypeOf(arg);
const ti = @typeInfo(T);
if (ti == .@"struct" and @hasDecl(T, "fmt")) {
try io_w.print(T.fmt, .{arg.value});
} else if (ti == .pointer and ti.pointer.is_const and ti.pointer.size == .one and @typeInfo(ti.pointer.child) == .array and @typeInfo(ti.pointer.child).array.child == u8) {
try io_w.writeAll(arg);
} else {
try io_w.print("{any}", .{arg});
}
}
}
// std.debug.print
pub fn print(args: anytype) void {
var buffer: [64]u8 = undefined;
const bw = std.debug.lockStderrWriter(&buffer);
defer std.debug.unlockStderrWriter();
nosuspend format(bw, args) catch return;
}
pub fn main() !void {
var float: f64 = 10.15;
_ = &float;
const unsigned: u32 = 1288;
const string: []const u8 = "rs!";
const cstring = "Comptime String!";
const my_struct = .{
.a = 1,
.b = false,
};
print(.{ "Float: ", float, ", Unsigned: ", unsigned, ", String: ", string, ", CString: ", cstring, ", Struct: ", my_struct, "\n" });
print(.{
"Formatted float: ", p("d:>7.1", float),
", Formatted unsigned: ", p("d:0>5", unsigned),
", Hex unsigned: ", p("x", unsigned),
"\n",
});
print(.{"Values are "});
print(.{ p("s:^20", string), " and " });
print(.{p("X", unsigned)});
print(.{"!\n"});
print(.{p("f", std.zig.fmtId("example"))});
}
@ivanstepanovftw

ivanstepanovftw commented Jun 27, 2026

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment