Skip to content

Commit

Permalink
feat(io): File.isTTY
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Sep 11, 2023
1 parent a667a81 commit 8bfaa6d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lib/buzz_io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ export fn getStdErr(ctx: *api.NativeCtx) c_int {
return 1;
}

export fn FileIsTTY(ctx: api.NativeCtx) c_int {
const handle: std.fs.File.Handle = @intCast(
ctx.vm.bz_peek(0).integer(),
);

ctx.vm.bz_pushBool(std.os.isatty(handle));

return 1;
}

fn handleFileOpenError(ctx: *api.NativeCtx, err: anytype) void {
switch (err) {
error.AccessDenied,
Expand Down
7 changes: 7 additions & 0 deletions src/lib/io.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ extern fun getStdIn() > int;
extern fun getStdOut() > int;
|| @private
extern fun getStdErr() > int;
|| @private
extern fun FileIsTTY(int fd) > bool;

|| File mode with which you can open a file
export enum FileMode {
Expand Down Expand Up @@ -70,6 +72,11 @@ export object File {
fun write(str bytes) > void !> FileSystemError, ReadWriteError, UnexpectedError {
FileWrite(this.fd, bytes: bytes);
}

|| @return true if file is TTY
fun isTTY() > bool {
return FileIsTTY(this.fd);
}
}

|| stdin
Expand Down

0 comments on commit 8bfaa6d

Please sign in to comment.