Hey there was working on a code generator and was trying to get some better indentation on the output. I was wondering if there is somethings similar to the ‘*’ specifier in C for the print format options.
i.e. in C:
#include <stdio.h>
int main() {
printf("%*s\n", 10, "Hello");
}
Btw, if you are generating zig code, you can also use std.zig.Ast.parse to parse the generated code and then render it formatted with std.zig.Ast.render, this also gives you little sanity check that your generated code passes the AST check.
Thanks for the link. Unfortunately I don’t think I can do what I want, because the format has to be comptime known. I want the width parameter to be a function parameter, i.e.
pass indent: []const u8 instead of depth where indent can for example subslice into some (possibly static string literal) buffer that only contains the wanted indent character (up to some max depth).
even if your not generating zig, something like what @Cloudef suggested is a good idea.
I tested it in case I forgot the details, but indentation is just not working with slices for some reason, it does still work with other things like numbers.
you can use reference a parameter for almost any part, the exceptions are the alignment (<,^,>) and the fill char.
Remember that the format args are a stuct/tuple, and you reference fields by name/index.
Highlighting this in case you missed it, alignment+fill isnt working at all with slices for some reason. Thats why you it didnt work when you tried it.
it is ensure you print at least n chars, if you print less use this filler char.
the annoying part is I think I had the exact misunderstanding multiple times already, perhaps the docs should be made more clear. They also say it only works on numbers but thats just not true so they need an update anyway