I’ve been using zig for another project that I’ll hopefully soon post here as well, but one thing I noticed was that I was using a lot of TODOs to organize my ideas around the code and it was difficult for me to get a big picture, so I wrote a separate, small thing called todo-zen.
The idea is two-fold: Something useful for me at the moment while also exercising my zig.
One thing I did here, which maybe is my zig-newbieness speaking louder is that I couldn’t find a way in the current API to skip a certain portion of the directory tree from being traversed by walker, so I naively copied std.fs.Dir.Walker into the project and added a skip() function, so I can do something like:
while (try dirWalker.next()) |entry| {
// NOTE: Revisit blocking all the hidden files
if (entry.kind == .directory and std.mem.startsWith(u8, entry.path, ".")) {
dirWalker.skip();
continue;
}
// Normal logic
}
The concrete use-case for this project is that I don’t want to open and read several files in the .git and .zig-cache folder needlessly. I assume there might be a proper way to do that, but I just wanted to get this rolling for now.
Anyways, thanks in advance for any feedback as this is my first “real” zig app, although quite a simple one - a proper one will, as I said, soon be posted here as well
I think that skip function would be a good addition to the standard library.
In the implementation you could directly use if to unwrap the optional result of pop, to make the code simpler.
Instead of copying the code I probably would have written it as a freestanding function that reaches into the iterator (calling a fn skip(it:*Iterator) void), but that is more for having a quick/temporary workaround, without needing to patch the standard library or copy the implementation and is a subjective preference.
You can (and probably should) avoid all realpath calls btw, will send a PR with that change in a bit (EDIT: PR created).
A few potential improvements if you’re interested:
Output the line number along with the filename, e.g. main.zig:123
For a bit of fun, add a --random option that outputs one random TODO/FIXME/etc (to allow using it for FIXME roulette)
(alternatively, make each entry output on one line so | shuf | head -n1 works)
That would actually introduce a bug. The point of the if is to avoid closing the initial dir (context), so pop returning non-null isn’t the relevant bit of information.
I’m honestly quite happy to hear that and I’d love to contribute, though I’m unsure about the process yet. I’d definitely like to learn more about it and I could submit a PR if that’s the process with zig, but even if that’s not the case, just to mention that makes me happy to know I’m somehow contributing - even if at least with a welcomed idea.
Those are indeed good suggestions and I can look into them, thanks a lot for the feedback!
Nit-pick: I just tried to compile with the latest master of zig and it seems you’re using the stable version (0.14), so that did not work. You might want to specify this, since zig is evolving, sometimes very breaking - hello, writergate. There are a couple of options, just have a look at other zig libraries/tools. One would be to put something like this in your build.zig:
const min_zig = std.SemanticVersion.parse("0.14.0") catch unreachable;
const max_zig = std.SemanticVersion.parse("0.14.1") catch unreachable;
comptime {
if (builtin.zig_version.order(min_zig) == .lt) {
@compileError(std.fmt.comptimePrint(
"Your Zig version v{} does not meet the minimum build requirement of v{}",
.{ builtin.zig_version, min_zig },
));
}
if (builtin.zig_version.order(max_zig) == .gt) {
@compileError(std.fmt.comptimePrint(
"Your Zig version v{} does not meet the maximum build requirement of v{}",
.{ builtin.zig_version, max_zig },
));
}
}
Besides, this might actually be a FR for the build.zig.zon; AFAIK at the moment it only allows to set a minimum version.
Thanks for the feedback! Not nitpicky at all. It is nice that you brought it up since there are still many things I’m learning about the language, including the build system. I’ll make sure to update accordingly.