wasm4.zig: print wrappers with comptime formatting and type safety#673
wasm4.zig: print wrappers with comptime formatting and type safety#673Ruulul wants to merge 1 commit intoaduros:mainfrom
Conversation
add functions `print`, that prints on console, `printText`, that wraps the `text` function, and `printBuffer`, that prints on an arbitrary buffer. allows leveraging custom formatters on custom types, type safety, and use the error handling for ergonomics.
|
the stdlib already has functions to print to a buffer and with an arguably better API. im also a little confused about the other functions youve added, if i know the |
|
again didnt know about this one handy function about the why... I just realized it was being too common to write out a block, create a scoped buffer, print to that buffer, then send the buffer to the proper function ( and on a second note, I dont like to need to manually count characters to know the needed buffer size, and it is not viable to do the std.debug trick and make a mostly I wanted a way to be able to do this kind of pattern in an ergonomic way: w4.textPrint(1, "Gen:\n{}", 135, 10, .{generation}) catch {
w4.text("Gen:\nXe?", 135, 20);
};I am curious tho of what is the expected way to handle printing in general... without a wrapper I would need to do something like... {
const fmt = "Gen:\n{}";
var buffer: [@as(comptime_int, fmt.len) + 1]u8 = undefined;
w4.text(std.fmt.bufPrint(&buffer, fmt, .{generation}) catch "Gen:\nXe?", 135, 20);
}I understand tho if you see this as not general enough for the wasm4.zig file. I just thought it is a pretty common need to want to use a formatter, and it is unpleasant to need to manually count characters to know the proper offsets |
|
I just realized I can write a simple comptime logic to make all calls use the same buffer, but I am not sure if this is worthywhile |
|
I think this is better handled by users since you both need to specify how big you want the buffer to be and it adds a dependency on |
add functions
print, that prints on console,printText, that wraps thetextfunction, andprintBuffer, that prints on an arbitrary buffer. allows leveraging custom formatters on custom types, type safety, and use the error handling for ergonomics.