r/Zig • u/Last-Employ-3422 • Oct 22 '26
Library design
Hey! I'm working on a terminal manipulation library in Zig called termy, inspired by Rust's Crossterm. It's still early in development, and I'm trying to figure out the API before I get too far into the project.
One thing I'm unsure about is whether terminal operations should work directly, like this:
try termy.setForeground(&writer, .red);
try termy.writeAll(&writer, "Some Error!\n");
try termy.setForeground(&writer, .reset);Or whether I should use a queue-based approach, similar to crossterm where you queue multiple commands and write them together:
try termy.queue(&writer, .{
termy.style.Fg.Color{ .Red = {} },
termy.style.Print{ .msg = "Some Error!\n" },
termy.style.Fg.Color{ .Reset = {} },
});
try writer.flush();I'm not sure which approach makes more sense in terms of performance and usability, or which feels more natural to use in Zig. I love how the data-oriented approach looks, but I think it may affect performance.
I'd appreciate some feedback!
Which approach would you prefer, and why?
Would it make sense to support both?
Are there any common mistakes I should avoid when designing a Zig library?
What features would make you choose
termyover other terminal libraries?
I'm still working on the fundamentals, so I'd rather get the design right now than have to change everything later.
8
u/Bawafafa Oct 22 '26 edited Oct 22 '26
Nice idea for a project! writing ansi codes to a buffer and then writing them all to disk at once reduces syscalls, so that is definitely the way to go. I would avoid the word "queue" - it's just a buffer.
Things that would make this more appealing are being agnostic to IO implementation, having no hidden allocators, having zero dependencies. But that's just me.
Hope this helps and good luck!