|
No terminfo support? I'm probably tilting at windmills here, but I wish people wouldn't hardcode terminal escape codes. Considering zig's good interop with C, wiring up a call to tigetstr(). It's also important not to emit escape codes at all when TERM=dumb. (You'll get this behavior automatically if you implement color support by asking terminfo to the escape codes.) const c = @cImport({
@cInclude("ncurses.h");
@cInclude("term.h");
});
pub fn main() !void {
// Initialize terminfo
_ = c.setupterm(null, 1, null);
// Get capability strings
const setaf = c.tigetstr("setaf"); // set foreground
const setab = c.tigetstr("setab"); // set background
const sgr0 = c.tigetstr("sgr0"); // reset
// Parameterize for red foreground
const red = c.tparm(setaf, c.COLOR_RED, 0, 0, 0, 0, 0, 0, 0, 0);
// Use it
_ = c.printf("%sThis is red%s\n", red, sgr0);
}
|
A bunch of commonly used escape codes are pretty much universally identical. I wrote about this before: https://www.arp242.net/safeterm.html
By and large I think terminfo/$TERM is outdated, or at least partly. It's a 1970s/80s thing when terminals all did radically different things. But that hasn't been the case for decades now. You still need it for some things (kind of), but basic support like colours? Not so much.