#Equivalent to `isatty()` from C unistd.h

1 messages · Page 1 of 1 (latest)

merry iris
#

Is there a Zig equivalent in stdlib to the isatty() function in unistd.h from C? I'm making a logging function override for std_options that additionally shows the time and ANSI colors indicating the log level. Currently I'm doing this with:

const c = @cImport({
  @cInclude("time.h"); // for printing the time
  @cInclude("unistd.h); // for checking if the terminal supports colors
});

// ...

if (c.isatty(c.STDERR_FILENO) == 0) logging.use_colors = false;

// ...

I wanted to ask, is there an equivalent to c.isatty() in the stdlib?

#

Bonus points if you can tell me how to replace this code with pure Zig (stdlib):

var tmvalue: c.time_t = undefined;
var buf: [64]u8 = undefined;
_ = c.time(&tmvalue);
const tminfo = c.localtime(&tmvalue);
const buflen = c.strftime(@ptrCast(&buf), 64, "%Y-%m-%d %H:%M:%S", tminfo);
const time_txt = buf[0..buflen];
#

Output right now

severe lodge
#

having a strftime("%F %T", .{...}) would be nice indeed.

merry iris
hushed lynx
#

std.io.tty does more though, including windows console support and respecting envvars

hushed lynx
merry iris
hushed lynx