How to get terminal width through windows api in zig 0.16?

Hi, everyone! I’m a new comer to zig and trying to build a cli-app with the exciting zig 0.16. Hearing that a lot of efforts are made in 0.16 to both adopt the Io interface and adjust the windows api, I wonder how to get terminal width with these mechanisms?

Current investigation I’ve made: First, I find that `std.os.windows.CONSOLE.USER_IO.INFO.SCREEN_BUFFER` is the struct that defines the screen information. Then, I find that std.os.windows.CONSOLE.USER_IO.GET_SCREEN_BUFFER_INFO wraps `SCREEN_BUFFER` with a `Header` struct, which offers two methods called `operate` and `request`.

So, the question is, how can I get the terminal width with the `Header` struct? Or essentially, how does the operate and request methods work?

Here’s a usage example if that’s helpful:

https://codeberg.org/ziglang/zig/src/commit/9636d76b6d26d142f7d23e6d25eec1c28cf571b4/lib/std/Progress.zig#L1537-L1548

        var get_console_info = windows.CONSOLE.USER_IO.GET_SCREEN_BUFFER_INFO;
        switch (try get_console_info.operate(io, file)) {
            .SUCCESS => {
                global_progress.rows = @intCast(get_console_info.Data.dwWindowSize.Y);
                global_progress.cols = @intCast(get_console_info.Data.dwWindowSize.X);
            },
            else => {
                std.log.debug("failed to determine terminal size; using conservative guess 80x25", .{});
                global_progress.rows = 25;
                global_progress.cols = 80;
            },
        }
4 Likes