zig-learn/term.zig

28 lines
775 B
Zig
Raw Permalink Normal View History

2024-10-30 09:38:15 +00:00
const std = @import("std");
const c = @cImport({
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("sys/ioctl.h");
@cInclude("stdio.h");
});
const ioctl = std.c.ioctl;
pub const Winsize = struct {
ws_row: c_ushort = 0,
ws_col: c_ushort = 0,
ws_xpixel: c_ushort = 0,
ws_ypixel: c_ushort = 0,
};
pub fn getTermDimension() !Winsize {
var w = Winsize{};
var ret: u16 = 0;
const err = error{CUnknown};
//print("stdout_fileno: {}\n", .{@TypeOf(std.c.STDOUT_FILENO)});
ret = @intCast(ioctl(std.c.STDOUT_FILENO, c.TIOCGWINSZ, &w));
if (ret > 0) {
return err.CUnknown;
}
//print("winsize.ws_row: {}\nws_line: {}\nws_xpixel: {} \nws_ypixel: {}\n", .{ w.ws_row, w.ws_col, w.ws_xpixel, w.ws_ypixel });
return w;
}